Spring中注入Hibernate的SessionFactory

Spring中注入Hibernate的SessionFactory
2008年04月24日 星期四 14:29
-------------------------------------------------------------------------------------------
Spring中注入Hibernate的SessionFactory
-------------------------------------------------------------------------------------------
这种注入方式非常清晰,将Hibernate的配置文件分离出来,可以根据需要进行单独配置,而无需改动Spring的任何配置。

感觉,在集成Spring和Hibernate的时候,版本问题需要特别注意,否则总是无法实例化一个SessionFactory,例如:

Error creating bean with name 'springSessionFactory' defined in class path resource [applicationContext.xml]:
Instantiation of bean failed; nested exception……
-------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
drop TABLE sampledb.orders;
CREATE TABLE sampledb.orders(
`ID` BIGINT NOT NULL,
`ORDER_NUMBER` VARCHAR(15),
`CUSTOMER_ID` BIGINT,
PRIMARY KEY(`ID`)
)
ENGINE = InnoDB;

drop TABLE `sampledb`.`customers`;
CREATE TABLE `sampledb`.`customers` (
`ID` BIGINT NOT NULL,
`NAME` VARCHAR(45),
PRIMARY KEY(`ID`)
)
ENGINE = InnoDB;


alter table sampledb.orders
add constraint constraint_constraint_fk
foreign key (CUSTOMER_ID)
references sampledb.customers(ID);

-----------------------------------------------------------------------------------------------------------

CustomersDAO
-------------------------------------------------------------------------------------------
public class CustomersDAO extends BaseHibernateDAO {
private static final Log log = LogFactory.getLog(CustomersDAO.class);


private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) { // 注入SessionFactory
this.sessionFactory = sessionFactory;
}
public void save(Customers transientInstance) {
Session session = null;
Transaction tx = null;
try {
session = sessionFactory.openSession();
tx = session.beginTransaction();
session.save(transientInstance);
tx.commit();
} catch (HibernateException e) {
if (tx != null) {
tx = null;
}
e.printStackTrace();
} finally {
session.close();
}
}
}
-------------------------------------------------------------------------------------------
applicationContext.xml
-------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">


<bean
>
<property
value="file:src/hibernate.cfg.xml">
</property>
</bean>



<bean >
<property >
<ref bean="springSessionFactory" />
</property>
</bean>


</beans>
-------------------------------------------------------------------------------------------
Demo.java
-------------------------------------------------------------------------------------------
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import db.Customers;
import db.CustomersDAO;
public class Demo {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
CustomersDAO dao = (CustomersDAO) ctx.getBean("customersDAO");
Customers c = new Customers();
c.setId(2l);
c.setName("xx");
dao.save(c);
System.out.println("END");
}
}


-------------------------------------------------------------------------------------------

本篇日志被作者设置为禁止发表新评论


©2008 Baidu



引文来源 Spring中注入Hibernate的SessionFactory_熊熊之家
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值