这是我的实体类
@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的主键也会自动递增,那么什么是正确的解决方案???帮帮我!!!
答案 0 :(得分:0)
试试这样:
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 :(得分:-1)
以下是一些建议,并尝试这样做;
我确信您正在尝试将新对象实例持久保存到DB
//session.saveOrUpdate(hdr);
session.save(hdr);
覆盖实体类中的equals()
和hashCode()
正如@ pL4Gu33所说,mappedBy
在nay db update操作之前打开事务
session.getTransaction().begin();