【问题标题】:update of entity with hibernate entity manager使用休眠实体管理器更新实体
【发布时间】:2011-06-24 15:20:49
【问题描述】:

我有一个实现,其中我的实体 Company 使用

扩展 RootEntity
strategy =InheritanceType.JOINED.(I am using hibernate-entitymanager) 

我还有其他不扩展 RootEntity 的实体。当我创建所有实体时,它确实会创建。但是当我尝试更新RootEntity 的扩展实体时,它不会更新。但非扩展实体正在更新。更新代码在GenericDaoImpl,所以更新方法是一样的。

可能是什么问题?我的代码是:

@Transactional(value="tx")
public E update(E entity) {
    E merged = null;
    RootEntity baseEntity = null;
    if(entity instanceof RootEntity ){
        baseEntity = (RootEntity)entity;
        merged = entityManager.find(entityClass, baseEntity.getId());
    }
    merged = entityManager.merge(entity);
    entityManager.flush();
    return merged;
}

我要合并的对象:

public class Societa extends RootEntity implements Serializable{

     private static final long serialVersionUID = 1L;

     // ASSOCIAZIONE CAMPI DB VARIABILI NELLA CLASSE
     @Column(name="nomeSocieta",nullable = false,columnDefinition="CHAR(80)")
     private String nomeSocieta;

      ......
}

我的 RootEntity 是:

@Entity
@Inheritance(strategy =InheritanceType.JOINED)
    public class RootEntity {

       private static final long serialVersionUID = 1L;

       @Id
       @Column(name="id",nullable = false)
       @GeneratedValue(strategy = GenerationType.IDENTITY)
       private Long id;
        ...getters/setters..
   }

【问题讨论】:

  • 你能举一个你试图合并但不起作用的对象的例子吗?您是否收到错误消息,或者您只是在数据库中看不到任何更改?
  • 嗨,我添加了一些代码示例,它没有给出任何错误,只是它没有更新
  • 别这么说,告诉我们你是怎么解决的。这个论坛是为了帮助所有人,而不仅仅是你

标签: java database hibernate jpa


【解决方案1】:

Newaz,我需要看看你的 RootEntityCompany 实体。

另外,虽然我很难理解您提供的代码 -

  1. 为什么需要“如果”条件?如果这不是真的会发生什么?
  2. entityManager.flush(); 您不必这样做,如果我理解正确,您正在使用 spring,@Transactional 在调用方法完成时自动关闭/刷新事务。

【讨论】:

    猜你喜欢
    • 2019-06-22
    • 2013-07-24
    • 2016-12-20
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 2020-02-16
    • 2016-08-20
    相关资源
    最近更新 更多