Spring注入Hibernate的SessionFactory不分离配置文件
2008年04月24日 星期四 14:30
-------------------------------------------------------------------------------------------
Spring中注入Hibernate的SessionFactory
不分离配置文件
-------------------------------------------------------------------------------------------
原因是工具myEclipse在建立SessionFactory的时候,少加一个包 commons-pool-1.3.jar 补上去问题解决。
该包下载地址
http://mirrors.ibiblio.org/pub/mirrors/maven2/commons-pool/commons-pool/1.3/commons-pool-1.3.jar
-----------------------------------------------------------------------------------------------------------
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 HibernateDaoSupport {
private static final Log log = LogFactory.getLog(CustomersDAO.class);
protected void initDao() {
// do nothing
}
public void save(Customers transientInstance) {
log.debug("saving Customers instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
}
-------------------------------------------------------------------------------------------
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="com.mysql.jdbc.Driver">
</property>
<property
value="jdbc:mysql://localhost:3306/sampledb">
</property>
<property value="root"></property>
</bean>
<bean
>
<property >
<ref bean="localMysql" />
</property>
<property >
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property >
<list>
<value>db/Orders.hbm.xml</value>
<value>db/Customers.hbm.xml</value></list>
</property></bean>
<bean >
<property >
<ref bean="localSessionFactory" />
</property>
</bean>
<bean >
<property >
<ref bean="localSessionFactory" />
</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 {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
CustomersDAO dao = (CustomersDAO) ctx.getBean("CustomersDAO");
Customers c = new Customers();
c.setId(4l);
c.setName("xxxx");
dao.save(c);
System.out.println("END");
}
}
-------------------------------------------------------------------------------------------
Spring中注入Hibernate的SessionFactory
不分离配置文件
-------------------------------------------------------------------------------------------
原因是工具myEclipse在建立SessionFactory的时候,少加一个包 commons-pool-1.3.jar 补上去问题解决。
该包下载地址
http://mirrors.ibiblio.org/pub/mirrors/maven2/commons-pool/commons-pool/1.3/commons-pool-1.3.jar
-----------------------------------------------------------------------------------------------------------
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 HibernateDaoSupport {
private static final Log log = LogFactory.getLog(CustomersDAO.class);
protected void initDao() {
// do nothing
}
public void save(Customers transientInstance) {
log.debug("saving Customers instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
}
-------------------------------------------------------------------------------------------
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="com.mysql.jdbc.Driver">
</property>
<property
value="jdbc:mysql://localhost:3306/sampledb">
</property>
<property value="root"></property>
</bean>
<bean
>
<property >
<ref bean="localMysql" />
</property>
<property >
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property >
<list>
<value>db/Orders.hbm.xml</value>
<value>db/Customers.hbm.xml</value></list>
</property></bean>
<bean >
<property >
<ref bean="localSessionFactory" />
</property>
</bean>
<bean >
<property >
<ref bean="localSessionFactory" />
</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 {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
CustomersDAO dao = (CustomersDAO) ctx.getBean("CustomersDAO");
Customers c = new Customers();
c.setId(4l);
c.setName("xxxx");
dao.save(c);
System.out.println("END");
}
}
-------------------------------------------------------------------------------------------
本篇日志被作者设置为禁止发表新评论
©2008 Baidu
引文来源 Spring注入Hibernate的SessionFactory不分离配置文件_熊熊之家