0

I am having a strange problem deleting entities and I am thinking that this is because of wrong spring configuration / bug and i am not able to exactly point what it is . I am able to do reads , but whenever i try remove i get an error..

Following is my set up.. I have a spring based transaction management delegating to jboss's jta. Following is my spring configuration

<context:annotation-config />
<context:component-scan base-package="com.blah.blah">
    <context:exclude-filter type="regex"
        expression="com.blah.blah.batch.*" />
</context:component-scan>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="pmabDataSource" />
    <property name="persistenceUnitName" value="MABPersistenceEl" />
    <property name="jpaDialect" ref="eclipseLinkDialect" />
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence-eclipse.xml" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="generateDdl" value="false" />
        </bean>
    </property>

    <property name="jpaPropertyMap">
        <map>
            <entry key="eclipselink.target-server" value="JBOSS" />
            <entry key="eclipselink.target-database" value="Oracle" />
            <entry key="eclipselink.persistence-context.flush-mode" value="AUTO" />
            <entry key="eclipselink.jdbc.native-sql" value="false" />
            <entry key="eclipselink.weaving" value="false" />
            <entry key="eclipselink.logging.level" value="FINEST" />
            <entry key="eclipselink.logging.parameters" value="true" />
            <entry key="eclipselink.logging.exceptions" value="true" />
            <entry key="eclipselink.orm.throw.exceptions" value="true" />
        </map>
    </property>
</bean>

<bean id="eclipseLinkDialect" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />

<tx:jta-transaction-manager />

persistence file is as follows

<persistence version="1.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

    <persistence-unit name="MABPersistenceEl"
        transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>pmabDataSource</jta-data-source>
        <!-- ,,,, other things ommited for brevity/> -->
    </persistence-unit>
</persistence>

My basedao gets injected with the EntityManager like this

@PersistenceContext(unitName = "MABPersistenceEl")
protected EntityManager entityManager ;

somewhere down in my derived dao i have the following

UserRole newRole=entityManager.find(UserRole.class,urole.getId()) ;
entityManager.remove(newRole);

I get the following 09:53:00,386 DEBUG Creating new EntityManager for shared EntityManager invocation 09:53:00,413 DEBUG [EntityManagerFactoryUtils]:328 Closing JPA EntityManager 09:53:04,119 DEBUGSharedEntityManagerCreator$SharedEntityManagerInvocationHandler:231 Creating new EntityManager for shared EntityManager invocation 09:53:04,146 DEBUG [EntityManagerFactoryUtils]:328 Closing JPA EntityManager 09:53:05,760 DEBUG Creating new EntityManager for shared EntityManager invocation 09:53:05,781 DEBUG [EntityManagerFactoryUtils]:328 Closing JPA EntityManager

That is springs EntityManager creator is creating an entityManager each time it is accessed, instead of returning the one for the transaction . This is causing EL libraries to go berserk. java.lang.IllegalArgumentException: Entity must be managed to call remove: UserRole@419d, try merging the detached and try the remove again. at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.performRemove(UnitOfWorkImpl.java:3559) at org.eclipse.persistence.internal.jpa.EntityManagerImpl.remove(EntityManagerImpl.java:518)

How do i configure the springs entity manager creator to give me back the entity manager for the transaction instead of creating one each time ?

user1540821
  • 41
  • 1
  • 6

1 Answers1

0

I did make the following change and it now works.. Code:

<!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean below) -->
<tx:advice id="pmabTxAdvice" transaction-manager="transactionManager">
    <!-- the transactional semantics... -->
    <tx:attributes>
        <!-- all methods starting with 'get' are read-only -->
        <!-- other methods use the default transaction settings (see below) -->
      <tx:method name="*" propagation="REQUIRED" read-only="false"/>
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut id="serviceOperation"
        expression="execution(* com.foo.foobar.abc.*.*.*(..))" />
    <aop:advisor advice-ref="pmabTxAdvice" pointcut-ref="serviceOperation" />
</aop:config>

Hope this helps someone

user1540821
  • 41
  • 1
  • 6
  • I don't quite understand what was the solution. I have the same issue when deploying my application. The same of message "Creating new EntityManager for shared EntityManager invocation" is logged many times, what configuration is causing this? – dinhokz Oct 07 '20 at 22:15