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");
}
}
-------------------------------------------------------------------------------------------
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_熊熊之家