Hibernate Annotation 唯一外键一对一双向关联
继续上面的东西,修改下数据库脚本,把共享主键中的Profile的外键关系单独出来唯一列。
?
数据库脚本(附件)
?
?
//User.java
package com.zyp.examples;import javax.persistence.CascadeType;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.OneToOne;import javax.persistence.Table;import org.hibernate.annotations.GenericGenerator;/** * Profile entity. @author MyEclipse Persistence Tools */@Entity@Table(name="profile")public class Profile implements java.io.Serializable {// Fieldsprivate static final long serialVersionUID = -804808825096862895L;@Id@GeneratedValue(generator="incrementGenerator", strategy=GenerationType.IDENTITY)@GenericGenerator(name="incrementGenerator", strategy="increment")@Column(name="id")private Integer id;@Column(name="email")private String email;@Column(name="address")private String address;@OneToOne(cascade={CascadeType.ALL},fetch=FetchType.EAGER)@JoinColumn(name="pid", referencedColumnName="id", unique=true)private User user;// Constructors/** default constructor */public Profile() {}/** minimal constructor */public Profile(String email, String address) {this.email = email;this.address = address;}// Property accessorspublic Integer getId() {return this.id;}public void setId(Integer id) {this.id = id;}public String getEmail() {return this.email;}public void setEmail(String email) {this.email = email;}public String getAddress() {return this.address;}public void setAddress(String address) {this.address = address;}public User getUser() {return user;}public void setUser(User user) {this.user = user;}}
?
?
测试类和之前的基于共享主键的测试类是一样的。
真是没明白mappedBy这个属性的含义,还得好好把那些属性的中文含义搞懂。
大家可以交换两个实体类的mappedBy属性,看看出现的错误