InvalidDataAccessApiUsageException after switchin from sessionFactory to EntityManage
there were an existing application, which everything worked fine. But for various reasons I had to switch from sessionFactory "org.springframework.orm.hibernate3.LocalSessionFa ctoryBean" to the entityManagerFactory "org.springframework.orm.jpa.LocalContainerEntityM anagerFactoryBean". Now I�m getting an "org.springframework.dao.InvalidDataAccessApiUsage Exception: Removing a detached instance server.model.instance.ComponentInstance" at a junit-integration.Test. After a five or six hours of fruitless googling, I decided to write my issue here. I would be thankful and It would be great for any help.
This is the junit test code, and the code which is mentioned at the stack trace:
Code:
public void deleteComponentInstance ( ComponentInstance instanceToDelete ) {
setComponentInstanceForeignKeyToNull ( instanceToDelete );
deleteAndFlush ( instanceToDelete );
}
the Method deleteAndFlush
Code:
public <P> void deleteAndFlush ( P persistentObject ) {
Session session = sessionFactory.getCurrentSession ();
session.delete ( persistentObject );
session.flush ();
}
the Method setComponentInstanceForeignKeyToNull
Code:
public void setComponentInstanceForeignKeyToNull ( Class<?> clazz, String attribute, Object value ) {
StringBuilder hqlQuery = new StringBuilder ()
.append ( "update " )
.append ( clazz.getSimpleName () )
.append ( " entity set entity." ).append ( attribute )
.append ( " = null where entity." ).append ( attribute )
.append ( " = :actualValue" );
Session currentSession = this.sessionFactory.getCurrentSession ();
currentSession.createQuery ( hqlQuery.toString () )
.setEntity ( "actualValue", value )
.executeUpdate ();
}
this is my "beans.xml" after switching to the entityManager
Code:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="spring-jpa" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
<property name="database" value="HSQL" />
</bean>
</property>
</bean>
<bean id="sessionFactory" factory-bean="entityManagerFactory" factory-method="getSessionFactory" />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
this is my beans.xml befor the switch:
Code:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingLocations" ref="hibernateMappingFiles" />
<property name="hibernateProperties" ref="hibernateProperties" />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
my new needed persistence.xml for the EntityManager:
Code:
<persistence-unit name="spring-jpa">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.hbm2ddl.auto" value="none" />
<property name="javax.persistence.validation.mode" value="none" />
<property name="hibernate.jdbc.use_streams_for_binary"
value="true" />
<property name="hibernate.jdbc.fetch_size" value="100" />
<property name="hibernate.current_session_context_class" value="org.springframework.orm.hibernate3.SpringSessionContext" />
</properties>