2
我試圖在我的(Oracle)數據庫中插入Mother
對象的集合,這些對象的對象列表爲Child
。增加allocationSize會生成一個NonUniqueObjectException異常
@Entity
@Table(name = "MOTHER")
public class Mother implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="SEQ_MOTHER")
@SequenceGenerator(name="SEQ_MOTHER", allocationSize=100) //Error?!?
@Column(name = "ID")
private Long id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "mother",
orphanRemoval = true)
private List<Child> children;
}
我很簡單地插入感謝persist
方法幾個Mother
千實例:這些對象的所有ID都使用序列生成
for (Mother mother : mothersToInsert) {
entityManager.persist(mother);
}
在我的模型中,f我不設置任何allocationSize
(根據文檔的默認值是50)或將其設置爲小於50的值,那麼一切運行良好。
然而,如果我嘗試將其設置爲100個或更多,然後我一直有一個NonUniqueObjectException
:
javax.persistence.PersistenceException: org.hibernate.NonUniqueObjectException:
a different object with the same identifier value was already associated with
the session
A「解決方案」可能是改變Cascade
(我沒有嘗試去改變它),但是我想堅持堅持所有的母親情況下,當孩子實例(和檢索他們...)
我可能不需要將alocationSize
設置爲高值,但我怎麼能增加它以提高插入性能而不會發生異常?如何確保即使沒有設置錯誤,也不會隱藏在某處?