实体管理器不会在数据库中保留数据

时间:2012-07-06 07:35:34

标签: spring jpa

通过实体管理器,我试图将实体持久存储在数据库中,但我没有设法坚持下去。这是我的配置。

我有这个实体:

@Entity
@Table(name = "User")
public class UserModel implements Serializable, ModelItem {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column(nullable = false)
    private String username;

    @Column(nullable = false)
    private String password;

    @Column(nullable = false)
    private String name;

    private String surname;

    private String notes;

    private String cellphone;

    @Column(nullable = false)
    private String email;

    private Boolean enabled;

    //get and set methods
    .....
}

和我执行持久性的导入bean:

@Repository
public class ImportServiceImpl implements ImportService {

    @PersistenceContext
    protected EntityManager entityManager;

    @Transactional
    public boolean importExample() {
        User u= new User();
        u.setUsername("username");
        u.setPassword("password");
        u.setName("name");
        u.setEmail("email");
        entityManager.persist(u);
    }
}

实体管理器的弹簧配置和db连接:

<tx:annotation-driven transaction-manager="transactionManager" />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<tx:annotation-driven />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> 
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" /> <!-- Prints used SQL to stdout -->
            <property name="generateDdl" value="true" /> <!-- Generates tables. -->
            <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
        </bean>
    </property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url">
        <value>${db.url}</value>
    </property>
    <property name="username">
        <value>${db.username}</value>
    </property>
    <property name="password">
        <value>${db.password}</value>
    </property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    <property name="dataSource" ref="dataSource"/>
</bean>

和我的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_1_0.xsd"
             version="1.0">
    <persistence-unit name="application" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/testdata"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.password" value="password"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
        </properties>

    </persistence-unit>
</persistence>

因此,当我运行我的示例时,我不会收到任何错误,但实体不会保留。我还试图在持久化之后添加entityManager.flush()但在这种情况下我得到这个错误:

javax.persistence.TransactionRequiredException: no transaction is in progress
    at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:793)

所以我认为我的Transactional bean没有很好地绑定到该方法,但我无法理解其中的原因。有人知道为什么吗?

我还注意到在STS中,为这个事务生成了2个具有相同数据的bean,它看起来很奇怪(我不知道它是否是STS的错误,或者我的配置中存在创建2个bean的问题):

enter image description here

5 个答案:

答案 0 :(得分:4)

我遇到了类似于上述问题的问题。 我发现@Transactional表示法的使用不正确的问题,特别是我错误地使用了javax.transaction.Transactional而不是org.springframework.transaction.annotation.Transactional

有关差异的详细说明,请参见javax.transaction.Transactional vs org.springframework.transaction.annotation.Transactional

答案 1 :(得分:1)

也许尝试在@PersistenceContext注释中指定持久性单元名称“application”?

@PersistenceContext(unitname =“application”)

答案 2 :(得分:1)

我也遇到了同样的问题,因为Repoker说持久性供应商还可以,但是我的交易经理被搞砸了。我在application-config.xml中添加了transactionManager bean,就像这样

<bean id="transactionManager" cass="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="myEntityManagerFactory" />
</bean>

现在工作正常!!

答案 3 :(得分:0)

你可以在坚持和合并之后立即使用entityManager.flush()来解决你的问题

答案 4 :(得分:0)

您的上下文配置中有<tx:annotation-driven />次。我不确定这是一个合法的配置。

相关问题