现在的位置: 首页 > 综合 > 正文

Hibernate Annotation @mappedBy含义在使用onetoone 时候【外键双向关联】(十三)

2013年07月18日 ⁄ 综合 ⁄ 共 3396字 ⁄ 字号 评论关闭

1

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

@Entity
public class Husband {
	private int id;
	private String name;
	private Wife wife;
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	
	public String getName() {
		return name;
	}
	@OneToOne
	@JoinColumn(name="wifeId")
	public Wife getWife() {
		return wife;
	}
	public void setId(int id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setWife(Wife wife) {
		this.wife = wife;
	}
	
}

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class Wife {
private int id;
private String name;
private Husband husband;
@OneToOne(mappedBy="wife")
public Husband getHusband() {
return husband;
}
public void setHusband(Husband husband) {
this.husband = husband;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}

通过双向关联,具体的 mapperby看下面的:

此处的 @OneToOne(mappedBy="wife")  wife 指的是 Hudson的变量wife,不能随便写的

1、关于mappedBy的含义
a) 只有OneToOne,OneToMany,ManyToMany上才有mappedBy属性,ManyToOne不存在该属性;
b) mappedBy标签一定是定义在the owned side(被拥有方的),他指向the owning side(拥有方);
c) mappedBy的含义,应该理解为,拥有方能够自动维护跟被拥有方的关系;
    当然,如果从被拥有方,通过手工强行来维护拥有方的关系也是可以做到的。
    例如:
Person.java 
@OneToOne(optional = true, cascade = CascadeType.ALL, mappedBy = "person")
 public IDCard getIdcard() {
  return idcard;
 }
 
IDCard.java
  @OneToOne(optional = false, cascade = CascadeType.ALL)
  @JoinColumn(name = "Person_ID", referencedColumnName = "personid",unique= true)
  public Person getPerson() {
  return person;
  }
 
OneToOneDAOBean.java
/*上面例子中IDCard为拥有方,这里强行使用Person作为主控方,手工维护两个实体的关系*/
    public void insertPerson(String name, boolean sex,short age, Date birthday,String cardID) {
        Person person = new Person();
        person.setName(name);
        person.setSex(sex);
        person.setAge(Short.valueOf(age));
        person.setBirthday(birthday);
        IDCard idcard = new IDCard(cardID);
        idcard.setPerson(person);
        person.setIdcard(idcard);
        em.persist(person);        
    }
 
/*其实既然IDCard为拥有方,那么将IDCard视为主控方的话,他自动维护了两个实体的关系*/
    public void insertPerson1(String name, boolean sex,short age, Date birthday,String cardID) {
        Person person = new Person();
        person.setName(name);
        person.setSex(sex);
        person.setAge(Short.valueOf(age));
        person.setBirthday(birthday);
        IDCard idcard = new IDCard(cardID);
        idcard.setPerson(person);
        //person.setIdcard(idcard);
        //em.persist(person);
        em.persist(idcard);
    }
d) mappedBy跟JoinColumn/JoinTable总是处于互斥的一方,可以理解为正是由于拥有方的关联被拥有方的字段存在,拥有方才拥有了被拥有方。mappedBy这方定义的JoinColumn/JoinTable总是失效的,不会建立对应的字段或者表;
e) 标准的关系映射,定义在ejb-3_0-fr-spec-persistence.pdf 的2.1.8 Relationship Mapping Defaults 章节中:
The following rules apply to bidirectional relationships:
• The inverse side of a bidirectional relationship must refer to its owning side by use of the
mappedBy element of the OneToOne, OneToMany, or ManyToMany annotation. The
mappedBy element designates the property or field in the entity that is the owner of the relationship.
• The many side of one-to-many / many-to-one bidirectional relationships must be the owning
side, hence the mappedBy element cannot be specified on the ManyToOne annotation.
• For one-to-one bidirectional relationships, the owning side corresponds to the side that contains
the corresponding foreign key.
• For many-to-many bidirectional relationships either side may be the owning side.
 

抱歉!评论已关闭.