Skip to content

Commit

Permalink
[Week9][Chap2] Feat : 도메인 세팅 (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
dionisos198 committed Nov 20, 2023
1 parent 3e9ee7f commit c0f52d4
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,48 @@
package study.datajpa.entity;

import lombok.Getter;
import lombok.Setter;
import lombok.*;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.*;

@Entity
@Getter @Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString(of = {"id","username","age"})
public class Member {
@Id @GeneratedValue
@Column(name = "member_id")
private Long id;

private String username;

protected Member(){
private int age;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "team_id")
private Team team;


}

public Member(String username) {
this.username = username;
this.username=username;
}

public Member(String username,int age){
this.username=username;
this.age=age;
}

public Member(String username,int age,Team team){
this.username=username;
this.age=age;

if(team!=null){
changeTeam(team);
}
}

public void changeTeam(Team team){
this.team=team;
team.getMembers().add(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package study.datajpa.entity;

import lombok.*;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@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")
List<Member> members=new ArrayList<>();

public Team(String name){
this.name=name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package study.datajpa.entity;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest

class MemberTest {

@PersistenceContext
EntityManager em;

@Test
@Transactional
@Rollback(false)
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();
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());
}
}
}

0 comments on commit c0f52d4

Please sign in to comment.