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.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

/**
 * User entity. @author MyEclipse Persistence Tools
 */

@Entity
@Table(name="user")
public class User implements java.io.Serializable {

	// Fields
	private static final long serialVersionUID = 8415887974331463494L;

	@Id
	@GeneratedValue(generator="incrementGenerator", strategy=GenerationType.IDENTITY)
	@GenericGenerator(name="incrementGenerator", strategy="increment")
	@Column(name="id")
	private Integer id;
	
	@Column(name="name")
	private String name;
	
	@Column(name="password")
	private String password;
	
	@OneToOne(targetEntity=Profile.class, fetch=FetchType.EAGER, cascade={CascadeType.ALL}, mappedBy="user")
	private Profile profile;

	// Constructors

	/** default constructor */
	public User() {
	}

	/** full constructor */
	public User(String name, String password) {
		this.name = name;
		this.password = password;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Profile getProfile() {
		return profile;
	}

	public void setProfile(Profile profile) {
		this.profile = profile;
	}
	
	

}
 

//Profile.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 {

	// Fields
	private 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 accessors

	public 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属性,看看出现的错误

猜你喜欢

转载自paladin1988.iteye.com/blog/1640340