2017-06-14 92 views
0

我有一個數據庫代碼jar,我用於訪問數據庫的不同應用程序。我正在使用spring data jpa。我需要調用刷新來檢查某些其他應用程序中db中行的更改。這裏我如何實現它。 我StudentRepository接口:彈簧數據Jpa實體無法管理刷新時出現異常

public interface StudentRepository extends JpaRepository<StudentEntity, Integer>, StudentRepositoryCustom {} 

StudentRespositoryCustom接口

public interface StudentRepositoryCustom { 
    void detach(StudentEntity studentEntity); 
    void refresh(StudentEntity studentEntity);} 

StudentRepositoryCustomImpl

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.finalSessionImpl類返回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> 

我的春天數據庫配置文件是

<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 
+0

你忘了用'@ Service'註釋你的'StudentRepositoryCustomImpl'還是隻是一個錯字? –

回答

0

其實這是一個查詢問題。該查詢產生內部連接,因此返回空列表,因此拋出異常。我改變了映射,以便它可以產生外連接,並且它返回一個行並且它是成功的。

0

嘗試

public StudentEntity refresh(StudentEntity studentEntity) { 
    StudentEntity managedEntity = entityManager.find(StudentEntity.class, studentEntity.getId()); 
    entityManager.refresh(managedEntity); 
    return managedEntity; 
}} 

您所遇到的問題的原因是,在您的測試,你的studentEntity在交易之外被傳遞(findOne),因此不再管理。當它被傳遞到一個新的事務(刷新)時,它不被管理,並且引發這個錯誤。

或者,如果原始刷新是期望的行爲,則可以將整個測試包裝在一個事務中,這應該維護對測試對象的管理。