Spring注入Hibernate的SessionFactory不分离配置文件

 
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");
    }
}



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

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


©2008 Baidu



引文来源   Spring注入Hibernate的SessionFactory不分离配置文件_熊熊之家
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值