两张表的关系如下:
对应的实体
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