Topic: Spring1.2 Support Hibernate3

  Print this page

1.Spring1.2 Support Hibernate3 Copy to clipboard
Posted by: andersonmao
Posted on: 2005-05-26 14:17

Congratulations the newly Spring 1.2 Support Hibernate 3!

spring's hibernate related packages are:
org.springframework.orm.hibernate3.*

Config the bean's xml file like this:

<!-- Hibernate SessionFactory -->
<bean id="sessionFactoryFoo" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref local="dataSourceFoo"/></property>
<property name="mappingResources">
<list>
<value>foo/model/FooA.hbm.xml</value>
<value>foo/model/FooB.hbm.xml</value>
<!-- ...... -->
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManagerFoo" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref local="sessionFactoryFoo"/></property>
</bean>

<bean id="fooADAO" class="foo.dao.FooADAOHibernateImpl">
<property name="sessionFactory"><ref local="sessionFactoryFoo"/></property>
</bean>

2.没必要都扔spring.xml里面去,将就原来的hibernate.cfb.xml也行。 [Re: andersonmao] Copy to clipboard
Posted by: mmwy
Posted on: 2005-05-28 21:57

以我做的一个测试代码中配置为例

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:/mapping/hibernate.cfg.xml</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>

<bean id="AbstractDAOProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="UserDAO"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
parent="AbstractDAOProxy">
<property name="target">
<bean class="dao.UserDAOHibernate">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
</property>
</bean>
</beans>


hibernate.cfb.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<hibernate-configuration>
<session-factory name="spring_hibernate">
<property name="hibernate.connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="hibernate.connection.url">
jdbc:oracle:thinAngry(DESCRIPTION = (ADDRESS_LIST = (ADDRESS =
(PROTOCOL = TCP)(HOST = xxx.xxx.xxx.xxx)(PORT = 1521)) )
(CONNECT_DATA = (SID = xx)(SERVER = DEDICATED)) )
</property>
<property name="hibernate.connection.username">xxxx</property>
<property name="hibernate.connection.password">
xxxxxxxxxxxx
</property>
<property name="hibernate.dbcp.maxActive">100</property>
<property name="hibernate.dbcp.whenExhaustedAction">1</property>
<property name="hibernate.dbcp.maxWait">120000</property>
<property name="hibernate.dbcp.maxIdle">10</property>
<property name="hibernate.dialect">
org.hibernate.dialect.OracleDialect
</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<property name="hibernate.cache.provider_class">
org.hibernate.cache.OSCacheProvider
</property>
<mapping resource="mapping/People.hbm.xml" />
<mapping resource="mapping/Lob.hbm.xml" />
<class-cache usage="read-only" class="domain.People" />
</session-factory>
</hibernate-configuration>

3.Re:没必要都扔spring.xml里面去,将就原来的hibernate.cfb.xml也行。 [Re: mmwy] Copy to clipboard
Posted by: andersonmao
Posted on: 2005-05-28 22:55

mmwy wrote:

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:/mapping/hibernate.cfg.xml</value>
</property>
</bean>


You are right.
Infact the hibernate.cfg.xml is also needed when using hibernate's SchemaExport. this reduce the replication of xxx.hbm.xml files.
I posted this mainly for that Springframework only support hibernate 2 before May,2005


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923