【问题标题】:Hibernate: EntityManager.merge does not insert any dataHibernate:EntityManager.merge 不插入任何数据
【发布时间】:2013-09-08 11:36:07
【问题描述】:

我在休眠时遇到了一些问题。 EntityManager.merge() 没有任何效果,persist() 也没有。

我的代码:

@Stateless
public class UserManager {

@Inject
private EntityManager em;

public User save(User user){
    return em.merge(user);
}
}

User 实体非常简单:

@Entity
public class User implements Serializable {

private static final long serialVersionUID = -6249050000984099316L;

@Id
@GeneratedValue
private Long id;

private String name;

//some more fields
//...

// getters / setters following
//...
}

我的 ManagedBean 只是调用 userManager.save(user) 并传递一个用户对象。

我的数据库中的用户表是完全空的,所以我希望merge() 产生一个 INSERT 语句。 表用户既没有插入也没有更新。 仅更新生成 ID 的 next_val:

INFO  [stdout] (http-localhost-127.0.0.1-8082-7) Hibernate: select next_val as id_val from hibernate_sequence for update
INFO  [stdout] (http-localhost-127.0.0.1-8082-7) Hibernate: update hibernate_sequence set next_val= ? where next_val=?

请帮助我。提前致谢。

编辑:

EntityManager 生产者:

public class Resources {

   @PersistenceContext
   private EntityManager em;

   @Produces
   public EntityManager produceEntityManager()
   {
      return em;
   }
}

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    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">
    <persistence-unit name="primary" transaction-type="RESOURCE_LOCAL">
        <jta-data-source>java:jboss/datasources/dkmgr</jta-data-source>
        <properties>
            <!-- Properties for Hibernate -->
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.import_files_sql_extractor"
                value="org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor" />
            <property name="hibernate.hbm2ddl.import_files" value="import.sql" />
        </properties>
    </persistence-unit>
</persistence>

【问题讨论】:

    标签: hibernate jakarta-ee merge persistence entitymanager


    【解决方案1】:

    如果你使用 EJB,也许你应该试试这个:

    public User save(User user){
        em.getTransaction().begin();
        em.merge(user);
        em.getTransaction().commit();
        return user;
    }
    

    【讨论】:

    • 谢谢,但我使用的是 EJB,所以没有 @Transactional 注释:-/'粘贴你的应用上下文'是什么意思?
    • 我改变了答案。通过粘贴你的 app-context.xml 我的意思是配置文件 entityManagerFactory itp
    • begin() / commit() 有效!感谢那。是否有任何解决方法,因此我的应用程序中的每个合并操作都不需要此 begin()/commit()?我在上面的问题中添加了我的 persistence.xml 和 EM-Producer。但是我没有任何app-context.xml,我应该担心吗?
    • 对了,为什么我需要手动开始事务,因为我的 EntityManager 是容器管理的?
    • 我不知道。我正在使用 Spring,persist() 和 merge() 经常出现问题是由于缺少 @Transactional 注释引起的。有时是因为错误的 用法。我对EJB不是很了解,也许你得为persist()、merge()等常见的数据库操作创建一些超类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 2013-12-28
    • 1970-01-01
    相关资源
    最近更新 更多