Hibernate的关联映射——单向1-1关联

Hibernate的关联映射——单向1-1关联

  对于单向的1-1关联关系,需要在持久化类里增加代表关联实体的成员变量,并为该成员变量添加setter方法和getter方法。从持久化类的代码上看,单向1-1与单向N-1没有丝毫区别。因为N的一端或者1的一端都是直接访问关联实体,只需要增加代表关联实体的属性即可。

  对于1-1关联(不管是单向关联,还是双向关联),都需要使用@OneToOne修饰代表关联实体的属性。

1.基于外键的单向1-1关联

  对于基于外键的1-1关联而言,只要先试用@OneToOne注解修饰代表关联实体的属性,再使用@JoinColumn映射外键列即可——由于是1-1关联,因此应该为@JoinColumn增加unique=true。

(1)Person2

import javax.persistence.Column;
import javax.persistence.Entity;
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.Cascade;
import org.hibernate.annotations.CascadeType;

@Entity
@Table(name="person2")
public class Person2 {
    @Id @Column(name="p_id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
    @Column(name="p_name")
    private String name;
    @Column(name="p_age")
    private Integer age;
    //定义该Person实体对应的Address实体
    @OneToOne(targetEntity=Address2.class)
    //映射名为address_id的外键列,参照关联实体对应表的address_id主键列
    @JoinColumn(
            name="address_id",
            referencedColumnName="address_id",
            unique=true
            )
    @Cascade(CascadeType.ALL)
    private Address2 address;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Address2 getAddress() {
        return address;
    }
    public void setAddress(Address2 address) {
        this.address = address;
    }
}

(2)Address2

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="addr2")
public class Address2 {
    @Id @Column(name="address_id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
    @Column(name="address_name")
    private String addrName;
    public Address2(){

    }
    public Address2(String addrName){
        this.addrName = addrName;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getAddrName() {
        return addrName;
    }
    public void setAddrName(String addrName) {
        this.addrName = addrName;
    }
}

2.有连接表的单向1-1关联

  Hibernate允许采用连接表映射单向1-1关联。有连接表的1-1关联同样需要显式使用@JoinTable映射连接表。由于此处的@JoinTable映射的连接表维护的是1-1关联,因此程序需要为@JoinTable中joinColumn属性映射的外键列增加unique=true,也为inverseJoinColumns属性映射的晚间列增加unique=true。

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="person3")
public class Person3 {
    @Id @Column(name="p_id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
    @Column(name="p_name")
    private String name;
    @Column(name="p_age")
    private Integer age;
    //定义该Person实体关联的Address实体
    @OneToOne(targetEntity=Address3.class)
    @JoinTable(
            name="person_address_3",
            joinColumns=@JoinColumn(
                        name="p_id",
                        referencedColumnName="p_id",
                        unique=true
                    ),
            inverseJoinColumns=@JoinColumn(
                        name="address_id",
                        referencedColumnName="address_id",
                        unique=true
                    )
            )
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}
上一篇:异常 android.content.res.Resources$NotFoundException: String resource ID #0x61


下一篇:spring boot自定义类配置绑定在配置文件中自动提示