我在實體圖中調用函數merge()作爲根元素。這些實體是級聯的。在圖中有持久和瞬時元素。我插入了一個空的額外實體。實體:Hibernate在merge()操作中插入空實體
@Entity
public class CnTermsAmortization {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
@ManyToOne
private CnTermsCounted cnTermsCounted;
@ManyToOne
private CnTermsBudget cnTermsBudget;
...
}
@Entity
public class CnTermsBudget {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
@OneToMany(mappedBy = "cnTermsBudget")
Set<CnTermsAmortization> amortizations = new HashSet<>();
@ManyToOne
private CnTermsGroup cnTermsGroup;
.....
}
@Entity
public class CnTermsCounted {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
@OneToMany(mappedBy = "cnTermsCounted", cascade=CascadeType.ALL, orphanRemoval=true)
private Set<CnTermsAmortization> amortizations = new HashSet<>();
@ManyToOne
private CnTermsGroup cnTermsGroup;
.....
}
@Entity
public class CnTermsGroup {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
@OneToMany(mappedBy="cnTermsGroup", cascade=CascadeType.ALL, orphanRemoval=true)
private List<CnTermsCounted> cnTermsCounteds = new ArrayList<>();
@OneToMany(mappedBy="cnTermsGroup", cascade=CascadeType.ALL, orphanRemoval=true)
private List<CnTermsBudget> cnTermsBudgets = new ArrayList<>();
.....
}
控制器:
public static void main(String[] args) {
// Insert data to database
EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa");
EntityManager em1 = emf.createEntityManager();
CnTermsBudget budget = new CnTermsBudget();
CnTermsCounted counted = new CnTermsCounted();
CnTermsGroup group = new CnTermsGroup();
group.getCnTermsBudgets().add(budget);
group.getCnTermsCounteds().add(counted);
budget.setCnTermsGroup(group);
counted.setCnTermsGroup(group);
EntityTransaction t1 = em1.getTransaction();
t1.begin();
Long id = em1.merge(group).getId();
t1.commit();
em1.close();
//Create transient entity
CnTermsAmortization amortization = new CnTermsAmortization();
EntityManager em = emf.createEntityManager();
EntityTransaction t = em.getTransaction();
t.begin();
//Read persistent graph
group = em.find(CnTermsGroup.class, id);
budget = group.getCnTermsBudgets().get(0);
counted = group.getCnTermsCounteds().get(0);
//Add transient entity to persistent tree
budget.getAmortizations().add(amortization);
counted.getAmortizations().add(amortization);
amortization.setCnTermsBudget(budget);
amortization.setCnTermsCounted(counted);
//merge on the root
em.merge(group);
t.commit();
em.close();
em = emf.createEntityManager();
List<?> ls = em.createQuery("from CnTermsAmortization").getResultList();
System.out.println(Arrays.toString(ls.toArray()));
em.close();
emf.close();
}
在Hibernate日誌,我看到兩個實體插入:
休眠:插入CnTermsAmortization(cnTermsBudget_id,cnTermsCounted_id,ID)值(,休眠:插入CnTermsAmortization(cnTermsBudget_id,cnTermsCounted_id,id)值(?,?,?)
在控制器輸出我看到它們中的一個在各個領域具有空:
[CnTermsAmortization [ID = 1,[email protected],[email protected]],CnTermsAmortization [ID = 2,cnTermsCounted = null,cnTermsBudget = null]]
當我調用所有實體都是Transient的圖形根的merge()時,輸出中有一個實體具有填充值。我使用Hibernate版本5.1.1,JPA接口。 我錯過了什麼嗎?