5. 级联持久化操作
主表DO从表对应的DO
- package com.alibaba.corp.metrix.admin.biz.edi.model.domain.taobao;
- import java.io.Serializable;
- import java.util.Date;
- import java.util.List;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.OneToMany;
- import javax.persistence.Table;
- import javax.persistence.Transient;
- import org.hibernate.annotations.GenericGenerator;
- @Entity(name = "IfreeOrder")
- @Table(name = "metrix_ifree_order")
- public class IfreeOrder implements Serializable {
- @Transient
- private static final long serialVersionUID = -3466339962287852603L;
- @Id
- @GenericGenerator(name = "generator", strategy = "assigned")
- @GeneratedValue(generator = "generator")
- @Column(name = "id", unique = true, nullable = false)
- private String Id; // 发布id
- @Column(name = "subject")
- private String Subject; // 发布标题
- @Column(name = "one_product_line")
- private String OneProductLine; // 一级产品线
- @Column(name = "two_product_line")
- private String TwoProductLine; // 二级产品线
- @Column(name = "team")
- private String Team; // 团队
- @Column(name = "applies")
- private String Applys; // 应用,如果有多个,中间用英文逗号隔开
- @Column(name = "pm")
- private String PM; // PM
- @Column(name = "pe")
- private String PE; // PE
- @Column(name = "qa")
- private String QA; // QA
- @OneToMany(mappedBy = "ifreeOrder", cascade = CascadeType.ALL)
- private List<IfreeOrderRelatedProject> ReleateProject; // 相关项目
- @Column(name = "gmt_created")
- private Date gmtCreated = new Date(); // 本地数据库创建时间
- public String getId() {
- return Id;
- }
- public void setId(String Id) {
- this.Id = Id;
- }
- public String getSubject() {
- return Subject;
- }
- public void setSubject(String Subject) {
- this.Subject = Subject;
- }
- public String getOneProductLine() {
- return OneProductLine;
- }
- public void setOneProductLine(String OneProductLine) {
- this.OneProductLine = OneProductLine;
- }
- public String getTwoProductLine() {
- return TwoProductLine;
- }
- public void setTwoProductLine(String TwoProductLine) {
- this.TwoProductLine = TwoProductLine;
- }
- public String getTeam() {
- return Team;
- }
- public void setTeam(String Team) {
- this.Team = Team;
- }
- public String getApplys() {
- return Applys;
- }
- public void setApplys(String Applys) {
- this.Applys = Applys;
- }
- public String getPM() {
- return PM;
- }
- public void setPM(String PM) {
- this.PM = PM;
- }
- public String getPE() {
- return PE;
- }
- public void setPE(String PE) {
- this.PE = PE;
- }
- public String getQA() {
- return QA;
- }
- public void setQA(String QA) {
- this.QA = QA;
- }
- public List<IfreeOrderRelatedProject> getReleateProject() {
- return ReleateProject;
- }
- public void setReleateProject(List<IfreeOrderRelatedProject> ReleateProject) {
- this.ReleateProject = ReleateProject;
- if (this.ReleateProject != null) {
- for (IfreeOrderRelatedProject ifreeOrderRelatedProject : this.ReleateProject) {
- ifreeOrderRelatedProject.setIfreeOrder(this);
- }
- }
- }
- public Date getGmtCreated() {
- return gmtCreated;
- }
- public void setGmtCreated(Date gmtCreated) {
- this.gmtCreated = gmtCreated;
- }
- }
说明: 1. 上面DO的field名称不规范,是因为接入我们系统序列化后的Json字符串不规范,为了能兼容其它的系统,使用了这种非常恶心的写法。
- package com.alibaba.corp.metrix.admin.biz.edi.model.domain.taobao;
- import java.io.Serializable;
- import java.util.Date;
- import javax.persistence.*;
- import org.hibernate.annotations.GenericGenerator;
- @Entity
- @Table(name = "ifree_order_related_project")
- public class IfreeOrderRelatedProject implements Serializable {
- @Transient
- private static final long serialVersionUID = -4783733149722014928L;
- @Id
- @GenericGenerator(name = "generator", strategy = "increment")
- @GeneratedValue(generator = "generator")
- @Column(name = "inner_id", unique = true, nullable = false)
- private Integer index;
- @Column(name = "id")
- private String Id;
- @Column(name = "project_id")
- private String projectId; // 项目Id
- @Column(name = "publish_id")
- private String PublishId; // 发布Id
- @Column(name = "is_last")
- private String IsLast; // 是否是最后一次发布
- @Column(name = "publish_num")
- private String PublishNum; // 发布次数
- @ManyToOne
- @JoinColumn(name = "order_id") //从表的外键,会默认关联主表的主键,不需要字段名相同
- private IfreeOrder ifreeOrder;
- @Column(name = "gmt_created")
- private Date gmtCreated = new Date(); // 本地数据库创建时间
- public Integer getIndex() {
- return index;
- }
- public void setIndex(Integer index) {
- this.index = index;
- }
- public String getId() {
- return Id;
- }
- public void setId(String Id) {
- this.Id = Id;
- }
- public String getProjectId() {
- return projectId;
- }
- public void setProjectId(String projectId) {
- this.projectId = projectId;
- }
- public String getPublishId() {
- return PublishId;
- }
- public void setPublishId(String PublishId) {
- this.PublishId = PublishId;
- }
- public String isIsLast() {
- return IsLast;
- }
- public void setIsLast(String IsLast) {
- this.IsLast = IsLast;
- }
- public String getPublishNum() {
- return PublishNum;
- }
- public void setPublishNum(String PublishNum) {
- this.PublishNum = PublishNum;
- }
- public IfreeOrder getIfreeOrder() {
- return ifreeOrder;
- }
- public void setIfreeOrder(IfreeOrder ifreeOrder) {
- this.ifreeOrder = ifreeOrder;
- }
- public Date getGmtCreated() {
- return gmtCreated;
- }
- public void setGmtCreated(Date gmtCreated) {
- this.gmtCreated = gmtCreated;
- }
- }
2. 上面的DO中我们只使用到了单向关联,主表的CRUD会级联到从表
3. 注意主表、从表中@OneToMany及@ManyToOne标签的写法
4. 进行级联保存时,调用主表对应Dao进行save或者saveOrUpdate操作时,可能会抛出update操作,但数据库中row size = 0的异常,这是因为hibernate根据你是否设置了主键ID来判断你进行的是新增或者更新操作,如果你需要持久化的对象主键已经有值了,此时会默认进行update操作,但数据库中此主键还未对应任何数据,因此会抛出异常
本文出自 “黑白灰” 博客,请务必保留此出处http://leonmau.blog.51cto.com/2202260/901125