ssh开发 hibernate 多对一,多方数据更新数据时还是插入一条新的数据

一兜兜 2014-01-25 11:00:32
/* EmrTBeds.java */
@Entity
@Table(name = "EMR_T_BEDS", schema = "DB2ADMIN")
@DynamicUpdate(true)
@DynamicInsert(true)
public class EmrTBeds implements java.io.Serializable {

private static final long serialVersionUID = 2150099574347070713L;
private String bedCode;
private EmrTHospital emrTHospital;
private String bedName;
private String deptCode;
private Date created;
private Date lastmod;
private Integer creater;
private Integer modifyer;
@Id
@Column(name = "BED_CODE", unique = true, nullable = false, length = 8)
public String getBedCode() {
return this.bedCode;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "HID", nullable = false)
public EmrTHospital getEmrTHospital() {
return this.emrTHospital;
}
}


/* EmrTHospital.java */
@Entity
@Table(name = "EMR_T_HOSPITAL", schema = "DB2ADMIN")
@DynamicUpdate(true)
@DynamicInsert(true)
public class EmrTHospital implements java.io.Serializable {

// Fields

private static final long serialVersionUID = 488889036261570315L;
private String hid;
private String hname;
private String myid;
private String hname2;
private String rate;
private String lev;
private String pid;
private String pname;
private Set<EmrTBeds> emrTBedses = new HashSet<EmrTBeds>(0);

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "emrTHospital")
public Set<EmrTBeds> getEmrTBedses() {
return this.emrTBedses;
}
}

/*bedService.java*/

public boolean saveOrUpdateBed(EmrTBeds bed,String hid) {
Integer userId=Constants.getCurrendUser().getUserId();
EmrTHospital h=new EmrTHospital();
h.setHid(hid);
if(bed.getBedCode()!=null||!"".equals(bed.getBedCode()))
{ //插入
bed.setEmrTHospital(h);
bed.setCreated(new Date());
bed.setCreater(userId);
bed.setLastmod(new Date());
bed.setModifyer(userId);
bedDao.save(bed);
}else
{ //更新
bed.setLastmod(new Date());
bed.setModifyer(userId);
//h.setHid(hid);
//bed.setEmrTHospital(h);
bed.getEmrTHospital().setHid(hid);
bedDao.update(bed);
}
return true;
}
...全文
177 3 打赏 收藏 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mich_LY 2014-01-26
  • 打赏
  • 举报
回复
不清楚问题是什么,不过hibernate有saveOrUpdate这个方法,主键不存在自动新建
乔不思 2014-01-26
  • 打赏
  • 举报
回复
没有看懂问题、
fw347969680 2014-01-26
  • 打赏
  • 举报
回复
没看懂。

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧