Hibernate 调用refresh时出现Spring数据Jpa实体未管理异常
我有一个db代码jar,我在不同的应用程序中使用它来访问db。我正在使用Hibernate 调用refresh时出现Spring数据Jpa实体未管理异常,hibernate,jpa,spring-data,spring-data-jpa,Hibernate,Jpa,Spring Data,Spring Data Jpa,我有一个db代码jar,我在不同的应用程序中使用它来访问db。我正在使用spring数据jpa。我需要调用refresh来检查来自其他应用程序的db行中的更改。下面是我如何实现它的。 我的StudentRepository界面: public interface StudentRepository extends JpaRepository<StudentEntity, Integer>, StudentRepositoryCustom {} 我的StudentRepository
spring数据jpa
。我需要调用refresh来检查来自其他应用程序的db行中的更改。下面是我如何实现它的。
我的StudentRepository
界面:
public interface StudentRepository extends JpaRepository<StudentEntity, Integer>, StudentRepositoryCustom {}
我的StudentRepositoryCustomImpl
class
public class StudentRepositoryImpl implements StudentRepositoryCustom {
@PersistenceContext
EntityManager entityManager;
@Override
@Transactional
public void detach(StudentEntity studentEntity) {
entityManager.detach(studentEntity);
}
@Override
@Transactional
public void refresh(StudentEntity studentEntity) {
entityManager.refresh(studentEntity);
}}
我的测试
@Test
public void refreshTest(){
StudentEntity studentEntity = studentRepository.findOne(6);
studentRepository.refresh(studentEntity);
}
但它抛出了一个例外:
org.springframework.dao.InvalidDataAccessApiUsageException: Entity not managed; nested exception is java.lang.IllegalArgumentException: Entity not managed
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:384)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:492)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
hibernate-core-5.2.1.final
SessionImpl
类中的此行返回null
EntityEntry entry = persistenceContext.getEntry( object );
我认为它应该返回实体
我的persistence.xml
文件是
<persistence-unit name="testPersitanceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.sample.dao.gen.StudentEntity</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"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.enable_lazy_load_no_trans" value="true" />
</properties>
</persistence-unit>
org.hibernate.jpa.HibernatePersistenceProvider
com.sample.dao.gen.StudentEntity
我的springdb配置文件是
<jpa:repositories base-package="com.sample.dao.repos"
entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"/>
<!-- Enable the component scan (auto wiring etc) for the following package -->
<context:component-scan base-package="com.sample.dao" />
<!-- Make sure the following is specified to enable transaction -->
<tx:annotation-driven />
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- This defines the entity manager factory with some custom properties -->
<bean id='entityManagerFactory' primary="true" class='org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean'>
<property name="persistenceUnitName" value="testPersitanceUnit"/>
<property name='dataSource' ref='dataSource' />
<property name="packagesToScan" value="com.sample.dao" />
enter code here
在这里输入代码
试试看
出现问题的原因是,在测试中,您的学生实体在事务(findOne)之外通过,因此不再进行管理。当它被传递到一个新事务(刷新)中时,它没有被管理,因此抛出这个错误
或者,如果原始刷新是所需的行为,则可以将整个测试包装在一个事务中,这将维护对测试对象的管理。实际上这是一个查询问题。查询正在生成内部联接,因此返回空列表,因此引发异常。我更改了映射,以便它可以生成外部联接,然后返回一行,结果成功。在重新设计之前尝试使用“合并”
public void refresh(T entity) {
this.entityManager.refresh(this.entityManager.merge(entity));
}
您是否忘记了用
@Service
为您的StudentRepositoryCustomImpl
添加注释,或者这只是一个输入错误?
public StudentEntity refresh(StudentEntity studentEntity) {
StudentEntity managedEntity = entityManager.find(StudentEntity.class, studentEntity.getId());
entityManager.refresh(managedEntity);
return managedEntity;
}}
public void refresh(T entity) {
this.entityManager.refresh(this.entityManager.merge(entity));
}