线程“AWT-EventQueue-0”中的异常javax.persistence.TransactionRequiredException:没有正在进行的事务

时间:2017-09-19 16:09:31

标签: java hibernate spring-data-jpa

好吧,我在论坛中查看了与我的问题相关的所有问题,但我无法解决我的问题。所以我向任何能帮我解决问题的人寻求帮助。

如果一切都在不使用@Transactional注释的情况下工作,但是我想让它以这种方式工作,等待评论。

这是我的persistence.xml:

    <persistence 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"
    version="2.0">
    <!-- Persistence Unit -->
    <persistence-unit name="databaseEstoque" transaction-type="RESOURCE_LOCAL">
        <!-- Implements to JPA, in this case Hibernate -->
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <!-- Here you can see all entities -->
        <class>com.systemhaus.examples.domain.Produto</class>
        <class>com.systemhaus.examples.domain.Gaveta</class>
        <class>com.systemhaus.examples.domain.AbstractOperacao</class>
        <class>com.systemhaus.examples.domain.AdicionaQuantidade</class>
        <class>com.systemhaus.examples.domain.RetiraQuantidade</class>

        <properties>
            <!-- JDBC properties -->

            <!-- Hibernate specific settings -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
        </properties>
    </persistence-unit>
</persistence>

这是我的applicationContext.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config />
    <bean id="myEmf"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="databaseEstoque" />
        <property name="dataSource" ref="dataSource"/>
        <property name="loadTimeWeaver">
            <bean
                class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
        </property>
    </bean>

    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <tx:annotation-driven transaction-manager="txManager" />

    <bean id="gavetaDAO" class="com.systemhaus.examples.infraestructure.GavetaDBDAO" />

    <bean id="produtoDAO" class="com.systemhaus.examples.infraestructure.ProdutoDBDAO" />

    <bean id="operacaoDAO" class="com.systemhaus.examples.infraestructure.OperacaoDBDAO" />

    <bean id="notaFiscalDAO" class="com.systemhaus.examples.infraestructure.NotaFiscalDBDAO" />

    <bean id="compraFacade" class="com.systemhaus.examples.application.CompraFacade">
        <constructor-arg ref="produtoDAO" />
        <constructor-arg ref="gavetaDAO" />
        <constructor-arg ref="operacaoDAO" />
        <constructor-arg ref="estoque" />
    </bean>

    <bean id="vendaFacade" class="com.systemhaus.examples.application.VendaFacade">
        <constructor-arg ref="produtoDAO" />
        <constructor-arg ref="gavetaDAO" />
        <constructor-arg ref="operacaoDAO" />
        <constructor-arg ref="estoque" />
    </bean>

    <bean id="notaFiscalFacade" class="com.systemhaus.examples.domain.NotaFiscalFacade">
        <constructor-arg ref="produtoDAO" />
        <constructor-arg ref="gavetaDAO" />
        <constructor-arg ref="operacaoDAO" />
        <constructor-arg ref="notaFiscalDAO" />
        <constructor-arg ref="estoque" />
    </bean>

    <bean id="estoque" class="com.systemhaus.examples.domain.Estoque">
        <constructor-arg ref="estoqueRepositoryDB" />
    </bean>

    <bean id="estoqueRepositoryDB"
        class="com.systemhaus.examples.infraestructure.EstoqueRepositoryDB">
        <constructor-arg ref="gavetaDAO" />
        <constructor-arg ref="produtoDAO" />
        <constructor-arg ref="operacaoDAO" />
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="org.h2.Driver" />
        <property name="url" value="jdbc:h2:/home/pauloprass/eclipse-workspace/Java_Spring_Examples/lib/databaseEstoque" />
        <property name="username" value="Paulo" />
        <property name="password" value="1" />
    </bean>

</beans>

这是我的方法:

private EntityManagerFactory emf;

    @PersistenceUnit
    public void setEntityManagerFactory(EntityManagerFactory emf) {
        this.emf = emf;
    }

    @Override
    @Transactional
    public void saveGaveta(Gaveta gaveta) {
        EntityManager em = emf.createEntityManager();
        em.persist(gaveta);
        em.flush();
    }

1 个答案:

答案 0 :(得分:0)

我能够解决我的问题,碰巧这是我第一次创建Maven项目。所以我已经在我的lib中导入了JAR,但我使用的是pom.xml,即使我正在更改我的pom.xml依赖项,他也使用从我的lib导入的类,我的解决方案是清理已编译的内容并从lib中删除所有JARS并在pom.xml中添加依赖项。