-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e9ee7f
commit c0f52d4
Showing
3 changed files
with
107 additions
and
8 deletions.
There are no files selected for viewing
39 changes: 31 additions & 8 deletions
39
이진우/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/entity/Member.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
이진우/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/entity/Team.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
이진우/spring_data_jpa_jinu/data-jpa/src/test/java/study/datajpa/entity/MemberTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} | ||
|