博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hibernate中OneToOne的级联保存
阅读量:6405 次
发布时间:2019-06-23

本文共 7600 字,大约阅读时间需要 25 分钟。

hot3.png

两张表的关系如下:

092039_SntO_914897.png

对应的实体

package com.jason.logistics.entity.member;import com.jason.logistics.entity.BaseEntity;import com.jason.logistics.entity.express.ExprDelCom;import java.util.Date;import javax.persistence.CascadeType;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.JoinColumn;import javax.persistence.ManyToOne;import javax.persistence.OneToOne;import javax.persistence.Table;import javax.persistence.Temporal;import javax.persistence.TemporalType;import javax.persistence.Transient;/** * MemberExpress. */@Entity@Table(name="logistics_member_express")public class MemberExpress extends BaseEntity {     private String userName;     private String password;     private String fullName;     private String gender;     private Long msisdn;     private Date dateOfBirth;     private String address;     private Short userType;     private String photo;     private String nickName;     private Short age;     private String idCard;         private ExprDelCom exprDelCom;//所属快递公司     private ExpressUser expressUser;        @Column(name="user_name", length=64)    public String getUserName() {        return this.userName;    }        public void setUserName(String userName) {        this.userName = userName;    }                @Column(name="password", length=32)    public String getPassword() {        return this.password;    }        public void setPassword(String password) {        this.password = password;    }                @Column(name="full_name", length=64)    public String getFullName() {        return this.fullName;    }        public void setFullName(String fullName) {        this.fullName = fullName;    }                @Column(name="gender", length=8)    public String getGender() {        return this.gender;    }        public void setGender(String gender) {        this.gender = gender;    }                @Column(name="msisdn", precision=11, scale=0)    public Long getMsisdn() {        return this.msisdn;    }        public void setMsisdn(Long msisdn) {        this.msisdn = msisdn;    }            @Temporal(TemporalType.TIMESTAMP)    @Column(name="date_of_birth", length=19)    public Date getDateOfBirth() {        return this.dateOfBirth;    }        public void setDateOfBirth(Date dateOfBirth) {        this.dateOfBirth = dateOfBirth;    }                @Column(name="address", length=128)    public String getAddress() {        return this.address;    }        public void setAddress(String address) {        this.address = address;    }        @OneToOne(fetch = FetchType.LAZY)	@JoinColumn(name="expr_del_com_id",nullable=true, referencedColumnName="id", unique=true, updatable=true)	public ExprDelCom getExprDelCom() {		return exprDelCom;	}	public void setExprDelCom(ExprDelCom exprDelCom) {		this.exprDelCom = exprDelCom;	}                @Column(name="user_type", precision=1, scale=0)    public Short getUserType() {        return this.userType;    }        public void setUserType(Short userType) {        this.userType = userType;    }                @Column(name="photo", length=128)    public String getPhoto() {        return this.photo;    }        public void setPhoto(String photo) {        this.photo = photo;    }                @Column(name="nick_name", length=64)    public String getNickName() {        return this.nickName;    }        public void setNickName(String nickName) {        this.nickName = nickName;    }                @Column(name="age", precision=3, scale=0)    public Short getAge() {        return this.age;    }        public void setAge(Short age) {        this.age = age;    }                @Column(name="id_card", length=18)    public String getIdCard() {        return this.idCard;    }        public void setIdCard(String idCard) {        this.idCard = idCard;    }        //@OneToOne(cascade={CascadeType.MERGE,CascadeType.REMOVE,CascadeType.REFRESH,CascadeType.PERSIST})    @OneToOne(cascade={CascadeType.ALL})    @JoinColumn(name="express_user_id", referencedColumnName="id", unique=true, nullable=true, updatable=true)	public ExpressUser getExpressUser() {		return expressUser;	}	public void setExpressUser(ExpressUser expressUser) {		this.expressUser = expressUser;	}	@Override	@Transient	public void onSave() {			// TODO Auto-generated method stub			}	@Override	@Transient	public void onUpdate() {				// TODO Auto-generated method stub			}}package com.jason.logistics.entity.member;import com.jason.logistics.entity.BaseEntity;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.Table;import javax.persistence.Transient;/** * ExpressUser. */@Entity@Table(name="logistics_express_user")public class ExpressUser extends BaseEntity {     private String kuaidiNumber;     private String sincerity;     private String deliverGoods;     private Short goodRate;     private String companyName;               @Column(name="kuaidi_number", length=64)    public String getKuaidiNumber() {        return this.kuaidiNumber;    }        public void setKuaidiNumber(String kuaidiNumber) {        this.kuaidiNumber = kuaidiNumber;    }                @Column(name="sincerity", length=32)    public String getSincerity() {        return this.sincerity;    }        public void setSincerity(String sincerity) {        this.sincerity = sincerity;    }                @Column(name="deliver_goods", length=2560)    public String getDeliverGoods() {        return this.deliverGoods;    }        public void setDeliverGoods(String deliverGoods) {        this.deliverGoods = deliverGoods;    }                @Column(name="good_rate", precision=3, scale=0)    public Short getGoodRate() {        return this.goodRate;    }        public void setGoodRate(Short goodRate) {        this.goodRate = goodRate;    }                @Column(name="company_name", length=64)    public String getCompanyName() {        return this.companyName;    }        public void setCompanyName(String companyName) {        this.companyName = companyName;    }    		@Override	@Transient	public void onSave() {			// TODO Auto-generated method stub			}	@Override	@Transient	public void onUpdate() {				// TODO Auto-generated method stub			}}

其中两个实体的对应关系如下:

 //@OneToOne(cascade={CascadeType.MERGE,CascadeType.REMOVE,CascadeType.REFRESH,CascadeType.PERSIST})    @OneToOne(cascade={CascadeType.ALL})    @JoinColumn(name="express_user_id", referencedColumnName="id", unique=true, nullable=true, updatable=true)	public ExpressUser getExpressUser() {		return expressUser;	}

经测试发现,只有在设置

cascade={CascadeType.ALL}

的情况下才能在新增MemberExpress情况下一起新增ExpressUser(如果ExpressUser的id为null),其它情况如设置

@OneToOne(cascade={CascadeType.MERGE,CascadeType.REMOVE,CascadeType.REFRESH,CascadeType.PERSIST})

会报以下异常:

ERROR [com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor] - object references an unsaved transient instance - save the transient instance before flushing: com.jason.logistics.entity.member.ExpressUser; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.jason.logistics.entity.member.ExpressUserorg.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: com.jason.logistics.entity.member.ExpressUser; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.jason.logistics.entity.member.ExpressUser

转载于:https://my.oschina.net/u/914897/blog/374655

你可能感兴趣的文章
self.navigationController退出到指定页面,或者一次性pop出n个页面
查看>>
Quartz实现数据库动态配置定时任务
查看>>
iptables 端口转发以及双向通信
查看>>
备战一线互联网公司Java工程师面试题 (1)
查看>>
ThinkPHP中自动验证失败
查看>>
jquery图片切换插件jquery.cycle.js参数详解
查看>>
JavaScript push() 方法
查看>>
Map集合
查看>>
JSP基础语法1
查看>>
elasticsearch Java API 之GET API & DELETE API
查看>>
《深入理解Java虚拟机》——GC基础概念
查看>>
微信小程序联盟:官方文档+精品教程+demo集合(5月31日更新,持续更新中……)...
查看>>
Fastjson 的 Set类型和 WriteClassName 选项引起的BUG
查看>>
翻译: 星球生成 II
查看>>
IOS 多线程
查看>>
python序列化数据本地存放
查看>>
#CCNA#IP地址、子网划分参考资料网址
查看>>
比较不错的图片上传插件
查看>>
判偶不判奇
查看>>
Sequelize 数据库的支持
查看>>