member - team : @ManyToOne
@Entity
@Getter @Setter // setter는 가급적 없는 게 GOOD(실무에서는 꼭 필요할 때만 setter를 쓰거나, 별도의 비즈니스 네이밍된 함수를 사용)
@NoArgsConstructor(access = AccessLevel.PROTECTED) // @Entity 기본 생성자 필요, 아무곳에서나 사용하지 않도록 PROTECTED
@ToString(of = {"id", "username", "age"}) // "team" 추가하면 안됨(양방향 연관관계 일 때, ToString 무한 루프 주의)
public class Member {
@Id
@GeneratedValue
@Column(name = "member_id")
private Long id;
private String username;
private int age;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "team_id")
private Team team;
// 생성자
public Member(String username) {
this.username = username;
}
public Member(String username, int age, Team team) {
this.username = username;
this.age = age;
if(team != null){ // 일단은 team == null이면 무시
changeTeam(team);
}
}
// setter 대신 이런 메서드 생성하는 것이 GOOD
public void changeUsername(String username){
this.username = username;
}
// 연관관계 편의 메서드
public void changeTeam(Team team){
this.team = team;
team.getMembers().add(this);
}
}
@Entity
@Getter @Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString(of = {"id", "name"})
public class Team {
@Id
@GeneratedValue
@Column(name = "team_id")
private Long id;
private String name;
@OneToMany(mappedBy = "team")
private List<Member> members = new ArrayList<>();
// 생성자
public Team(String name) {
this.name = name;
}
}
@SpringBootTest
@Transactional
@Rollback(value = false) // 저장되는지 실제로 보기 위해서
class MemberTest {
@PersistenceContext
EntityManager em;
@Test
public void testEntity(){
Team teamA = new Team("teamA");
Team teamB = new Team("teamB");
em.persist(teamA);
em.persist(teamB);
Member member1 = new Member("member1", 10, teamA);
Member member2 = new Member("member2", 20, teamA);
Member member3 = new Member("member3", 30, teamB);
Member member4 = new Member("member4", 40, teamB);
em.persist(member1);
em.persist(member2);
em.persist(member3);
em.persist(member4);
// 초기화
em.flush(); // DB에 쿼리 날림
em.clear();
// 확인
List<Member> members = em.createQuery("select m from Member m", Member.class)
.getResultList();
// 출력
for (Member member : members) {
System.out.println("member = " + member);
System.out.println("-> member.team = " + member.getTeam()); // 속한 팀 출력
}
}
}