0

エンティティの削除で奇妙な問題が発生しています。これは、間違ったスプリング構成/バグが原因であると考えており、それが何であるかを正確に指摘することはできません。読み取りを行うことはできますが、削除しようとするとエラーが発生します..

以下は私のセットアップです。jboss の jta に委譲するスプリング ベースのトランザクション管理があります。以下は私の春の設定です

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

私の basedao には、このように EntityManager が注入されます

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

私の派生daoのどこかに私は次のものを持っています

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

次の 09:53:00,386 DEBUG Making new EntityManager for shared EntityManager invocation 09:53:00,413 DEBUG [EntityManagerFactoryUtils]:328 Closing JPA EntityManager 09:53:04,119 DEBUGSharedEntityManagerCreator$SharedEntityManagerInvocationHandler:231 Making new EntityManager for shared EntityManager invocation 09: 53:04,146 DEBUG [EntityManagerFactoryUtils]:328 JPA EntityManager を閉じています 09:53:05,760 DEBUG 共有 EntityManager 呼び出しのための新しい EntityManager を作成しています 09:53:05,781 DEBUG [EntityManagerFactoryUtils]:328 JPA EntityManager を閉じています

つまり、Springs EntityManager 作成者は、 transaction 用のものを返すのではなく、アクセスされるたびに entityManager を作成しています。これにより、EL ライブラリが暴走します。java.lang.IllegalArgumentException: Entity must be managed to call remove: UserRole@419d、デタッチされたものをマージして、削除を再試行してください。org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.performRemove(UnitOfWorkImpl.java:3559) で org.eclipse.persistence.internal.jpa.EntityManagerImpl.remove(EntityManagerImpl.java:518) で

毎回作成するのではなく、トランザクションのエンティティ マネージャーを返してくれるように、Springs エンティティ マネージャー クリエーターを構成するにはどうすればよいですか?

4

1 に答える 1

0

私は次の変更を行いましたが、現在は機能しています。コード:

<!-- 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>

これが誰かに役立つことを願っています

于 2012-07-27T21:32:18.473 に答える