您好,我启动了将Jersey与spring集成的应用程序。为了测试我的资源,我正在使用spring-test,如下所示:

@ContextConfiguration(locations = { "classpath*:testApplicationContext.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(defaultRollback = false) //just to check if txmanager was 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());
    }


问题是我所有的测试都在@Before方法中中断。为什么?因为用户ID为空。
让我们回顾一下我的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>


我的UserDAO类:

@Component
public class UserDAO {

    private GenericDAO<Long, User> dao;

    @PersistenceContext
    private EntityManager manager;

    @Transactional
    User save(User user) {
        return dao.save(user);
    }
...


有人可以帮我解决这个问题吗?
谢谢

编辑添加一些控制台日志:

13/10/14 14:16:50  INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader:315
Loading XML bean definitions from 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 from 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 and supported for component scanning
13/10/14 14:16:50  INFO org.springframework.context.annotation.ClassPathBeanDefinitionScanner:231
JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
13/10/14 14:16:50  INFO org.springframework.context.annotation.ClassPathBeanDefinitionScanner:239
JSR-330 'javax.inject.Named' annotation found and supported for component scanning
13/10/14 14:16:50  INFO org.springframework.context.annotation.ClassPathBeanDefinitionScanner:239
JSR-330 'javax.inject.Named' annotation found and supported for 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 from class path resource [testDatabase.properties]
13/10/14 14:16:50  INFO org.springframework.beans.factory.config.PropertyPlaceholderConfigurer:172
Loading properties file from 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 and supported for autowiring
13/10/14 14:16:50  INFO org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor:141
JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
13/10/14 14:16:50  INFO org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean:287
Building JPA container EntityManagerFactory for persistence unit 'default'
13/10/14 14:16:50  INFO org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean:287
Building JPA container EntityManagerFactory for 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 factory 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 factory 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 factory 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 factory hierarchy
13/10/14 14:16:51  INFO org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean:441
Closing JPA EntityManagerFactory for persistence unit 'default'
13/10/14 14:16:51  INFO org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean:441
Closing JPA EntityManagerFactory for persistence unit 'default'


编辑2在我的测试类中放入事务注释后,我得到:

Caught exception while allowing TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener@47b42535] to process 'after' execution for 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: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:522)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724)
    at org.springframework.test.context.transaction.TransactionalTestExecutionListener$TransactionContext.endTransaction(TransactionalTestExecutionListener.java:591)
    at org.springframework.test.context.transaction.TransactionalTestExecutionListener.endTransaction(TransactionalTestExecutionListener.java:297)
    at org.springframework.test.context.transaction.TransactionalTestExecutionListener.afterTestMethod(TransactionalTestExecutionListener.java:192)
    at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:406)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:91)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: javax.persistence.RollbackException: Error while committing the transaction
    at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:92)
    at 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 the Session after an exception occurs)
    at org.hibernate.event.internal.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:79)
    at org.hibernate.event.internal.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:194)


编辑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) {
        return (T) entityManager.find(clazz, pk);
    }

    public T save(T entity) {
        entityManager.persist(entity);
        return entity;
    }

    public void update(T entity) {
        entityManager.merge(entity);
    }

    public void delete(T entity) {
        entityManager.remove(entity);
    }

    @SuppressWarnings("unchecked")
    public List<T> findAll() {
        return entityManager.createQuery(("FROM " + clazz.getName() + " obj")).getResultList();
    }

    public List<?> listByHql(String hql, HqlParameter... params) {
        Query query = entityManager.createQuery(hql);

        for (HqlParameter param : params)
            param.apply(query);

        return query.getResultList();
    }

    public Object uniqueResultByHql(String hql, HqlParameter... params) {
        Query query = entityManager.createQuery(hql);

        for (HqlParameter param : params)
            param.apply(query);

        try {
            return query.getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }

}


编辑4用户实体代码

@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() {
    }


编辑5数据库

+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| 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)


编辑6我更改了这一行:

@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)


现在,我又遇到了一个例外:

java.lang.AbstractMethodError: org.joda.time.contrib.hibernate.PersistentDateTime.nullSafeSet(Ljava/sql/PreparedStatement;Ljava/lang/Object;ILorg/hibernate/engine/spi/SessionImplementor;)V
    at org.hibernate.type.CustomType.nullSafeSet(CustomType.java:155)
    at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2602)

最佳答案

聊天讨论摘要


将@Transactional添加到测试类
在@TransactionConfiguration中添加transactionManager


@TransactionConfiguration(transactionManager =“ transactionManager”,defaultRollback = true)
@交易
//测试类

一些论坛建议使用Jadira
带有此注释

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

09-12 11:38