0

好吧,我正在尝试使用 DeltaSpike 的@Transactional,但从未调用过 TransactionalInterceptor。

这是我的 EntityManagerProducer:

public class EntityManagerProducer implements Serializable {

    private static final long serialVersionUID = 1L;


    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("generalPU");

    @Produces
    @TransactionScoped
    // is a bit better than @RequestScoped because it won't allow perform injection outside transaction context
    public EntityManager createEntityManager() {
        return emf.createEntityManager();
    }

    public void close(@Disposes EntityManager em) {
        if (em.isOpen()) {
            em.close();
        }
    }


    public EntityManagerFactory getEmf() {
        return emf;
    }


    public void setEmf(EntityManagerFactory emf) {
        this.emf = emf;
    }

}

我有一个注入 EntityManager 并使用 @Transactional 的类,请参阅:

@Named
@RequestScoped
public class BasicDAOImpl implements BasicDAO, Serializable {
    @Inject
    private EntityManager entityManager;

    @Transactional
    public void save(AbstractBean bean) {
    try {
        entityManager.merge(bean);
    } catch (Exception e) {
        logger.error("Erro ao tentar salvar. \n " + e);
    }
    }
}

我有一个带有拦截器的 beans.xml,看:

<?xml version="1.0"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://jboss.org/schema/cdi/beans_1_0.xsd">
     <interceptors>
        <class>org.apache.deltaspike.jpa.impl.transaction.TransactionalInterceptor</class>
    </interceptors>
</beans>

我该如何解决这个问题?我在 apache 页面(https://deltaspike.apache.org/documentation/jpa.html)中遵循完全相同的示例。

我正在使用 Tomcat7 + JDK 1.8 + CDI 1.2(焊接 2.3.0 最终实现)

编辑1:

这是我的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd ">


    <persistence-unit name="generalPU">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>java:/comp/env/jdbc/cedDS</non-jta-data-source>
        <properties>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="false" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.cache.use_second_level_cache"
                value="false" />
            <property name="hibernate.cache.use_query_cache" value="false" />
            <property name="hibernate.jdbc.batch_size" value="50" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />           
        </properties>
    </persistence-unit>

</persistence>

编辑 2:我在 pom.xml 中的依赖项

<dependency>
            <groupId>org.facebook4j</groupId>
            <artifactId>facebook4j-core</artifactId>
            <version>2.3.1</version>
        </dependency>

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.deltaspike.modules</groupId>
            <artifactId>deltaspike-jpa-module-api</artifactId>
            <version>1.7.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.deltaspike.modules</groupId>
            <artifactId>deltaspike-jpa-module-impl</artifactId>
            <version>1.7.1</version>
        </dependency>


        <!-- JASPER REPORT IMPORTS -->

        <dependency>
            <groupId>net.sourceforge.barbecue</groupId>
            <artifactId>barbecue</artifactId>
            <version>1.5-beta1</version>
        </dependency>


        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>5.0.1</version>
        </dependency>

        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports-fonts</artifactId>
            <version>4.0.0</version>
        </dependency>

        <dependency>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
            <version>1.4.01</version>
        </dependency>
        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xercesImpl</artifactId>
            <version>2.10.0</version>
        </dependency>


        <!-- FIM JASPER RESPORT IMPORTS -->

        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.1.6</version>
        </dependency>

        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.1.6</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>


        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
        </dependency>

        <!-- CDI -->
        <dependency>
            <groupId>org.jboss.weld.servlet</groupId>
            <artifactId>weld-servlet</artifactId>
            <version>2.3.0.Final</version>
        </dependency>

        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>


        <!-- HIBERNATE -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.10.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.6.10.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm-commons</artifactId>
            <version>3.1</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>20040616</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3</version>
        </dependency>

        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>3.5</version>
        </dependency>

        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>jsr250-api</artifactId>
            <version>1.0</version>
        </dependency>
4

1 回答 1

1

这对我有用,断点进入TransactionalInterceptor. 手动注册TransactionalInterceptor是没有必要的。

public class EntityManagerProducer implements Serializable {

    @Inject
    @PersistenceUnitName(value="generalPU")
    private EntityManagerFactory entityManagerFactory;

    @Produces
    @TransactionScoped
    protected EntityManager createTranEntityManager() {
        return this.entityManagerFactory.createEntityManager();
    }

    protected void closeEntityManager(@Disposes EntityManager entityManager){
        if (entityManager.isOpen()) {
            entityManager.close();
        }
    }
}
于 2016-12-11T21:41:49.423 回答