java - Spring+JPA/Hibernate questions -
i have working code saves entity db using ejb+jpa+hibernate. need change ejb spring.
below simplified manager class.
//@stateless - changed @service @service public class manager { //@ejb - changed autowired @autowired private clientdao clientdao; public void addclient(client client) { clientdao.addclient(client); } }
below dao class.
//@stateless - changed @repository @repository public class jpaclientdao implements clientdao { @persistencecontext(unitname="clientsservice") private entitymanager em; public void addclient(client client) { em.persist(client); } }
below persistence.xml.
<?xml version="1.0" encoding="utf-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="clientsservice" transaction-type = "jta"> <provider>org.hibernate.jpa.hibernatepersistenceprovider</provider> <jta-data-source>myjtadatabase</jta-data-source> <class>com.entity.client</class> <properties> <property name="hibernate.archive.autodetection" value="class"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> <property name="hbm2ddl.auto" value="update"/> </properties> </persistence-unit> </persistence>
applicationcontext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="com" /> <context:annotation-config/> </beans>
resoures.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <resource id="myjtadatabase" type="datasource"> jdbcdriver org.apache.derby.jdbc.clientdriver jdbcurl jdbc:derby://localhost:1527/c:/clientdb username test password 123456 validationquery = select 1 jtamanaged true </resource> </resources>
questions.
1) when used ejb, had container-managed transactions. should manage transactions using spring?
2) need use spring framework transaction management? there alternatives?
found examples http://www.codingpedia.org/ama/java-persistence-example-with-spring-jpa2-and-hibernate/, , cant undestand code below spring specific or suits me.
<!-- ************ jpa configuration *********** --> <tx:annotation-driven transaction-manager="transactionmanager" /> <bean id="transactionmanager" class="org.springframework.orm.jpa.jpatransactionmanager"> <property name="entitymanagerfactory" ref="entitymanagerfactory" /> </bean> <bean id="entitymanagerfactory" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean"> <property name="persistencexmllocation" value="classpath:config/persistence-demo.xml" /> <property name="datasource" ref="restdemods" /> <property name="packagestoscan" value="org.codingpedia.demo.*" /> <property name="jpavendoradapter"> <bean class="org.springframework.orm.jpa.vendor.hibernatejpavendoradapter"> <property name="showsql" value="true" /> <property name="databaseplatform" value="org.hibernate.dialect.mysqldialect" /> </bean> </property> </bean>
3) need edit java code or should steps in xml configuration?
any useful links encouraged.
1) when used ejb, had container-managed transactions. should manage transactions using spring?
answer : spring provides container managed transaction support (see jta transaction manager , sample oracle article) , application managed transaction (which means application can declaratively/programatically manage transactions minimal effort using spring transaction api ) . see spring transaction doc
2) need use spring framework transaction management? there alternatives?
answer: if spring framework not managing transactions, container need manage them..you have alternatives of java ee jta implementations opensource jbossts or enterprise jta implementations oracle-weblogicjtatransactionmanager or ibm-websphereuowtransactionmanager, can find how use them in same point 1 spring documentation. can have own transaction manager implemented.
but if using spring framework why not benefit spring transaction management plenty of configurations possible, (spring + hibernate transaction manager, spring + jpacontainermanager, spring+ jtacontainermanager etc..)
3) need edit java code or should steps in xml configuration?
answer: transcation manager xml config seems fine use jpatransactionmanager, can initiate transactions in service layer java code annotating @transactional typically handles service methods participate in transaction according configured transaction manager.
@service @org.springframework.transaction.annotation.transactional public class manager {
Comments
Post a Comment