簡體   English   中英

使用休眠實體管理器更新實體

[英]update of entity with hibernate entity manager

我有一個實施方案,其中我的實體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..
   }

Newaz,我需要查看您的RootEntityCompany實體。

另外,我雖然很難理解您提供的代碼-

  1. 為什么需要這種“如果”條件? 如果不正確,將會發生什么?
  2. entityManager.flush(); 您不必執行此操作,如果我理解正確,則使用的是Spring,並且@Transactional在調用方法完成后會自動關閉/刷新事務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM