source 는 Github 에 있습니다.
insert into team (team_name) values (?)
insert into team_member (name) values (?)
insert into team_member (name) values (?)
insert into team_list (team_team_id, list_member_id) values (?, ?)
insert into team_list (team_team_id, list_member_id) values (?, ?)
@Test
public void 단방향_OneToMany_테스트() throws Exception {
// given
List<TeamMember> teamMembers = new ArrayList<>();
TeamMember teamMemer = TeamMember.builder()
.name("testMember1")
.build();
teamMembers.add(teamMemer);
TeamMember teamMemer2 = TeamMember.builder()
.name("testMember2")
.build();
teamMembers.add(teamMemer2);
Team team = Team.builder()
.teamName("testName")
.teamMembers(teamMembers)
.build();
// when
Team result = teamRepository.save(team);
// then
Assert.assertThat(result.getTeamMembers().size(), is(2));
}
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Team {
@Id @Column(name = "team_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String teamName;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
List<TeamMember> teamMembers = new ArrayList<>();
@Builder
public Team(Long id, String teamName, List<TeamMember> teamMembers) {
this.id = id;
this.teamName = teamName;
this.teamMembers = teamMembers;
}
}
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class TeamMember {
@Id @Column(name = "member_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String name;
@Builder
public TeamMember(Long id, String name) {
this.id = id;
this.name = name;
}
}
insert into team (team_name) values (?)
insert into team_member (name) values (?)
insert into team_member (name) values (?)
update team_member set team_id=? where team_member_id=?
update team_member set team_id=? where team_member_id=?
@Test
public void 단방향_OneToMany_테스트() throws Exception {
// given
List<TeamMember> teamMembers = new ArrayList<>();
TeamMember teamMemer = TeamMember.builder()
.name("testMember1")
.build();
teamMembers.add(teamMemer);
TeamMember teamMemer2 = TeamMember.builder()
.name("testMember2")
.build();
teamMembers.add(teamMemer2);
Team team = Team.builder()
.teamName("testName")
.teamMembers(teamMembers)
.build();
// when
Team result = teamRepository.save(team);
// then
Assert.assertThat(result.getTeamMembers().size(), is(2));
}
public class Team {
@Id @Column(name = "team_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String teamName;
// 조인 컬럼 단방향 매핑
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "team_id")
List<TeamMember> teamMembers = new ArrayList<>();
}
insert into team (team_name) values (?)
insert into team_member (name) values (?)
insert into team_member (name) values (?)
@Test
@Transactional
public void 양방향_OneToMany_테스트() throws Exception {
// given
Team team = Team.builder()
.teamName("testName")
.build();
team.addTeamMember("teamMember11");
team.addTeamMember("teamMember22");
// when
Team result = teamRepository.save(team);
// then
Assert.assertThat(result.getTeamMembers().size(), is(2));
}
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Team {
@OneToMany(mappedBy = "team", cascade = CascadeType.ALL, orphanRemoval = true)
List<TeamMember> teamMembers = new ArrayList<>();
...
...
}
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString
public class TeamMember {
@ManyToOne(optional = false)
@JoinColumn(name = "team_id")
private Team team;
...
...
}