[英]Save Parent Object and list of child object Same Time in hibernate
This is my entity classes 这是我的实体课
@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
}
Detail Info Entity class 详细信息实体类
@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
}
Main 主要
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();
}
now if i run main class to save HderInfo class with list of DtlInfoClass gives me error of same object Identifier was found while saving List of DtlInfo,Now primary key of DtlInfo is also auto incremented then what is proper solution ??? 现在,如果我运行主类来保存带有DtlInfoClass列表的HderInfo类,则会给我同一对象标识符的错误,同时保存DtlInfo列表时发现了,现在DtlInfo的主键也会自动递增,那么正确的解决方案是什么? help me !!! 帮我 !!!
Try it like this: 像这样尝试:
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();
}
Here are some suggestions and try like that; 这里有一些建议,可以这样尝试;
I am sure that you are trying to persist as new object instances to DB 我确定您正在尝试将新对象实例持久保存到数据库
//session.saveOrUpdate(hdr); session.save(hdr);
Override equals()
and hashCode()
in entity classes 在实体类中重写equals()
和hashCode()
as @pL4Gu33 said, care spelling of mappedBy
如@ pL4Gu33说,关心的拼写mappedBy
Open transaction before nay db update operation 否db更新操作之前打开事务
session.getTransaction().begin();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.