JPA ManyToMany坚持 [英] JPA ManyToMany persist

查看:164
本文介绍了JPA ManyToMany坚持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NOTIFICATION 表,它包含一个ManyToMany关联:

  @Entity 
@Table(name =NOTIFICATION)
@NamedQuery(name =Notification.findAll,query =SELECT f FROM Notification f)
public class Notification {

**某些列定义对于我的个案不重要
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)
}

私人列表< PiecesJointesGen> piecesJointesGens;



$ b $ p
$ b

所以,我有一个关联表,名为 PJ_PAR_NOTIF



我尝试坚持一个通知实体。这里是来自Value Object的piecesJointesGens初始化:

  @PersistenceContext(unitName =pu / middle)

private EntityManager entityMgr;

FoaNotification lFoaNotification = new FoaNotification();

(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
(COD,DATE,ID_THEME,ID_TYP,IC_ARCH,ID_CLIENT,INFOS,NAME,TITRE_NOT,ID_NOT)

(?,?,?,?,?,?,?,?然后,JPA尝试在我的关联表中插入值,但片断jointesGen不会暂时存在:

 插入

中PJ_PAR_NOTIF
(ID_NOTIF,ID_PJ_GEN)

(?,?)

所以,我有这个错误:

  GRAVE:EJB异常:: java.lang.IllegalStateException:org.hibernate.TransientObjectException:对象引用未保存的瞬态实例 - 保存在冲洗前的瞬间实例:com.entities.PiecesJointesGen 

有没有办法告诉JPA插入 piecesJointesGen PJ_PAR_NOTIF 之前ert?

解决方案

修改 piecesJointesGens 映射到 @ManyToMany(cascade = CascadeType.PERSIST)

I have a NOTIFICATION table who contains one ManyToMany association :

@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.

I try to persist a Notification entity. Here is piecesJointesGens initialisation, from a Value Object :

@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 :

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 :

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 ?

解决方案

Modify piecesJointesGens mapping to @ManyToMany(cascade = CascadeType.PERSIST).

这篇关于JPA ManyToMany坚持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆