[英]JPA ManyToMany persist
我有一個NOTIFICATION
表,其中包含一個ManyToMany關聯:
@Entity
@Table(name="NOTIFICATION")
@NamedQuery(name="Notification.findAll", query="SELECT f FROM Notification f")
public class Notification {
/** SOME COLUMN DEFINITION NOT IMPORTANT FOR MY CASE
COD, DATE, ID_THEME, ID_TYP, IC_ARCH, ID_CLIENT, INFOS, NAME, TITRE_NOT, ID_NOT
**/
@ManyToMany
@JoinTable(
name="PJ_PAR_NOTIF"
, joinColumns={
@JoinColumn(name="ID_NOTIF")
}
, inverseJoinColumns={
@JoinColumn(name="ID_PJ_GEN")
}
)
private List<PiecesJointesGen> piecesJointesGens;
}
因此,我有一個名為PJ_PAR_NOTIF
的關聯表。
我嘗試保留一個Notification
實體。 這是來自Value Object的piecesJointesGens初始化:
@PersistenceContext(unitName="pu/middle")
private EntityManager entityMgr;
FoaNotification lFoaNotification = new FoaNotification();
for(PieceJointeGenVO lPJGenVO : pNotificationVO.getPiecesJointes()){
PiecesJointesGen lPiecesJointesGen = new PiecesJointesGen();
lPiecesJointesGen.setLienPjGen(lPJGenVO.getLienPieceJointeGen());
lPiecesJointesGen.setIdPjGen(lPJGenVO.getIdPieceJointeGen());
lNotification.getFoaPiecesJointesGens().add(lFoaPiecesJointesGen);
}
entityMgr.persist(pNotification);
持久不起作用。 JPA為我的Notification對象生成第一個插入,沒關系:
insert
into
NOTIFICATION
(COD, DATE, ID_THEME, ID_TYP, IC_ARCH, ID_CLIENT, INFOS, NAME, TITRE_NOT, ID_NOT)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
然后,JPA嘗試在我的關聯表中插入值,但是暫時不存在piecesJointesGen:
insert
into
PJ_PAR_NOTIF
(ID_NOTIF, ID_PJ_GEN)
values
(?, ?)
所以,我有這個錯誤:
GRAVE: EJB Exception: : java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.entities.PiecesJointesGen
有沒有辦法告訴JPA在PJ_PAR_NOTIF
插入之前插入piecesJointesGen
?
修改piecesJointesGens
映射到@ManyToMany(cascade = CascadeType.PERSIST)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.