由于要配置hibernate search的原因,因此这次更换了个新版本的hibernate4.3.6,新版本的hibernate相较于先前版本的在配置上有些许改动,记录在下:
1.在hibernate.cfg.xml中网上提到了这些问题,
http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd
- dtd的版本问题,有些地方说需要与hibernate版本号相同,我查阅了官方网站,最新的也只有3.0版本,这里统一为3.0就可以了,
- dtd网站链接的问题,有些地方说需要写成下面这样,我测试的时候,发现两者都是可行的
http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd
2.sessionFactory获取
之前的版本
Configuration configuration= new Configuration().configure();
sessionFactory = <span style="font-family: Arial, Helvetica, sans-serif;">configuration</span>.buildSessionFactory();
新版本,你会发现buildSessionFactory方法被废除了,增加了ServiceRegistry类,sessionFactory获取的时候,需要先实现这个类
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
附上hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/database</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">XXXXXX</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<span style="white-space:pre"> </span><mapping resource="com/gigi/model/Product.hbm.xml"/>
</session-factory>
</hibernate-configuration>
本人学习hibernate时间不长,有错误的地方欢迎批评指正

被折叠的 条评论
为什么被折叠?



