在EclipseLink中,我遇到了一個插入元素兩次的問題,導致主鍵違規。場景如下: 我有三個實體,Element,Restriction和RestrictionElement。實體RestrictionElement充當另外兩個之間的多對多關係。 當我創建一個新的RestrictionElement併合並元素時,RestrictionElement被插入兩次。代碼:爲什麼JPA在合併時執行雙重插入()
// element is an Element, restriction is a Restriction. Both are already in present in the database.
RestrictionElement newRestrictionElement = new RestrictionElement(restriction, element);
Transaction transaction = new Transaction();
em.merge(element); //em is the EntityManager
transaction.commit();
但是,如果我刪除行restriction.getReferencedRestrictionElements().add(this);
的RestrictionElement插入一次。 任何人都可以解釋爲什麼會發生這種情況?或者指向一個解釋如何計算merge()命令的文檔?
相關JPA代碼:(我只給一小部分有不與代碼的任何其他大的問題。)
public class RestrictionElement {
@JoinColumns({@JoinColumn(name = "ELEMENT_ID", referencedColumnName = "ID"),@JoinColumn(name = "ELEMENT_DESCRIPTOR", referencedColumnName = "DESCRIPTOR")})
private Element element;
@JoinColumns({@JoinColumn(name = "RESTRICTION_ID", referencedColumnName = "ID"),@JoinColumn(name = "RESTRICTION_DESCRIPTOR", referencedColumnName = "DESCRIPTOR")})
private Restriction restriction;
public RestrictionElement(Restriction restriction, Element element) {
this.restriction = restriction;
this.element = element;
restriction.getReferencedRestrictionElements().add(this);
element.getReferingRestrictionElements().add(this);
}
}
public class Element {
@OneToMany(mappedBy = "element")
private List<RestrictionElement> referingRestrictionElements = new ArrayList<RestrictionElement>();
}
public class Restriction extends Element {
@OneToMany(mappedBy = "restriction", cascade = { ALL, PERSIST, MERGE, REMOVE, REFRESH })
private List<RestrictionElement> referencedRestrictionElements = new ArrayList<RestrictionElement>();
}
在您的RestrictionElement中的代碼中,我看不到您聲明瞭關係的性質(即@OneToOne,@ManyToOne等)。我在這裏錯過了什麼嗎? – 2011-05-18 16:56:34
真實的代碼並不包含它,並且大多數情況下,它的工作原理很明顯並不是必需的。 – bspoel 2011-05-20 10:04:12
這不是一個趣味問題,這是[JPA規範](http://www.jcp.org/en/jsr/detail?id=317)的一部分。此外,在這些註釋中,您可以定義給定關聯的合併行爲。您甚至可以將OneToMany映射到二元關聯另一側的不存在的關係。如果我是你,我會先解決這個問題,然後看看問題是否存在,但在實體沒有正確註釋之前,沒有人能確定事情爲什麼沒有按預期行事。這裏的問題是爲什麼你的代碼不起作用,所以你不應該低估這一點。 – 2011-05-20 15:00:44