Home Spring: Entity OneToMany(일대다) 관계 정리
Post
Cancel

Spring: Entity OneToMany(일대다) 관계 정리

OneToMany

relation

위 처럼 일대다 관계를 가지는 Entity를 작성하는 방법과 차이점에 대한 정리

기본 동작(by JoinTable)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Entity
class Student(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Long,
    var name: String
)

@Entity
class Class(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Long,
    @OneToMany
    var students: MutableSet<Student> = mutableSetOf()
)

결과

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
create table class (
    id bigint generated by default as identity,
    primary key (id)
)

create table class_students (
    class_id bigint not null,
    students_id bigint not null,
    primary key (class_id, students_id)
)

create table student (
   id bigint generated by default as identity,
    name varchar(255),
    primary key (id)
)

join-table

OneToMany관계는 기본적으로 JoinTable 기반으로 동작하며 생성되는 Table은 위와 같다.

MappedBy

자식(여기서는 student) entity에서 부모 entity의 정보를 포함하여 ManyToOne 관계를 구현

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Entity
class Student(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Long,
    var name: String,
    var classId: Long
    or
    @ManyToOne
    var `class`: Class
)

@Entity
class Class(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Long,
    @OneToMany(mappedBy = "classId")
    var students: MutableSet<Student> = mutableSetOf()
    or
    @OneToMany(mappedBy = "class")
    var students: MutableSet<Student> = mutableSetOf()
)
1
2
3
4
5
6
7
8
9
10
11
create table class (
   id bigint generated by default as identity,
    primary key (id)
)

create table student (
   id bigint generated by default as identity,
    name varchar(255),
    class_id bigint not null,
    primary key (id)
)

mapped-by

Query 비교

INESRT - JOIN TABLE

1
2
3
4
5
6
7
8
9
10
val `class` = classRepository.save(Class())
val students = studentRepository.saveAll(
    mutableListOf(
        Student(name = "a"),
        Student(name = "b"),
        Student(name = "c"),
    )
).toMutableSet()

`class`.students = students
1
2
3
4
5
6
7
Hibernate: insert into class (id) values (default)
Hibernate: insert into student (id, name) values (default, ?)
Hibernate: insert into student (id, name) values (default, ?)
Hibernate: insert into student (id, name) values (default, ?)
Hibernate: insert into class_students (class_id, students_id) values (?, ?)
Hibernate: insert into class_students (class_id, students_id) values (?, ?)
Hibernate: insert into class_students (class_id, students_id) values (?, ?)

INESRT - MappedBy

1
2
3
4
5
6
7
8
9
10
val `class` = classRepository.save(Class())
val students = studentRepository.saveAll(
    mutableListOf(
        Student(name = "a", `class` = `class`),
        Student(name = "b", `class` = `class`),
        Student(name = "c", `class` = `class`),
    )
).toMutableSet()

`class`.students = students
1
2
3
4
Hibernate: insert into class (id) values (default)
Hibernate: insert into student (id, class_id, name) values (default, ?, ?)
Hibernate: insert into student (id, class_id, name) values (default, ?, ?)
Hibernate: insert into student (id, class_id, name) values (default, ?, ?)

FetchType은 따로 지정하지않고 기본값(LAZY)으로 비교진행

SELECT - JOIN TABLE

1
2
Hibernate: select class0_.id as id1_0_0_ from class class0_ where class0_.id=?
Hibernate: select students0_.class_id as class_id1_1_0_, students0_.students_id as students2_1_0_, student1_.id as id1_2_1_, student1_.name as name2_2_1_ from class_students students0_ inner join student student1_ on students0_.students_id=student1_.id where students0_.class_id=?

SELECT - MappedBy

1
2
Hibernate: select class0_.id as id1_0_0_ from class class0_ where class0_.id=?
Hibernate: select students0_.class_id as class_id3_1_0_, students0_.id as id1_1_0_, students0_.id as id1_1_1_, students0_.class_id as class_id3_1_1_, students0_.name as name2_1_1_ from student students0_ where students0_.class_id=?
This post is licensed under CC BY 4.0 by the author.

Spiring: ManyToMany 관계 Entity에서 컬럼 추가

Spring 애플 로그인(oauth) 구현