在hibernate中保存父对象和子对象列表

时间:2014-05-29 07:53:36

标签: java hibernate hibernate-mapping

这是我的实体类

    @Entity
        @Table(name="hdr_info")
        public class HdrInfo implements Serializable {
            private static final long serialVersionUID = 1L;

            @Id
            @Column(name="hdr_sr_no")
            @GeneratedValue(strategy=GenerationType.IDENTITY)
            private Integer HdrSrNo;

            @Column(name="comp_cd")
            private Integer compCd;

            //bi-directional many-to-one association to EppsMmMtrlReqDtl
            @OneToMany(mappedBy="hdrInfo")
            @Cascade(CascadeType.SAVE_UPDATE)
            private List<DetailInfo> detailInfo;
            //getters and setters
        }

详情信息实体类

        @Entity
        @Table(name="detail_info")
        public class DetailInfo implements Serializable {
            private static final long serialVersionUID = 1L;

            @Id
            @GeneratedValue(strategy=GenerationType.IDENTITY)
            @Column(name="dtl_sr_no")
            private Integer dtlSrNo;

            @Column(name="active_yn")
            private String activeYn;

            @Column(name="short_close_qty")
            private BigDecimal shortCloseQty;

            @Column(name="short_close_rsn")
            private String shortCloseRsn;

            @Column(name="short_close_yn")
            private String shortCloseYn;

            @Column(name="sugg_brand")
            private String suggBrand;

            @Column(name="terminal_id")
            private String terminalId;

            @Column(name="transaction_stage")
            private Integer transactionStage;

            //bi-directional many-to-one association to EppsMmMtrlReqHdr
            @ManyToOne
            @JoinColumn(name="hdr_sr_no", nullable = false)
            private HdrInfo hdrInfo;

               //getters and setters
        }

主要

public static void main(String[] args) {
    Session session = HibernateUtil.getSessionFactory().openSession();

    HdrInfo hdr =new HdrInfo();
    hdr.setCompCd(1);
    List<DetailInfo> lst=new ArrayList<DetailInfo>();
    for (int j = 0; j < 3; j++) {                       
        DetailInfo dtl=new DetailInfo();

        dtl.setTransactionStage(Integer.parseInt("1"));
        dtl.setTerminalId("linuxPc01");
        dtl.setActiveYn("Y");

        dtl.setShortCloseYn("Y");

        dtl.setHdrInfo(hdr);
        lst.add(dtl);
    }
    hdr.setEppsMmMtrlReqDtls(lst);
    session.saveOrUpdate(hdr);
    session.getTransaction().commit();
}

现在,如果我运行主类来保存带有DtlInfoClass列表的HderInfo类,则会在保存DtlInfo列表的同时找到相同对象的标识符,现在DtlInfo的主键也会自动递增,那么什么是正确的解决方案???帮帮我!!!

2 个答案:

答案 0 :(得分:0)

试试这样:

HdrInfo

private List<DetailInfo> detailInfos = new ArrayList<DetailInfo>();

public void addDetailInfo(DetailInfo detailInfo) {
    detailInfo.setParent(this);
    detailInfos.add(detailInfo);
}

public void removeDetailInfo(DetailInfo detailInfo) {
    detailInfos.remove(detailInfo);
    if (detailInfo != null) {
        detailInfo.setParent(null);
    }
}

主要

Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
    tx = session.beginTransaction();

    HdrInfo hdr = new HdrInfo();
    hdr.setCompCd(1);
    for (int j = 0; j < 3; j++) {                       
        DetailInfo dtl=new DetailInfo();

        dtl.setTransactionStage(Integer.parseInt("1"));
        dtl.setTerminalId("linuxPc01");
        dtl.setActiveYn("Y");

        dtl.setShortCloseYn("Y");

        hdr.addDetailInfo(dtl);
    }
    session.saveOrUpdate(hdr);

    tx.commit();
}
catch (RuntimeException e) {
    if (tx != null) tx.rollback();
    throw e;
}
finally {
    sess.close();
}
  1. 您需要显式启动/提交/回滚事务。
  2. 始终close the session释放已分配的资源(数据库连接)
  3. 最好从头开始初始化所有子集合
  4. 在进行双向关联时,首选添加添加/删除实用程序

答案 1 :(得分:-1)

以下是一些建议,并尝试这样做;

  1. 我确信您正在尝试将新对象实例持久保存到DB

    //session.saveOrUpdate(hdr);
    session.save(hdr);
    
  2. 覆盖实体类中的equals()hashCode()

  3. 正如@ pL4Gu33所说,mappedBy

  4. 的关注拼写
  5. 在nay db update操作之前打开事务

    session.getTransaction().begin();