Saturday 15 June 2013

java - Jersey + Spring = @Transactional not working -



java - Jersey + Spring = @Transactional not working -

hello started app bailiwick of jersey integrated spring. test resources i´m using spring-test, this:

@contextconfiguration(locations = { "classpath*:testapplicationcontext.xml" }) @runwith(springjunit4classrunner.class) @transactionconfiguration(defaultrollback = false) //just check if txmanager working public class userdaotest { @autowired private userdao dao; @autowired private authorizationverifyer verifyer; @before public void init() { user user = new user(null, "name", "email", "pass", datetime.now(), true); user saveduser = dao.save(user); assertnotnull(saveduser); assertnotnull(saveduser.getid()); }

the problem tests break in @before method. why? because user id null. lets go on testapplicationcontext.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" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="com.sifionsolution.sig.authorization" /> <!-- inject properties --> <bean class="org.springframework.beans.factory.config.propertyplaceholderconfigurer"> <property name="locations"> <list> <value>testdatabase.properties</value> </list> </property> </bean> <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource"> <property name="driverclassname" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${user}"/> <property name="password" value="${password}"/> </bean> <!-- jpa entitymanagerfactory --> <bean id="emf" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean" depends-on="flyway"> <property name="datasource" ref="datasource" /> <property name="persistenceprovider"> <bean class="org.hibernate.ejb.hibernatepersistence" /> </property> <property name="packagestoscan"> <list> <value>com.sifionsolution.sig.authorization.entity</value> </list> </property> </bean> <!-- transaction manager --> <bean id="transactionmanager" class="org.springframework.orm.jpa.jpatransactionmanager"> <property name="entitymanagerfactory" ref="emf" /> </bean> <bean id="flyway" class="org.flywaydb.core.flyway" init-method="migrate"> <property name="datasource" ref="datasource"/> </bean> <tx:annotation-driven transaction-manager="transactionmanager" /> </beans>

my userdao class:

@component public class userdao { private genericdao<long, user> dao; @persistencecontext private entitymanager manager; @transactional user save(user user) { homecoming dao.save(user); } ...

can 1 help me working? give thanks you

edit adding of console logs:

13/10/14 14:16:50 info org.springframework.beans.factory.xml.xmlbeandefinitionreader:315 loading xml bean definitions url [file:/d:/workspace/sig-authorization-jax-rs/bin/testapplicationcontext.xml] 13/10/14 14:16:50 info org.springframework.beans.factory.xml.xmlbeandefinitionreader:315 loading xml bean definitions url [file:/d:/workspace/sig-authorization-jax-rs/bin/testapplicationcontext.xml] 13/10/14 14:16:50 info org.springframework.context.annotation.classpathbeandefinitionscanner:231 jsr-250 'javax.annotation.managedbean' found , supported component scanning 13/10/14 14:16:50 info org.springframework.context.annotation.classpathbeandefinitionscanner:231 jsr-250 'javax.annotation.managedbean' found , supported component scanning 13/10/14 14:16:50 info org.springframework.context.annotation.classpathbeandefinitionscanner:239 jsr-330 'javax.inject.named' annotation found , supported component scanning 13/10/14 14:16:50 info org.springframework.context.annotation.classpathbeandefinitionscanner:239 jsr-330 'javax.inject.named' annotation found , supported component scanning 13/10/14 14:16:50 info org.springframework.context.support.genericapplicationcontext:510 refreshing org.springframework.context.support.genericapplicationcontext@162cc04e: startup date [mon oct 13 14:16:50 brt 2014]; root of context hierarchy 13/10/14 14:16:50 info org.springframework.context.support.genericapplicationcontext:510 refreshing org.springframework.context.support.genericapplicationcontext@162cc04e: startup date [mon oct 13 14:16:50 brt 2014]; root of context hierarchy 13/10/14 14:16:50 info org.springframework.beans.factory.config.propertyplaceholderconfigurer:172 loading properties file class path resource [testdatabase.properties] 13/10/14 14:16:50 info org.springframework.beans.factory.config.propertyplaceholderconfigurer:172 loading properties file class path resource [testdatabase.properties] 13/10/14 14:16:50 info org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor:141 jsr-330 'javax.inject.inject' annotation found , supported autowiring 13/10/14 14:16:50 info org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor:141 jsr-330 'javax.inject.inject' annotation found , supported autowiring 13/10/14 14:16:50 info org.springframework.orm.jpa.localcontainerentitymanagerfactorybean:287 building jpa container entitymanagerfactory persistence unit 'default' 13/10/14 14:16:50 info org.springframework.orm.jpa.localcontainerentitymanagerfactorybean:287 building jpa container entitymanagerfactory persistence unit 'default' 13/10/14 14:16:51 info org.springframework.beans.factory.support.defaultlistablebeanfactory:603 pre-instantiating singletons in org.springframework.beans.factory.support.defaultlistablebeanfactory@247fd5c5: defining beans [org.springframework.context.annotation.internalconfigurationannotationprocessor,org.springframework.context.annotation.internalautowiredannotationprocessor,org.springframework.context.annotation.internalrequiredannotationprocessor,org.springframework.context.annotation.internalcommonannotationprocessor,org.springframework.context.annotation.internalpersistenceannotationprocessor,tokendao,userdao,tokencreator,tokenvalidator,authorizationverifyer,authenticateduserbuilder,permissionbuilder,rolewrapperbuilder,org.springframework.beans.factory.config.propertyplaceholderconfigurer#0,datasource,emf,transactionmanager,flyway,org.springframework.aop.config.internalautoproxycreator,org.springframework.transaction.annotation.annotationtransactionattributesource#0,org.springframework.transaction.interceptor.transactioninterceptor#0,org.springframework.transaction.config.internaltransactionadvisor,org.springframework.context.annotation.configurationclasspostprocessor.importawareprocessor]; root of mill hierarchy 13/10/14 14:16:51 info org.springframework.beans.factory.support.defaultlistablebeanfactory:603 pre-instantiating singletons in org.springframework.beans.factory.support.defaultlistablebeanfactory@247fd5c5: defining beans [org.springframework.context.annotation.internalconfigurationannotationprocessor,org.springframework.context.annotation.internalautowiredannotationprocessor,org.springframework.context.annotation.internalrequiredannotationprocessor,org.springframework.context.annotation.internalcommonannotationprocessor,org.springframework.context.annotation.internalpersistenceannotationprocessor,tokendao,userdao,tokencreator,tokenvalidator,authorizationverifyer,authenticateduserbuilder,permissionbuilder,rolewrapperbuilder,org.springframework.beans.factory.config.propertyplaceholderconfigurer#0,datasource,emf,transactionmanager,flyway,org.springframework.aop.config.internalautoproxycreator,org.springframework.transaction.annotation.annotationtransactionattributesource#0,org.springframework.transaction.interceptor.transactioninterceptor#0,org.springframework.transaction.config.internaltransactionadvisor,org.springframework.context.annotation.configurationclasspostprocessor.importawareprocessor]; root of mill hierarchy 13/10/14 14:16:51 info org.springframework.context.support.genericapplicationcontext:1042 closing org.springframework.context.support.genericapplicationcontext@162cc04e: startup date [mon oct 13 14:16:50 brt 2014]; root of context hierarchy 13/10/14 14:16:51 info org.springframework.context.support.genericapplicationcontext:1042 closing org.springframework.context.support.genericapplicationcontext@162cc04e: startup date [mon oct 13 14:16:50 brt 2014]; root of context hierarchy 13/10/14 14:16:51 info org.springframework.beans.factory.support.defaultlistablebeanfactory:444 destroying singletons in org.springframework.beans.factory.support.defaultlistablebeanfactory@247fd5c5: defining beans [org.springframework.context.annotation.internalconfigurationannotationprocessor,org.springframework.context.annotation.internalautowiredannotationprocessor,org.springframework.context.annotation.internalrequiredannotationprocessor,org.springframework.context.annotation.internalcommonannotationprocessor,org.springframework.context.annotation.internalpersistenceannotationprocessor,tokendao,userdao,tokencreator,tokenvalidator,authorizationverifyer,authenticateduserbuilder,permissionbuilder,rolewrapperbuilder,org.springframework.beans.factory.config.propertyplaceholderconfigurer#0,datasource,emf,transactionmanager,flyway,org.springframework.aop.config.internalautoproxycreator,org.springframework.transaction.annotation.annotationtransactionattributesource#0,org.springframework.transaction.interceptor.transactioninterceptor#0,org.springframework.transaction.config.internaltransactionadvisor,org.springframework.context.annotation.configurationclasspostprocessor.importawareprocessor]; root of mill hierarchy 13/10/14 14:16:51 info org.springframework.beans.factory.support.defaultlistablebeanfactory:444 destroying singletons in org.springframework.beans.factory.support.defaultlistablebeanfactory@247fd5c5: defining beans [org.springframework.context.annotation.internalconfigurationannotationprocessor,org.springframework.context.annotation.internalautowiredannotationprocessor,org.springframework.context.annotation.internalrequiredannotationprocessor,org.springframework.context.annotation.internalcommonannotationprocessor,org.springframework.context.annotation.internalpersistenceannotationprocessor,tokendao,userdao,tokencreator,tokenvalidator,authorizationverifyer,authenticateduserbuilder,permissionbuilder,rolewrapperbuilder,org.springframework.beans.factory.config.propertyplaceholderconfigurer#0,datasource,emf,transactionmanager,flyway,org.springframework.aop.config.internalautoproxycreator,org.springframework.transaction.annotation.annotationtransactionattributesource#0,org.springframework.transaction.interceptor.transactioninterceptor#0,org.springframework.transaction.config.internaltransactionadvisor,org.springframework.context.annotation.configurationclasspostprocessor.importawareprocessor]; root of mill hierarchy 13/10/14 14:16:51 info org.springframework.orm.jpa.localcontainerentitymanagerfactorybean:441 closing jpa entitymanagerfactory persistence unit 'default' 13/10/14 14:16:51 info org.springframework.orm.jpa.localcontainerentitymanagerfactorybean:441 closing jpa entitymanagerfactory persistence unit 'default'

edit 2 after putting transactional annotation in test class got:

caught exception while allowing testexecutionlistener [org.springframework.test.context.transaction.transactionaltestexecutionlistener@47b42535] process 'after' execution test: method [public void com.sifionsolution.sig.authorization.dao.userdaotest.shouldloginactiveuser()], instance [com.sifionsolution.sig.authorization.dao.userdaotest@3f6df5a], exception [java.lang.abstractmethoderror: org.joda.time.contrib.hibernate.persistentdatetime.nullsafeset(ljava/sql/preparedstatement;ljava/lang/object;ilorg/hibernate/engine/spi/sessionimplementor;)v] org.springframework.transaction.transactionsystemexception: not commit jpa transaction; nested exception javax.persistence.rollbackexception: error while committing transaction @ org.springframework.orm.jpa.jpatransactionmanager.docommit(jpatransactionmanager.java:522) @ org.springframework.transaction.support.abstractplatformtransactionmanager.processcommit(abstractplatformtransactionmanager.java:755) @ org.springframework.transaction.support.abstractplatformtransactionmanager.commit(abstractplatformtransactionmanager.java:724) @ org.springframework.test.context.transaction.transactionaltestexecutionlistener$transactioncontext.endtransaction(transactionaltestexecutionlistener.java:591) @ org.springframework.test.context.transaction.transactionaltestexecutionlistener.endtransaction(transactionaltestexecutionlistener.java:297) @ org.springframework.test.context.transaction.transactionaltestexecutionlistener.aftertestmethod(transactionaltestexecutionlistener.java:192) @ org.springframework.test.context.testcontextmanager.aftertestmethod(testcontextmanager.java:406) @ org.springframework.test.context.junit4.statements.runaftertestmethodcallbacks.evaluate(runaftertestmethodcallbacks.java:91) @ org.springframework.test.context.junit4.statements.springrepeat.evaluate(springrepeat.java:72) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:231) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:88) @ org.junit.runners.parentrunner$3.run(parentrunner.java:238) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:63) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:236) @ org.junit.runners.parentrunner.access$000(parentrunner.java:53) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:229) @ org.springframework.test.context.junit4.statements.runbeforetestclasscallbacks.evaluate(runbeforetestclasscallbacks.java:61) @ org.springframework.test.context.junit4.statements.runaftertestclasscallbacks.evaluate(runaftertestclasscallbacks.java:71) @ org.junit.runners.parentrunner.run(parentrunner.java:309) @ org.springframework.test.context.junit4.springjunit4classrunner.run(springjunit4classrunner.java:174) @ org.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference.java:50) @ org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution.java:38) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:467) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:683) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:390) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:197) caused by: javax.persistence.rollbackexception: error while committing transaction @ org.hibernate.ejb.transactionimpl.commit(transactionimpl.java:92) @ org.springframework.orm.jpa.jpatransactionmanager.docommit(jpatransactionmanager.java:513) ... 25 more caused by: org.hibernate.assertionfailure: null id in com.sifionsolution.sig.authorization.entity.user entry (don't flush session after exception occurs) @ org.hibernate.event.internal.defaultflushentityeventlistener.checkid(defaultflushentityeventlistener.java:79) @ org.hibernate.event.internal.defaultflushentityeventlistener.getvalues(defaultflushentityeventlistener.java:194)

edit 3 genericdao

public class genericdao<pk, t> { private final entitymanager entitymanager; private final class<?> clazz; public genericdao(class<?> clazz, entitymanager entitymanager) { this.clazz = clazz; this.entitymanager = entitymanager; } @suppresswarnings("unchecked") public t getbyid(pk pk) { homecoming (t) entitymanager.find(clazz, pk); } public t save(t entity) { entitymanager.persist(entity); homecoming entity; } public void update(t entity) { entitymanager.merge(entity); } public void delete(t entity) { entitymanager.remove(entity); } @suppresswarnings("unchecked") public list<t> findall() { homecoming entitymanager.createquery(("from " + clazz.getname() + " obj")).getresultlist(); } public list<?> listbyhql(string hql, hqlparameter... params) { query query = entitymanager.createquery(hql); (hqlparameter param : params) param.apply(query); homecoming query.getresultlist(); } public object uniqueresultbyhql(string hql, hqlparameter... params) { query query = entitymanager.createquery(hql); (hqlparameter param : params) param.apply(query); seek { homecoming query.getsingleresult(); } grab (noresultexception e) { homecoming null; } } }

edit 4 user entity code

@entity @table(name = "tb_user") @xmlrootelement public class user { @id @generatedvalue(strategy = generationtype.identity) private long id; private string name; private string email; private string password; @type(type = "org.joda.time.contrib.hibernate.persistentdatetime") private datetime registration; @column(columndefinition = "tinyint(1)") private boolean active; @onetomany(mappedby = "user") private list<userrole> userroles; @onetomany(mappedby = "user") private set<userpermission> userpermissions; public user() { }

edit 5 database

+--------------+--------------+------+-----+---------+----------------+ | field | type | null | key | default | | +--------------+--------------+------+-----+---------+----------------+ | id | bigint(20) | no | pri | null | auto_increment | | name | varchar(255) | no | | null | | | password | varchar(128) | no | | null | | | email | varchar(255) | no | uni | null | | | active | tinyint(1) | no | | null | | | registration | datetime | no | | null | | +--------------+--------------+------+-----+---------+----------------+ 6 rows in set (0.04 sec)

edit 6 changed line:

@transactionconfiguration(transactionmanager = "transactionmanager", defaultrollback = true)

and i´m getting exception:

java.lang.abstractmethoderror: org.joda.time.contrib.hibernate.persistentdatetime.nullsafeset(ljava/sql/preparedstatement;ljava/lang/object;ilorg/hibernate/engine/spi/sessionimplementor;)v @ org.hibernate.type.customtype.nullsafeset(customtype.java:155) @ org.hibernate.persister.entity.abstractentitypersister.dehydrate(abstractentitypersister.java:2602)

summary of chat discussion

adding @transactional test class adding transactionmanager in @transactionconfiguration

@transactionconfiguration(transactionmanager = "transactionmanager", defaultrollback = true) @transactional //test class

some forum suggest utilize jadira annotation

@type(type = "org.jadira.usertype.dateandtime.joda.persistentdatetimewithzone")

java spring junit jersey transactional

No comments:

Post a Comment