补充说明:
不同的应用场景,解决方案也不同。
例如下文的配置实例,是创建了两个事务管理器。
如果项目要求必须只能存在一个事务管理,解决方案可参考:http://today.java.net/pub/a/today/2006/08/31/jotm-transactions-in-spring-and-hibernate.html
----------------配置文件
-------------------------------------META-INF/persistence.xml-------------------------------------------------------------
01 |
<? xml version = "1.0" encoding = "UTF-8" ?> |
02 |
< persistence xmlns = "http://java.sun.com/xml/ns/persistence" |
03 |
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
04 |
xsi:schemaLocation = "http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" |
08 |
< persistence-unit name = "mysql" transaction-type = "RESOURCE_LOCAL" > |
09 |
< provider >org.hibernate.ejb.HibernatePersistence</ provider > |
10 |
< non-jta-data-source >mysqlDataSource</ non-jta-data-source > |
12 |
< class >com.sunhope.model.core.ExternalEnvironment</ class > |
15 |
< property name = "hibernate.dialect" value = "org.hibernate.dialect.MySQL5Dialect" /> |
16 |
< property name = "hibernate.archive.autodetection" value = "false" /> |
17 |
< property name = "hibernate.transaction.auto_close_session" value = "false" /> |
18 |
< property name = "hibernate.hbm2ddl.auto" value = "update" /> |
22 |
< persistence-unit name = "postgre" transaction-type = "RESOURCE_LOCAL" > |
23 |
< provider >org.hibernate.ejb.HibernatePersistence</ provider > |
25 |
< non-jta-data-source >postgreDataSource</ non-jta-data-source > |
27 |
< class >com.sunhope.model.baseinfo.BdStorageCompany</ class > |
28 |
< class >com.sunhope.model.baseinfo.BdHouseType</ class > |
29 |
< class >com.sunhope.model.baseinfo.BdStorage</ class > |
30 |
< class >com.sunhope.model.core.StoreEnvironment</ class > |
33 |
< property name = "hibernate.dialect" value = "org.hibernate.dialect.PostgreSQLDialect" /> |
34 |
< property name = "hibernate.archive.autodetection" value = "false" /> |
35 |
< property name = "hibernate.transaction.auto_close_session" value = "false" /> |
36 |
< property name = "hibernate.hbm2ddl.auto" value = "update" /> |
-------------------------------------applicationContext.xml-------------------------------------------------------------
001 |
<? xml version = "1.0" encoding = "UTF-8" ?> |
002 |
< beans xmlns = "http://www.springframework.org/schema/beans" |
003 |
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
004 |
xmlns:tx = "http://www.springframework.org/schema/tx" |
005 |
xsi:schemaLocation="http://www.springframework.org/schema/beans |
006 |
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd |
007 |
http://www.springframework.org/schema/tx |
008 |
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> |
010 |
< bean id = "defaultPersistenceUnitManager" |
011 |
class = "org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager" > |
012 |
< property name = "persistenceXmlLocation" value = "classpath:META-INF/persistence.xml" /> |
014 |
< property name = "dataSourceLookup" > |
016 |
class = "org.springframework.jdbc.datasource.lookup.BeanFactoryDataSourceLookup" /> |
021 |
< tx:annotation-driven transaction-manager = "postgreEntityManagerFactory" /> |
023 |
< bean id = "postgreJpaVendor" |
024 |
class = "org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" > |
025 |
< property name = "showSql" value = "true" /> |
026 |
< property name = "generateDdl" value = "true" /> |
029 |
< bean id = "postgreDataSource" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method = "close" > |
030 |
< property name = "driverClassName" value = "org.postgresql.Driver" /> |
031 |
< property name = "url" value = "..." /> |
032 |
< property name = "username" value = "..." /> |
033 |
< property name = "password" value = "..." /> |
034 |
< property name = "initialSize" value = "1" /> |
035 |
< property name = "maxActive" value = "100" /> |
036 |
< property name = "maxIdle" value = "8" /> |
037 |
< property name = "minIdle" value = "1" /> |
040 |
< bean id = "postgreEntityManagerFactory" |
041 |
class = "org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" > |
042 |
< property name = "persistenceUnitManager" ref = "defaultPersistenceUnitManager" /> |
043 |
< property name = "persistenceUnitName" value = "postgre" /> |
044 |
< property name = "jpaVendorAdapter" ref = "postgreJpaVendor" /> |
045 |
< property name = "loadTimeWeaver" > |
047 |
class = "org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" /> |
049 |
< property name = "jpaDialect" > |
050 |
< bean class = "org.springframework.orm.jpa.vendor.HibernateJpaDialect" > |
055 |
< bean id = "postgreTransactionManager" class = "org.springframework.orm.jpa.JpaTransactionManager" > |
056 |
< property name = "entityManagerFactory" ref = "postgreEntityManagerFactory" /> |
057 |
< qualifier value = "postgreEM" /> |
058 |
< property name = "jpaDialect" > |
059 |
< bean class = "org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> |
064 |
< tx:annotation-driven transaction-manager = "mysqlEntityManagerFactory" /> |
066 |
< bean id = "mysqlJpaVendor" |
067 |
class = "org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" > |
068 |
< property name = "showSql" value = "true" /> |
069 |
< property name = "generateDdl" value = "true" /> |
072 |
< bean id = "mysqlDataSource" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method = "close" > |
073 |
< property name = "driverClassName" value = "org.gjt.mm.mysql.Driver" /> |
074 |
< property name = "url" value = "..." /> |
075 |
< property name = "username" value = "..." /> |
076 |
< property name = "password" value = "..." /> |
077 |
< property name = "initialSize" value = "1" /> |
078 |
< property name = "maxActive" value = "100" /> |
079 |
< property name = "maxIdle" value = "8" /> |
080 |
< property name = "minIdle" value = "1" /> |
083 |
< bean id = "mysqlEntityManagerFactory" |
084 |
class = "org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" > |
085 |
< property name = "persistenceUnitManager" ref = "defaultPersistenceUnitManager" /> |
086 |
< property name = "persistenceUnitName" value = "mysql" /> |
087 |
< property name = "jpaVendorAdapter" ref = "mysqlJpaVendor" /> |
088 |
< property name = "loadTimeWeaver" > |
090 |
class = "org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" /> |
092 |
< property name = "jpaDialect" > |
093 |
< bean class = "org.springframework.orm.jpa.vendor.HibernateJpaDialect" > |
098 |
< bean id = "mysqlTransactionManager" class = "org.springframework.orm.jpa.JpaTransactionManager" > |
099 |
< property name = "entityManagerFactory" ref = "mysqlEntityManagerFactory" /> |
100 |
< qualifier value = "mysqlEM" /> |
101 |
< property name = "jpaDialect" > |
102 |
< bean class = "org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> |
---------------------注解
-------------------------------------DAO-------------------------------------------------------------
------------MySqlBaseDaoImpl.java
1 |
@Transactional (value= "mysqlEM" ) |
2 |
public class MySqlBaseDaoImpl<T> implements IMySqlBaseDao<T>{ |
3 |
@PersistenceContext (unitName= "mysql" ) |
4 |
protected EntityManager em; |
------------PostgreSqlBaseDaoImpl.java
1 |
@Transactional (value= "postgreEM" ) |
2 |
public class PostgreSqlBaseDaoImpl<T> implements IPostgreSqlBaseDao<T>{ |
3 |
@PersistenceContext (unitName= "postgre" ) |
4 |
protected EntityManager em; |
-------------------------------------Service-------------------------------------------------------------
---------------ExternalEnvironmentServiceImpl.java
2 |
@Transactional (value= "mysqlEM" ) |
3 |
public class ExternalEnvironmentServiceImpl extends |
4 |
MySqlBaseDaoImpl<ExternalEnvironment> implements IExternalEnvironmentService { |
--------------------BdStorageCompanyServiceImpl.java
2 |
@Transactional (value= "postgreEM" ) |
3 |
public class BdStorageCompanyServiceImpl extends PostgreSqlBaseDaoImpl<BdStorageCompany> |
4 |
implements IBdStorageCompanyService { |
-------------------------------------Model(Entity)-------------------------------------------------------------
-------------ExternalEnvironment.java
1 |
@Entity (name = "TBL_EXTERNALENVIRONMENT" ) |
2 |
public class ExternalEnvironment extends BaseModel{ |
-----------------BdStorageCompany.java
1 |
@Entity (name = "TBL_BDSTORAGECOMPANY" ) |
2 |
public class BdStorageCompany extends BaseModel { |