我有一个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为Notification生成第一个insert,这是可以的:
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在piecesJointesGen插入之前插入PJ_PAR_NOTIF吗?
发布于 2015-07-21 09:39:17
将piecesJointesGens映射修改为@ManyToMany(cascade = CascadeType.PERSIST)。
https://stackoverflow.com/questions/31534871
复制相似问题