11

我有使用EntityManager.persist(Object)方法的问题。现在,当我摆脱其他问题时,应用程序可以在没有异常的情况下工作,但对象没有放入我的数据库中。

我的实体类:

@Entity
@Table(name ="Chain")
public class Chain implements Serializable{

@Id
@Column(name = "id")
  private Long id;
@Column(name = "date")
  private Date date;
@Column(name = "name")
  private String name;
//setters and getters
}

我的道课:

@Transactional
@Repository("ChainDao")
public class ChainDaoImpl implements ChainDao{


    private EntityManager em;


    @PersistenceContext
    public void setEntityManager(EntityManager em) {
        this. em = em;
    }

    public int saveChain(Chain chain) {
        chain.setDate(new Date());
        chain.setId((long)44);
        Boolean a;
        em.persist(chain);

        return 222;
    }
}

我的 xml 上下文:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
    <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"></property></bean>


    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <bean class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory"
            ref="entityManagerFactory" />
    </bean>

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

pereistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
       <persistence-unit name="sample">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <!-- Scan for annotated classes and Hibernate mapping XML files -->
        <properties>
           <property name="hibernate.archive.autodetection" value="class, hbm"/>
           <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
           <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/database"/>
           <property name="hibernate.connection.username" value="postgres"/>
           <property name="hibernate.connection.password" value="pwd"/>
           <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
           <property name="hibernate.show_sql" value="true"/>
        </properties>
        </persistence-unit>

    </persistence>

有谁知道我错过了什么?

4

9 回答 9

9

你得到一个特定的例外吗?如果你是,知道它是什么会很有帮助。

在 Stackoverflow 上已经有许多类似的问题:

Spring事务上下文不持久化数据

Spring、Hibernate 和 JPA:在 entitymanager 上调用 persist 似乎并没有提交到数据库

这些建议您应该尝试在 ,em.flush()之后添加em.persist(chain)并更改@transactional注释

您还应该通过以下行检查是否启用了事务注释:

<tx:annotation-driven transaction-manager="transactionManager"/> 

在您的 .xml 配置中

于 2012-12-07T09:16:38.707 回答
7

试试这个:

em.getTransaction().begin();
em.persist(entity);
em.getTransaction().commit();

PS:你也应该为你的ID设置一个生成方法。

于 2012-12-07T14:50:23.070 回答
1

你能发布你得到什么例外吗?我会假设你的错误是你的persistence.xml 你没有指定你的“链”对象。

您可以使用此标签指定

<exclude-unlisted-classes>false</exclude-unlisted-classes>

要不就

 <class>your.package.Chain</class>

把这个放在提供者标签上面。

此外,永远不要为标记为 @Id 的列设置数字

当您使用带有设置值的 Id 列的方法 save() 时,hibernate 将尝试更新而不插入您的数据。

这样做:创建 getEntityManager 方法

public EntityManager getEntityManager() {
    return entityManager;
}

然后

@Transactional
public void saveChain(Chain chain) {

    chain.setDate(new Date());
    getEntityManager().persist(chain);
}
于 2012-12-07T09:10:41.393 回答
0

将这些添加到您的web.xml可能会解决此问题:

<!-- Open Entity Manager in View filter -->
<filter>
    <filter-name>openEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>openEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
于 2015-03-06T08:09:36.377 回答
0

解决了我的问题

org.springframework.transaction.jta.JtaTransactionManager

希望这项工作!

于 2018-01-05T20:41:49.943 回答
0

在 AppConfig 配置文件头中使用 @EnableTransactionManagement

于 2018-02-19T12:21:16.487 回答
0

您可以打开新的交易,然后提交您的记录。

@PersistenceUnit(unitName = "NameOfPersistenceUnit")
private EntityManagerFactory entityManagerFactory;

void someMethod(AccountCommodity accountCommodity){
     EntityManager entityManager = entityManagerFactory.createEntityManager();
     entityManager.getTransaction().begin();
     entityManager.persist(accountCommodity);
     entityManager.flush();
     entityManager.getTransaction().commit();
     entityManager.close();
}
于 2019-07-31T09:41:20.913 回答
-1

您将需要分配 id 生成策略,如下所示:

@Entity
@Table(name ="Chain")
public class Chain implements Serializable{

@Id
@Column(name = "id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
  private Long id;
@Column(name = "date")
  private Date date;
@Column(name = "name")
  private String name;
//setters and getters
}

并在保存方法中

  public int saveChain(Chain chain) {
        chain.setDate(new Date());
      //chain.setId((long)44);  remove this line
        Boolean a;
        em.persist(chain);

        return 222;
    }

如果您分配 Id,那么 hibernate/jpa 将尝试更新不可用的记录,而不是插入新记录,我认为不会抛出异常。

于 2012-12-07T09:09:51.723 回答
-1

我能够通过将实体添加到persistence.xml来解决同样的问题:

<persistence-unit name="OwnerPersistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.test.domain.local.Entity</class>
    <exclude-unlisted-classes />
    </persistence-unit>
</persistence>
于 2014-02-01T23:01:47.100 回答