第二十七弹,JPA如何使用注解实现一对多两端同时保存,并且查询同时查出
一端PO如下:
@Entity
@Table(name = "oc_accident",indexes = {@Index(name = "index_create_user",columnList = "create_user"),
@Index(name = "index_update_user",columnList = "update_user")})
@ApiModel("事故管理")
public class OcAccidentPO implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
@ApiModelProperty(value = "主键Id")
private Integer id;
@Basic
@Column(name = "create_time")
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@ApiModelProperty(value = "创建时间")
private Date createTime;
@Basic
@Column(name = "create_user")
@ApiModelProperty(value = "创建人")
private Integer createUser;
@Basic
@Column(name = "update_time")
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@ApiModelProperty(value = "修改时间")
private Date updateTime;
@Basic
@Column(name = "update_user")
@ApiModelProperty(value = "修改人")
private Integer updateUser;
@Valid
@OneToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE,CascadeType.REFRESH}
,orphanRemoval = true,mappedBy="ocAccidentPO",fetch=FetchType.EAGER)
private Set<OcAccidentDetailPO> ocAccidentDetailPOSet = new HashSet<OcAccidentDetailPO>();
gettingsetting省略。。。
多端PO如下:
@Entity
@Table(name = "oc_accident_detail")
@ApiModel("事故管理明细")
public class OcAccidentDetailPO implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
@ApiModelProperty(value = "主键Id")
private Integer id;
@Basic
@Column(name = "create_time")
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@ApiModelProperty(value = "创建时间")
private Date createTime;
@Basic
@Column(name = "create_user")
@ApiModelProperty(value = "创建人")
private Integer createUser;
@Basic
@Column(name = "update_time")
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@ApiModelProperty(value = "修改时间")
private Date updateTime;
@Basic
@Column(name = "update_user")
@ApiModelProperty(value = "修改人")
private Integer updateUser;
@ManyToOne(cascade={CascadeType.REFRESH},fetch = FetchType.LAZY)
@JoinColumn(name="accident_id")
private OcAccidentPO ocAccidentPO;
gettingsetting省略。。。
@JoinColumn(name=“accident_id”)注解代表会在多端数据库表自动生成accident_id字段
保存时如下:
@Override
@Transactional(rollbackFor = Exception.class)
public OcAccidentPO save(OcAccidentPO ocAccidentPO, Integer userId) throws Exception {
if(ocAccidentPO == null){
throw new Exception("要添加的数据不能为空");
}
if(ocAccidentPO.getOcAccidentDetailPOSet() != null && ocAccidentPO.getOcAccidentDetailPOSet().size() > 0){
for(OcAccidentDetailPO item : ocAccidentPO.getOcAccidentDetailPOSet()){
if(item.getId() != null){
item.setUpdateUser(userId);
item.setUpdateTime(Calendar.getInstance().getTime());
}else{
item.setCreateTime(Calendar.getInstance().getTime());
item.setCreateUser(userId);
item.setUpdateUser(userId);
item.setUpdateTime(Calendar.getInstance().getTime());
}
item.setOcAccidentPO(ocAccidentPO);
}
}
if(ocAccidentPO.getId() == null){
ocAccidentPO.setCreateTime(Calendar.getInstance().getTime());
ocAccidentPO.setCreateUser(userId);
ocAccidentPO.setUpdateUser(userId);
ocAccidentPO.setUpdateTime(Calendar.getInstance().getTime());
}else{
ocAccidentPO.setUpdateTime(Calendar.getInstance().getTime());
ocAccidentPO.setUpdateUser(userId);
}
return ocAccidentDao.save(ocAccidentPO);
}
需要注意的是,此功能为JPA级联保存修改删除,详情请参考jpa官网
|