@Test
@Transactional
void contextLoads() {
Member member = Member.builder().name("김길동").build();
em.persist(member);//1차 캐시에 저장
Team team = Team.builder().name("A팀").build();
em.persist(team);//1차 캐시에 저장
team.settingMember(member);
Member findMember = em.find(Member.class, member.getId());
Team findTeam = em.find(Team.class, team.getId());
Assertions.assertEquals(findMember, findTeam.getMemberList().get(0));
Assertions.assertEquals(findTeam, findMember.getTeam());
}
@Entity
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@SequenceGenerator(name="TEAM_SEQUENCE",
sequenceName = "TEAM_SEQ"
)
public class Team {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "TEAM_SEQUENCE")
@Column(name = "TEAM_ID")
private Long id;
@Column(unique = true)
private String name;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "team")
private List<Member> memberList = new ArrayList<>();
public void settingMember(Member member){
member.setTeam(this);
//memberList = new ArrayList<>(); builder로 생성하면 이주석을 해제해줘야지만 테스트통과뜸
this.memberList.add(member);
}
}
private List<Member> memberList = new ArrayList<>();
할당은 왜 해주는 것일까?= new ArrayList
하는 부분이 builder에 대한 class에는 없기때문에전체코드
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package jpaStudy.ex.entity;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
@Entity
@NamedQuery(
name = "Member.findByUsername",
query = "select m from Member m where m.name = :name"
)
public class Member {
@Id
@GeneratedValue
@Column(
name = "MEMBER_ID"
)
private Long id;
@Column
private String name;
private Integer age;
@ElementCollection
@CollectionTable(
name = "Address",
joinColumns = {@JoinColumn(
name = "MEMBER_ID"
)}
)
private List<Address> addressList = new ArrayList();
@ManyToOne(
fetch = FetchType.EAGER
)
@JoinColumn(
name = "team_id"
)
private Team team;
@ManyToOne(
fetch = FetchType.EAGER
)
@JoinColumn(
name = "group1_id"
)
private Group1 group1;
@Embedded
private Address myAddress;
public Member(String name, Team team) {
this.name = name;
team.addMember(this);
this.team = team;
}
public Member(String name, Team team, int age, Group1 group1) {
this.name = name;
team.addMember(this);
this.age = age;
this.team = team;
this.group1 = group1;
}
public static Member.MemberBuilder builder() {
return new Member.MemberBuilder();
}
public Long getId() {
return this.id;
}
public String getName() {
return this.name;
}
public Integer getAge() {
return this.age;
}
public List<Address> getAddressList() {
return this.addressList;
}
public Team getTeam() {
return this.team;
}
public Group1 getGroup1() {
return this.group1;
}
public Address getMyAddress() {
return this.myAddress;
}
public void setId(final Long id) {
this.id = id;
}
public void setName(final String name) {
this.name = name;
}
public void setAge(final Integer age) {
this.age = age;
}
public void setAddressList(final List<Address> addressList) {
this.addressList = addressList;
}
public void setTeam(final Team team) {
this.team = team;
}
public void setGroup1(final Group1 group1) {
this.group1 = group1;
}
public void setMyAddress(final Address myAddress) {
this.myAddress = myAddress;
}
public Member() {
}
public Member(final Long id, final String name, final Integer age, final List<Address> addressList, final Team team, final Group1 group1, final Address myAddress) {
this.id = id;
this.name = name;
this.age = age;
this.addressList = addressList;
this.team = team;
this.group1 = group1;
this.myAddress = myAddress;
}
public static class MemberBuilder {
private Long id;
private String name;
private Integer age;
private List<Address> addressList;
private Team team;
private Group1 group1;
private Address myAddress;
MemberBuilder() {
}
public Member.MemberBuilder id(final Long id) {
this.id = id;
return this;
}
public Member.MemberBuilder name(final String name) {
this.name = name;
return this;
}
public Member.MemberBuilder age(final Integer age) {
this.age = age;
return this;
}
public Member.MemberBuilder addressList(final List<Address> addressList) {
this.addressList = addressList;
return this;
}
public Member.MemberBuilder team(final Team team) {
this.team = team;
return this;
}
public Member.MemberBuilder group1(final Group1 group1) {
this.group1 = group1;
return this;
}
public Member.MemberBuilder myAddress(final Address myAddress) {
this.myAddress = myAddress;
return this;
}
public Member build() {
return new Member(this.id, this.name, this.age, this.addressList, this.team, this.group1, this.myAddress);
}
public String toString() {
return "Member.MemberBuilder(id=" + this.id + ", name=" + this.name + ", age=" + this.age + ", addressList=" + this.addressList + ", team=" + this.team + ", group1=" + this.group1 + ", myAddress=" + this.myAddress + ")";
}
}
}