成长的足迹

见证个人成长,分享技术经验

Hibernate中的Entity类之间的OneToOne关联

Hibernate中的Entity类之间的OneToOne关联也存在单向关联和双向关联两种形式。

OneToOne关联的两个Entity类是完全对等的,任何一方都可以是这种关联的子类(owning side),而另一方是父类(reverse side)。


1. 双向关联

在OneToOne关联的两个Entity类中,选择一个作为子实体类(owning side),其对应的表中设置外键;另一个作为父实体类(inverse side)并设置mappedBy="..."关联属性。

父Entity类定义如下:

@Entity(name = "Phone")
public static class Phone {

    @Id
    @GeneratedValue
    private Long id;

    private String number;

    @OneToOne(mappedBy = "phone", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
    private PhoneDetails details;

    public Phone() {
    }

    public Phone(String number) {
        this.number = number;
    }

    public Long getId() {
        return id;
    }

    public String getNumber() {
        return number;
    }

    public PhoneDetails getDetails() {
        return details;
    }

    public void addDetails(PhoneDetails details) {
        details.setPhone( this );
        this.details = details;
    }

    public void removeDetails() {
        if ( details != null ) {
            details.setPhone( null );
            this.details = null;
        }
    }
}


子Entity类定义如下:

@Entity(name = "PhoneDetails")
public static class PhoneDetails {

    @Id
    @GeneratedValue
    private Long id;

    private String provider;

    private String technology;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "phone_id")
    private Phone phone;

    public PhoneDetails() {
    }

    public PhoneDetails(String provider, String technology) {
        this.provider = provider;
        this.technology = technology;
    }

    public String getProvider() {
        return provider;
    }

    public String getTechnology() {
        return technology;
    }

    public void setTechnology(String technology) {
        this.technology = technology;
    }

    public Phone getPhone() {
        return phone;
    }

    public void setPhone(Phone phone) {
        this.phone = phone;
    }
}

在这种双向关联中,Hibernate自动为子实体类(owning side)中的外键属性设置UNIQUE约束。如果违反了该约束,则在访问父实体类时会抛出org.hibernate.HibernateException异常。


2. 单向关联

在OneToOne单向关联中,只需在子实体类(owning side)设置关联,只需在子实体类(owning side)对应的表中设置外键。

父Entity类定义如下:

@Entity(name = "PhoneDetails")
public static class PhoneDetails {

    @Id
    @GeneratedValue
    private Long id;

    private String provider;

    private String technology;

    public PhoneDetails() {
    }

    public PhoneDetails(String provider, String technology) {
        this.provider = provider;
        this.technology = technology;
    }

    public String getProvider() {
        return provider;
    }

    public String getTechnology() {
        return technology;
    }

    public void setTechnology(String technology) {
        this.technology = technology;
    }
}

子Entity类定义如下:

@Entity(name = "Phone")
public static class Phone {

    @Id
    @GeneratedValue
    private Long id;

    private String number;

    @OneToOne
    @JoinColumn(name = "details_id")
    private PhoneDetails details;

    public Phone() {
    }

    public Phone(String number) {
        this.number = number;
    }

    public Long getId() {
        return id;
    }

    public String getNumber() {
        return number;
    }

    public PhoneDetails getDetails() {
        return details;
    }

    public void setDetails(PhoneDetails details) {
        this.details = details;
    }
}

阅读更多
版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/taiyangdao/article/details/51537373
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Hibernate中的Entity类之间的OneToOne关联

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭