我有一个 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
实体。这是来自值对象的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 为我的通知对象生成第一个插入,没问题:
insert
into
NOTIFICATION
(COD, DATE, ID_THEME, ID_TYP, IC_ARCH, ID_CLIENT, INFOS, NAME, TITRE_NOT, ID_NOT)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
然后,JPA 尝试在我的关联表中插入值,但pieceJointesGen 目前不存在:
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)
。
关于java - JPA ManyToMany 持续存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31534871/