简体   繁体   English

使用休眠实体管理器更新实体

[英]update of entity with hibernate entity manager

I have one implementation where my entity Company extends RootEntity with 我有一个实施方案,其中我的实体Company扩展RootEntity

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

I have other entities which does not extends RootEntity . 我还有其他不扩展RootEntity实体。 When I create all entites it does create.But when I try to update an extended entity of RootEntity ,it does not update. 当我创建所有实体时,它确实会创建。但是当我尝试更新RootEntity的扩展实体时,它不会更新。 But the non extending entity is updating. 但是非扩展实体正在更新。 The update code is in GenericDaoImpl so it is the same update method. 更新代码在GenericDaoImpl因此它是相同的更新方法。

what can be the problem? 可能是什么问题? my code is: 我的代码是:

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

My Object That I am trying to merge: 我要合并的对象:

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;

      ......
}

and my RootEntity is: 而我的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..
   }

Newaz, I need to see your RootEntity and Company entity. Newaz,我需要查看您的RootEntityCompany实体。

Also, I was having hard time understanding your provided code though - 另外,我虽然很难理解您提供的代码-

  1. Why you need that "if" condition? 为什么需要这种“如果”条件? and what will happen if that if is not true? 如果不正确,将会发生什么?
  2. entityManager.flush(); you don't have to do this, if i understand correctly, you are using spring and @Transactional automatically closes/flushes transaction when invoked method is finished. 您不必执行此操作,如果我理解正确,则使用的是Spring,并且@Transactional在调用方法完成后会自动关闭/刷新事务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM