Hibernate!!一对一关联映射

one-to-one 

 一对一......单向外键关联

1)annotation方式: 在加入外键(即加入引用的一方)的实体类的类名上加@Entity ; 在主键的get方法上加@id @GeneratedValue ; 

  在体现关系的外键属性上加 @onetoone 和  @JoinColumn(name=”添加的外键的名字“,不写的话就是自动生成的名字)

package com.hb.model;

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

@Entity
public class Wife {
    private  int  id ;
    private  String  name ;
    
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

  ....
  
}

 

package com.hb.model;

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;
    }...

} 

2)在xml中,用到<many-to-one    unique="true">

public class Student {
    
    private  int  id ;
    private   String  age ;    
    private  String  name ;
    // get、set方法省略
public class StuIdCard {
    private  int  id ;
    private  String  num ;
    private  Student  student ;
    // get、set方法省略

StuIdCard.hbm.xml如下:

<hibernate-mapping  package="com.hb.model">
    <!-- 映射表 和 类 -->
    <class  name="com.hb.model.StuIdCard">
        <id name="id"  column="id">
            <generator class="native"></generator>
        </id>
        <!--  映射具体的属性 -->
        <property name="num"  column="num"></property>
        <many-to-one name="student"  column="studentId" unique="true"></many-to-one>
    </class>
   
</hibernate-mapping>

 

 

 

 一对一.......双向外键关联

1)annotation方式: 在两个实体类上均加入对方的引用,但是在单项的基础上,在加入的新的引用的实体类上加@onetoone(mappedBy=“另一个实体类的关于此类的引用的属性名”)

规律:凡是双向关联,必设mappedBy,表示被对方设置,即在数据库中,只有对方存在一个外键,防止冗余。

@Entity
public class Wife {
    private  int  id ;
    private  String  name ;
    private  Husband  husband ;
    
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }    
    @OneToOne(mappedBy="wife")
    public Husband getHusband() {
        return husband;
    }
@Entity
public class Husband {
    private  int  id ;
    private  String  name ;
    private  Wife   wife ;
    
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    
    @OneToOne
    @JoinColumn(name="wifeId")
    public Wife getWife() {
        return wife;
    }
    

2)xml方式:用到<many-to-one    unique="true">  用到<one-to-one    property-ref="XXXX"> ,不是很懂

<hibernate-mapping  package="com.hb.model">
    <!-- 映射表 和 类 -->
    <class  name="com.hb.model.StuIdCard">
        <id name="id"  column="id">
            <generator class="native"></generator>
        </id>
        <!--  映射具体的属性 -->
        <property name="num"  column="num"></property>
        <many-to-one name="student"  column="studentId" unique="true"></many-to-one>
    </class>
   
</hibernate-mapping>
<hibernate-mapping  package="com.hb.model">
    <!-- 映射表 和 类 -->
    <class  name="com.hb.model.Student">
        <id name="id"  column="id">
            <generator class="native"></generator>
        </id>
        <!--  映射具体的属性 -->
        <property name="name"  column="name"></property>
        <property name="age"  column="age"></property>

<one-to-one name="stuIdCard" property-ref="student"></one-to-one> </class> </hibernate-mapping>

 

 一对一......单向主键关联 (主键关联用的很少,不重要)

1)annotation方式: 在加入外键(即加入另一个类引用的一方)的实体类的类名上加@Entity ; 在主键的get方法上加@id @GeneratedValue ; 在体现关系的外键属性上加 @onetoone 和  @@PrimaryKeyJoinColumn

注:但是在数据库中没有任何关联,不知道为什么

 

@Entity
public class Husband {
    private  int  id ;
    private  String  name ;
    private  Wife   wife ;
    
    
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
        
    @OneToOne
    @PrimaryKeyJoinColumn
    public Wife getWife() {
        return wife;
    }
    
@Entity
public class Wife {
    private  int  id ;
    private  String  name ;
    private  int  age ;
    
    
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

 

 

 2)xml方式:其中一个映射文件用到<one-to-one    name="XXX"> 

  另一个映射文件用到<one-to-one    name="XXXX"  constrained=“true”>此的文件的主键的生成策略也要改变

 

 

 一对一......双向主键关联 (主键关联用的很少,不重要)

 annotation方式:

@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
    @PrimaryKeyJoinColumn
    public Wife getWife() {
        return wife;
    }
@Entity
public class Wife {
    private int id;
    private String name;
    private Husband husband;
    @OneToOne
    @PrimaryKeyJoinColumn
    public Husband getHusband() {
        return husband;
    }
    public void setHusband(Husband husband) {
        this.husband = husband;
    }
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

 

 

联合主键

常用的一种方法是:将符合主键单独写成一个类,称之为复合主键类,此类就是一个普通的类,无需任何注释,但是要继承Serializable接口,重写equals()和hashCode()方法。

 

其它的类的属性无需修改。

在含有复合主键的实体类上加上@IdClass     @IdClass标注复合主键类

在加入关系属性的实体类上加上 @OneToOne  和 @JoinColumns (此方法的属性是一个数组)

 

public class WifePK implements Serializable {
    private int id;
    private String name;
    //get、set 方法省略
    //重写equals()和hashCode()方法

 

@Entity
@IdClass(value=WifePk.class)  //标注复合主键类
public class Wife {
    private  int  id ;
    private  String  name ;
    private  int  age ;
    
    
    @Id
    public int getId() {
        return id;
    }
    @Id
    public String getName() {
        return name;
    }
@Entity
public class Husband {
    private  int  id ;
    private  String  name ;
    private  Wife   wife ;
    
    
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    
    @OneToOne
    @JoinColumns(
            {
                @JoinColumn(name="wifeId", referencedColumnName="id") ,
                @JoinColumn(name="wifeName" , referencedColumnName="name")
            }
    )//这是一个数组,表示复合主键的名字还有对应的是类的属性
    public Wife getWife() {
        return wife;
    }
    

 

posted @ 2014-12-11 14:57  Mokaffe  阅读(181)  评论(0编辑  收藏  举报