博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate中的Entity类之间的OneToMany关联
阅读量:4179 次
发布时间:2019-05-26

本文共 2937 字,大约阅读时间需要 9 分钟。

OneToMany关联将一个父Entity类与若干个子Entity类联系起来。

1. 双向关联

通常,OneToMany关联都有与之反向的ManyToOne关联对应,两者成组出现,这被称为双向关联。

双向关联中,可以从任何一个Entity类实例访问关联的另一个Entity类实例(通过get*()方法)。

在数据库模式中,也只需要常规地,在子Entity类(owning side)中设置外键关联父Entity类(mappedBy side)即可。

父Entity定义如下:

@Entity(name = "Person")public static class Person {    @Id    @GeneratedValue    private Long id;    @OneToMany(mappedBy = "person", cascade = CascadeType.ALL, orphanRemoval = true)    private List
phones = new ArrayList<>(); public Person() { } public Person(Long id) { this.id = id; } public List
getPhones() { return phones; } public void addPhone(Phone phone) { phones.add( phone ); phone.setPerson( this ); } public void removePhone(Phone phone) { phones.remove( phone ); phone.setPerson( null ); }}

子Entity定义如下:

@Entity(name = "Phone")public static class Phone {    @Id    @GeneratedValue    private Long id;    @NaturalId    @Column(unique = true)    private String number;    @ManyToOne    private Person person;    public Phone() {    }    public Phone(String number) {        this.number = number;    }    public Long getId() {        return id;    }    public String getNumber() {        return number;    }    public Person getPerson() {        return person;    }    public void setPerson(Person person) {        this.person = person;    }    @Override    public boolean equals(Object o) {        if ( this == o ) {            return true;        }        if ( o == null || getClass() != o.getClass() ) {            return false;        }        Phone phone = (Phone) o;        return Objects.equals( number, phone.number );    }    @Override    public int hashCode() {        return Objects.hash( number );    }}
子Entity类重写了equals()和hashCode()方法,是为了利用Phone类中的number的唯一性,通常不是必须的。

2. 单向关联

不过,双向关联并不是必须的,也可以只有其中一种关联,就是单向关联,这样只能从拥有(owning)关联的Entity类实例访问关联的另一个Entity类实例。

如果只有OneToMany关联,则在数据库模式中,首先在子Entity类对应的数据库表中无需外键,因为子Entity类实例根本不知道要关联谁。此外,还额外需要一个中间表,以表示两者的关联关系。因为,在OneToMany关联的父Entity类(owning side)对应的数据库表中,是无法表示这种关联关系的。

父Entity定义如下:

@Entity(name = "Person")public static class Person {    @Id    @GeneratedValue    private Long id;    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)    private List
phones = new ArrayList<>(); public Person() { } public List
getPhones() { return phones; }}
子Entity定义如下:

@Entity(name = "Phone")public static class Phone {    @Id    @GeneratedValue    private Long id;    private String number;    public Phone() {    }    public Phone(String number) {        this.number = number;    }    public Long getId() {        return id;    }    public String getNumber() {        return number;    }}
数据库模式中的中间表如下:

CREATE TABLE Person_Phone (    Person_id BIGINT NOT NULL ,    phones_id BIGINT NOT NULL)
单向关联在删除子Entity类实例的操作中,执行效率非常低下。因为,在持久化的操作过程中,首先要删除父Entity类实例关联的所有子Entity类实例,然后再重新插入尚未被删除的子Entity类实例。

转载地址:http://anlai.baihongyu.com/

你可能感兴趣的文章
637. Average of Levels in Binary Tree(Tree)
查看>>
226. Invert Binary Tree(Tree)
查看>>
328. Odd Even Linked List(链表)
查看>>
617. Merge Two Binary Trees(Tree)
查看>>
700. Search in a Binary Search Tree(Tree)
查看>>
515. Find Largest Value in Each Tree Row(Tree)
查看>>
897. Increasing Order Search Tree(Tree)
查看>>
199. Binary Tree Right Side View(Tree)
查看>>
230. Kth Smallest Element in a BST(Tree)
查看>>
求字符串的最长回文串-----Manacher's Algorithm 马拉车算法
查看>>
回溯法常用的解题模板和常见题型
查看>>
深入分析Java I/O 的工作机制
查看>>
动态规划的套路----左神
查看>>
KMP算法简解
查看>>
左神算法课进阶版总结
查看>>
左神算法基础班总结
查看>>
Linux性能优化
查看>>
进程间的通信---UNIX高级环境编程
查看>>
基于SSH开发的城市公交管理系统 JAVA MySQL
查看>>
基于SSH开发的勤工助学管理系统 JAVA MySQL
查看>>