【问题标题】:Spring with JPA not persisting objects使用 JPA 的 Spring 不持久化对象
【发布时间】:2013-02-27 21:37:32
【问题描述】:

我已经阅读了许多使用 JPA 配置 spring 的教程。我正在使用本地 MySQL 数据库,并且我有这个上下文:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- DataSource Setup -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/test" />
        <property name="username" value="vitornobrega" />
        <property name="password" value="" />
    </bean>

    <!-- Entity Manager Factory -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.vitornobrega.myapp.entities" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="generateDdl" value="true" />
                <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
            </bean>
        </property>
    </bean>

    <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <tx:annotation-driven transaction-manager="txManager" />
</beans>

我在 JUnit 上进行了测试以测试我的 DAO,并且效果很好,但是当我尝试使用此配置持久化实体时,我从未在休眠状态中记录插入命令,但在测试用例中我看到了它。如果我尝试创建 entitymanager.flush,则会出现异常,因为任何事务都在运行。

我应该改变什么才能使这个实体在我的本地数据库上持久化? 谢谢

【问题讨论】:

  • 你能分享一下你的堆栈跟踪,看看错误是什么吗?所以我们可以更好地分析它并为您提供帮助。

标签: spring hibernate jpa entitymanager


【解决方案1】:

如果我尝试创建 entitymanager.flush,我会得到一个异常,因为任何事务都在运行。

如果我没看错,那么问题是您试图在不启动事务的情况下保存某些内容。更改持久状态时需要事务。

【讨论】:

    【解决方案2】:

    为了能够在数据库中持久化,必须在事务中。所以你可以更好地解决它是注释@Transactional ...例如关于方法

    这也让我前段时间疯狂了几个小时。

    【讨论】:

      【解决方案3】:

      你在你的类中使用注解吗?如果是这样,请确保你 <context:component-scan base-package="com.vitornobrega.myapp"> 因为从 jpa 配置中我可以看到你在哪里注入你的依赖项。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-19
        • 1970-01-01
        • 2017-04-09
        • 2012-11-29
        • 2017-10-10
        • 2018-05-01
        • 2012-01-17
        相关资源
        最近更新 更多