通过实体管理器,我试图将实体保存在数据库中,但我无法保存它。这是我的配置。

我有这个实体:

@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);
    }
}

实体管理器和数据库连接的 Spring 配置:
<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 没有很好地绑定(bind)到该方法,但我无法理解原因。有人知道为什么吗?

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

最佳答案

我遇到了与所描述的问题类似的问题。
我发现@Transactional 符号使用不正确的问题,特别是我错误地使用了 javax.transaction.Transactional 而不是 org.springframework.transaction.annotation.Transactional

可以在 javax.transaction.Transactional vs org.springframework.transaction.annotation.Transactional 中找到差异的详细说明

关于spring - 实体管理器不会在数据库中保留数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11357817/

10-09 06:14
查看更多