hibeenate配置文件|java eclipse怎样自动生成hibernate配置映射文件

|

1. hibernate怎么读取配置文件

Hibernate加载配置文件的两种方法:(一):configuration.configure();(默认加载src下的hibernate.cfg.xml文件) 代码:private static SessionFactory sf = null; static{Configuration configuration = new Configuration();configuration.configure();sf = configuration.buildSessionFactory();} 如果使用了这种加载方法那么在hibernate.cfg.xml文件中就需要mapping XX.hbm.xml文件了 代码:<session-factory><mapping resource="domain/Students.hbm.xml"/><mapping resource="domain/Techer.hbm.xml"/></session-factory>第二种方法使用的是con.addClass(class);方法 在test类中代码:private static SessionFactory sf = null; static{Configuration configuration = new Configuration();configuration.configure();configuration.addClass(Techer.class);configuration.addClass(Students.class);sf = configuration.buildSessionFactory();}

2. hibernate配置文件在哪

调用new Configuraction()时,Hibernate在classpath的根目录下搜索名为hibernate.properties的文件。如果找到了,所有hibernate.*属性都会被加载并添加到Configuration对象。调用configure()时,Hibernate在classpath的根目录下搜索名为hibernate.cfg.xml的文件,如果无法找到会抛出一个异常。当然,如果你没有这个配置文件,就不一定要调用这个方法。如果XML配置文件中的设置与较早设置的属性完全相同,XML设置就覆盖前面的设置。hibernate.properties配置文件的位置始终在classpath的根目录中,处在任何包之外。如果要使用一个不同的文件,或者要Hibernate在classpath的子目录中查找XML配置文件,就必须把路径当作configure()方法的一个实参进行传递:SessionFactory sessionFactory = new Configuration().configure("auction.cfg.xml").buildSessionFactory();

3. 如何在hibernate.cfg.xml中的主配置文件中配置多个映射文件

需要写个路径来读spring配置文件。或者你把映射的东西拷贝到hibernate.cfg.xml中,把hibernate.cfg.xml放到src目录下,然后运行生成数据库表也行的。

4. java eclipse怎样自动生成hibernate配置映射文件

步骤如下:

1、创建数据库,创建相应的表;

2、点击图标,选择MyEclipse Datebase Explorer;

5. hibernate.cfg.xml怎么添加xml配置文件

Hibernate默认的配置文件路裤档没径是/hibernate.cfg.xml,如果你不想使用默认路径。比如你把配置文件都放到conf目录下面。那如何修改Hibernate的config路径呢。其实也很简单。看我蠢漏写的HibernateUti类。(Hibernate hibernate.cfg.xml 配置路径) (Hibernate hibernate.cfg.xml 配置路径)(Hibernate hibernate.cfg.xml 配置路径)(Hibernate hibernate.cfg.xml 配置路径)(Hibernate hibernate.cfg.xml 配置路径)(Hibernate hibernate.cfg.xml 配置路径)我们只要把sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();改为:sessionFactory = new AnnotationConfiguration().configure(new File("../conf/hibernate.cfg.xml")).buildSessionFactory();就可也了。../conf/hibernate.cfg.xml就是你的路胡纳径。

6. 在Eclipse中创建Hibernate配置文件的代码写在src目录下,代码写在什么文件里

hibernate 的配置文件写在根目录下,你说的代码,是指各个表的实体类的配置,新建包,entity,把实体类和实体类的配置文件都写在这里,当然,你配置文件中的路径要写对。

7. Hibernate3 和Hibernate4 在配置文件上的区别

在使用hibernate之前要首先对hibernate进行一些基础的配置信息,像映射文件XXX.hbm.xml XXX代表当前的domain的模型类名1 <?xml version="1.0"?>2 <!DOCTYPE hibernate-mapping PUBLIC3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">5 <hibernate-mapping package="com._520it.day01._01_hello.domain">6 <!– 映射关系 –>7 <class name="User" table="t_user">8 <id name="id" column="uid">9 <generator class="native"/>10 </id>11 <property name="name" column="uname"/>12 <property name="salary" column="usalary"/>13 <property name="hiredate" column="uhiredate"/>14 </class>15 </hibernate-mapping>下一步配置hibernate.cfg.xml文件Hibernate3 在配置的时候,需要创建一个hibernate.cfg.xml文件,然后配置session-factory,把连接数据库的基本信息都以属性的形式罗列出来1 <!DOCTYPE hibernate-configuration PUBLIC 2 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 3 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 4 5 <hibernate-configuration> 6 <session-factory> 7 <!– 包含连接数据库的基本信息 –> 8 <!– 连接数据库的方言 –><!–这里注意在property 的name 和value中不能有空格的出现–> 9 <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>10 <!– 连接数据库的驱动 –>11 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>12 <!– 连接数据库的url –>13 <property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>14 <property name="hibernate.connection.username">root1</property>15 <property name="hibernate.connection.password">admin1</property>16 <!– 是否显示sql语句 –>17 <property name="hibernate.show_sql">true</property>18 19 <!– 关联映射文件 –>20 <mapping resource="com/_520it/day01/_01_hello/domain/User.hbm.xml"/>21 </session-factory>22 </hibernate-configuration>而在Hibernate4中,官方建议在另外创建一个hibernate.properties文件,在这个配置文件里,存放和数据库连接的基本信息即:把上述文件中的对应的参数在这里写出来,同样不要有空格的出现1 hibernate.dialect=org.hibernate.dialect.MySQL5Dialect2 hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect3 hibernate.dialect=org.hibernate.dialect.MySQLMyISAMDialect4 hibernate.connection.driver_class=com.mysql.jdbc.Driver5 hibernate.connection.url=jdbc:mysql:///hibernate6 hibernate.connection.username=root17 hibernate.connection.password=admin1这个时候在原来的hibernate.cfg.xml文件中就只剩下了关联映射文件的配置信息1 <!DOCTYPE hibernate-configuration PUBLIC2 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"3 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">4 <!– 当用这种方式的时候需要配置一个hibernate.properties文件 –>5 <hibernate-configuration>6 <session-factory>7 <!– 关联映射文件 –>8 <mapping resource="com/_520it/day01/_01_hello/domain/User.hbm.xml"/>9 </session-factory>10 </hibernate-configuration>相比之下,hibernate3中的所有信息都在一个配置文件中,比较集中,结构清晰,hibernate4中,文件分开进行配置,便于管理,以后如果想要添加多个映射文件,也只需要在bibernate.cfg.xml文件中进行添加就好了,不用再去管数据库的连接配置文件在hibernate3中创建sessionFactory1 //1.创建配置对象2 Configuration config = new Configuration();3 //2.读取配置文件4 config.configure();5 //3.创建sessionFactory对象6 SessionFactory sessionFactory = config.buildSessionFactory();7 //4.获取session对象8 Session session =sessionFactory.openSession();但是这个方法,官方已经不建议使用,目前依然可以使用在hibernate4中,这个方法需要在创建sessionFactory的时候传入一个registy的参数//1.创建配置对象Configuration config = new Configuration();//2.读取配置文件config.configure();//3.创建serviceRegistry对象ServiceRegistry registry = new ().build();//4.创建sessionFactory对象SessionFactory sessionFactory = config.buildSessionFactory(registry);//5.获取session对象Session session =sessionFactory.openSession();以上两种方法都可以使用,仅凭个人喜好


赞 (0)