[英]JPA ManyToMany persist
I have a NOTIFICATION
table who contains one ManyToMany association : 我有一个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;
}
So, I have an association table called PJ_PAR_NOTIF
. 因此,我有一个名为PJ_PAR_NOTIF
的关联表。
I try to persist a Notification
entity. 我尝试保留一个Notification
实体。 Here is piecesJointesGens initialisation, from a Value Object : 这是来自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);
The persist doesn't work. 持久不起作用。 JPA generate a first insert for my Notification object, that is ok : JPA为我的Notification对象生成第一个插入,没关系:
insert
into
NOTIFICATION
(COD, DATE, ID_THEME, ID_TYP, IC_ARCH, ID_CLIENT, INFOS, NAME, TITRE_NOT, ID_NOT)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Then, JPA try to insert values in my association table, but piecesJointesGen doesn't exists for the moment : 然后,JPA尝试在我的关联表中插入值,但是暂时不存在piecesJointesGen:
insert
into
PJ_PAR_NOTIF
(ID_NOTIF, ID_PJ_GEN)
values
(?, ?)
So, I have this error : 所以,我有这个错误:
GRAVE: EJB Exception: : java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.entities.PiecesJointesGen
Is there a way to tell JPA to insert piecesJointesGen
before the PJ_PAR_NOTIF
insert ? 有没有办法告诉JPA在PJ_PAR_NOTIF
插入之前插入piecesJointesGen
?
修改piecesJointesGens
映射到@ManyToMany(cascade = CascadeType.PERSIST)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.