hibernate配置

由于要配置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时间不长,有错误的地方欢迎批评指正

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值