分离后查询的Spring Data错误

时间:2016-11-26 15:14:47

标签: spring jpa spring-data spring-data-jpa

我正在使用Spring数据1.10.5和JPA 2.1以及Spring 4.3

这是我的配置

@Bean
    public JpaTransactionManager transactionManager(){
        JpaTransactionManager res = new JpaTransactionManager();
        res.setEntityManagerFactory(entityManagerFactory());

        return res;
    }

@Bean
    public EntityManagerFactory entityManagerFactory() throws PropertyVetoException{
        LocalContainerEntityManagerFactoryBean res = new LocalContainerEntityManagerFactoryBean();
        res.setDataSource(datasource());
        res.setPackagesToScan("com.myapp.core.model");

        Map<String,Object> jpaPropertiesMap = new HashMap<>();
        jpaPropertiesMap.put("hibernate.hbm2ddl.auto", hibernateHbm2ddl);
        res.setJpaPropertyMap(jpaPropertiesMap);


        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
        hibernateJpaVendorAdapter.setShowSql(hibernateShowSql);
        hibernateJpaVendorAdapter.setDatabasePlatform(hibernateDialect);
        res.setJpaVendorAdapter(hibernateJpaVendorAdapter);
        res.afterPropertiesSet();

        return res.getObject();
    }

    @Bean
    public DataSource datasource() throws PropertyVetoException {
        ComboPooledDataSource pooledDataSource = new ComboPooledDataSource();
        pooledDataSource.setDriverClass(dbDriverClassName);
        pooledDataSource.setJdbcUrl(dbUrl);
        pooledDataSource.setUser(dbUser);
        pooledDataSource.setPassword(dbPassword);
        pooledDataSource.setMinPoolSize(2);
        pooledDataSource.setInitialPoolSize(5);
        pooledDataSource.setMaxPoolSize(20);
        pooledDataSource.setMaxIdleTime(14400);
        pooledDataSource.setIdleConnectionTestPeriod(21600);

        return pooledDataSource;
    }

我有一些简单关系的简单实体

@Entity
@Table(name = "advertising")
public class Advertising{
    @ManyToOne
    private WebUserStatus webUserStatus;
    @ManyToOne
    private Location location;
    ... ...
}

一切都工作正常一年,但如果我做以下代码

entityManager.detach(advertisingOne); 然后是webUserStatus上的查询(它只是一个选择。所以读取数据并且不写任何内容的东西)我有以下错误。 错误很奇怪,我不明白为什么错误引用了我没有做任何事情的Location实体。

javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.myapp.core.model.Location
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
    at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1407)
    at org.hibernate.query.internal.AbstractProducedQuery.getSingleResult(AbstractProducedQuery.java:1444)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$SingleEntityExecution.doExecute(JpaQueryExecution.java:210)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:82)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:116)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:106)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:482)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
    at com.sun.proxy.$Proxy100.getActiveStatus(Unknown Source)
    at com.myapp.core.manager.WebUserStatusManager.getActiveStatus(WebUserStatusManager.java:16)

拜托,你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

我正在使用@Transactional测试方法。

在生产环境中一切都很好,但在测试中缺少一些东西。

为了获得干净的情况,有时需要一个entityManager.clear();