java - Configuring Spring + Hibernate JPA Transaction Manager through JTA -
i had config hibernate using resource-local transaction type:
persistence.xml:
<persistence-unit name="mypu" transaction-type="jta"> <provider>org.hibernate.jpa.hibernatepersistenceprovider</provider> </persistence-unit>
applicationcontext (dataaccess bit):
<bean class="org.springframework.orm.jpa.support.persistenceannotationbeanpostprocessor" /> <bean id="transactionmanager" class="org.springframework.orm.jpa.jpatransactionmanager" p:entitymanagerfactory-ref="entitymanagerfactory"></bean> <bean id="entitymanagerfactory" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean"> <property name="datasource" ref="datasource" /> <property name="jpavendoradapter" ref="jpaadapter" /> <property name="persistenceunitname" value="mypu"/> <property name="jpaproperties"> <props> <prop key="javax.persistence.validation.mode">none</prop> </props> </property> </bean> <bean id="entitymanager" class="org.springframework.orm.jpa.support.sharedentitymanagerbean"> <property name="entitymanagerfactory" ref="entitymanagerfactory" /> </bean> <bean id="sessionfactory" class="org.springframework.orm.jpa.vendor.hibernatejpasessionfactorybean"> <property name="entitymanagerfactory" ref="entitymanagerfactory" /> <!-- there other properties required? --> </bean> <bean id="jpaadapter" class="org.springframework.orm.jpa.vendor.hibernatejpavendoradapter"> <property name="showsql" value="true" /> <property name="generateddl" value="false" /> </bean> <bean id="datasource" class="org.springframework.jndi.jndiobjectfactorybean"> <property name="jndiname" value="java:comp/env/jdbc/cntxesdb" /> <property name="lookuponstartup" value="true" /> <property name="cache" value="true" /> <property name="proxyinterface" value="javax.sql.datasource" /> </bean>
but kind of transaction seems not work glassfish, had switch jta transactions.
the problem -- spring manage transaction creation (through @transactional) need define transactionmanager
bean jtatransactionmanager
included in spring-tx not accept entitymanagerfactory bean, not know entitymanager in order open/close/flush hibernate session.
so how can configure spring hibernate use jta transactions?
edit: turns out can use resource_local transactions glassfish, somehow cannot have persistence.xml file. renamed file my_persistence.xml
, configured localcontainerentitymanagerfactorybean
this:
<bean id="entitymanagerfactory" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean"> <property name="datasource" ref="datasource" /> <property name="jpavendoradapter" ref="jpaadapter" /> <property name="persistenceunitname" value="mypu"/> <property name="persistencexmllocation" value="classpath:meta-inf/my_persistence.xml" /> <property name="jpaproperties"> <props> <prop key="javax.persistence.validation.mode">none</prop> </props> </property> </bean>
i had similar problem , solved can see in little demo: https://github.com/miguelangelprogramacion/spring4_jpa_hibernate
with [1] reference, prefer use spring's transaction support before jta.
also, i've used annotation based approach.
[1] http://spring.io/blog/2011/08/15/configuring-spring-and-jta-without-full-java-ee/
Comments
Post a Comment