Hibernate:一对一双向主键关联

参考资料:

Hibernate系列:映射一对一主键双向关联

http://blog.csdn.net/igoder/archive/2009/06/23/4292166.aspx

Hibernate一对一主键双向关联映射(Annotation配置)

http://www.blogjava.net/sxyx2008/archive/2010/10/14/335093.html

Hibernate一对一主键双向关联

一环境:W7+Eclipse3.6+JDK1.6+MySQL

二所用Hibernate版本:hibernate-distribution-3.6.0

三具体代码

hibernate一对一双向指的是2个对象都可以互相的调用,双向的配置可以方便我们调用对象。比如知道A对象就可以使用B对象,可以调用B对象就可以使用A对象。

Hibernate映射一对一双向关联总结:只需对有外键一方的xml文件设置constrained="true"即可。

若操作涉及到映射关系双方的数据,以操作没有外键一方的对象为主(因为有级联);

若操作只涉及到有外键的一方,则只操作这一方对象即可。

1Annotations版本

主导对象

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

@Entity
public class A {
	private int id;
	private String name;
	private B b;

	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	
	public String getName() {
		return name;
	}

	@OneToOne
	@PrimaryKeyJoinColumn
      	public B getB() {
		return b;
	}
	public void setId(int id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setB(B b) {
		this.b = b;
	}
	
}

被参考对象

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

@Entity
public class B{
	private int id;
	private String name;

	private A a ;
	
	@OneToOne
	@PrimaryKeyJoinColumn
	public A getA() {
		return a;
	}


	public void setA(A a) {
		this.a = a;
	}

	

	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	
	public String getName() {
		return name;
	}

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

XML版本

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
	 <class name="org.hibernate.model.A">
        <id name="id" column="id">
           <!-- <generator class="native"/> -->
<!-- class="foreign": 一对一主键映射中,使用另外一个相关联的对象的标识符 -->
            <generator class="foreign">   
                <param name="property">b</param>   
           </generator>   

        </id>
        <property name="name"/>
<!-- 表示在B表存在一个外键约束,外键参考相关联的表person -->
	<one-to-one name="b" constrained="true"></one-to-one>
    </class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
	 <class name="org.hibernate.model.B">
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        <property name="name"/>	   
	<one-to-one name="a" property-ref="b"></one-to-one>
    </class>
</hibernate-mapping>

SQL

create table A (
        id integer not null auto_increment,
        name varchar(255),
        bId integer unique,
        primary key (id)
)

create table B (
        id integer not null auto_increment,
        name varchar(255),
        primary key (id)
)

 alter table A 
        add index FK418DB1FDB2 (id), 
        add constraint FK418DB1FDB2 
        foreign key (id) 
        references B (id)

相关推荐