๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Spring๐Ÿƒ

T ์•„์นด๋ฐ๋ฏธ : JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ์ดˆ (2)

728x90

์—ฐ๊ด€๊ด€๊ณ„

๊ฐ์ฒด๋ฅผ ํ…Œ์ด๋ธ”์— ๋งž์ถ”์–ด ๋ชจ๋ธ๋งํ•˜๋Š” ๊ฒฝ์šฐ( ์ฐธ์กฐ ๋Œ€์‹  ์™ธ๋ž˜ ํ‚ค๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ ) : ๋ฐ์ดํ„ฐ ์ง€ํ–ฅ ๋ฐฉ์‹

@Entity
 public class Member {
 @Id @GeneratedValue
 private Long id;
 @Column(name = "USERNAME")
 private String name;
 private int age;
 @Column(name = "TEAM_ID")
 private Long teamId;
 …
 }
 @Entity
 public class Team {
 @Id @GeneratedValue
 private Long id;
 private String name;
 …
 }
 // Main
 //ํŒ€ ์ €์žฅ
 Team team = new Team();
 team.setName("TeamA");
 em.persist(team);
 // ๊ฐ์ฒด ์ƒ์„ฑ, ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๊ฐ’์„ ์ €์žฅ ํ›„ ์—”ํ‹ฐํ‹ฐ๋งค๋‹ˆ์ €์— ์ €์žฅํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ž‘์—…
 //ํšŒ์› ์ €์žฅ
 Member member = new Member();
 member.setName("member1");
 member.setTeamId(team.getId());
 em.persist(member);
 // ๊ฐ์ฒด ์ƒ์„ฑํ›„ ์ธ์Šคํ„ด์Šค๋ฅผ setter๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋’ค ์—”ํ‹ฐํ‹ฐ๋งค๋‹ˆ์ €์— ์ €์žฅ
 
 // => ์—ฐ๊ด€๊ด€๊ณ„๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ ์กฐํšŒํ•ด์•ผ ํ•œ๋‹ค
 //์กฐํšŒ
 Member findMember = em.find(Member.class, member.getId());
 // ๋ชจ๋“  ์ž‘์—…์€ ์—”ํ‹ฐํ‹ฐ๋งค๋‹ˆ์ €๊ฐ€ ์ˆ˜ํ–‰ํ•œ๋‹ค
 //์—ฐ๊ด€๊ด€๊ณ„๊ฐ€ ์—†์Œ
 Team findTeam = em.find(Team.class, team.getId());
 
 // ํ˜‘๋ ฅ๊ด€๊ณ„๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์—†๋Š” ๋ฐ์ดํ„ฐ ์ง€ํ–ฅ๋ฐฉ์‹ ~ ๋งค ๋ฒˆ ๋ฐ–์—์„œ ์™ธ๋ž˜ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ฐพ์•„์•ผ ํ•œ๋‹ค

 

๋‹จ๋ฐฉํ–ฅ ๋งคํ•‘

์ถœ์ฒ˜ : T ์•„์นด๋ฐ๋ฏธ, member๋Š” team์„ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค
๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ ์ฐธ์กฐ๋ฅผ ํ†ตํ•œ ํ…Œ์ด๋ธ”๊ณผ์˜ ์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘

@Entity
 public class Member {
 
 	@Id @GeneratedValue
 	private Long id;
 
 	@Column(name = "USERNAME")
 	private String name;
 	private int age;
 	// @Column(name = "TEAM_ID")
 	// private Long teamId; ~ ์™ธ๋ž˜ ํ‚ค ๋ฐฉ์‹์ด ์•„๋‹Œ,
	@ManyToOne // ๊ธฐ๋ณธ ๊ฐ’ Member, Team์„ ๊ฐ™์ด ๊ฐ€์ ธ์˜ค๋Š”๋ฐ, fetchType.LAZY ์‹ค์ œ ์‚ฌ์šฉํ•  ๋•Œ ๊ฐ€์ ธ์˜ด
    // ์ง€์—ฐ๋กœ๋”ฉ
    @JoinColumn(name = "TEAM_ID")
    private Team team; // ~ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ Team ํด๋ž˜์Šค๋ฅผ ์ฐธ์กฐ
 }
 
 // main
 // ํŒ€ ์ €์žฅ
 Team team = new Team();
 team.setName("TeamA");
 em.persist(team);
 // ํšŒ์› ์ €์žฅ
 Member member = new Member();
 member.setName("member1");
 member.setTeam(team); // ๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„ ์„ค์ •, ์ฐธ์กฐ ์ €์žฅ
 em.persist(member);
 
 // ์กฐํšŒ
 Member findMember = em.find(Member.class, member.getId());
// ์ฐธ์กฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์—ฐ๊ด€๊ด€๊ณ„ ์กฐํšŒ
 Team findTeam = findMember.getTeam();

-> DB์—์„œ๋Š” ๋ฐ์ดํ„ฐ ์ง€ํ–ฅ ๋ฐฉ์‹์ด๋‚˜ ์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘ ๋ฐฉ์‹์ด๋‚˜ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ™๋‹ค.

 

** ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ : ๊ฐ์ฒด ๊ฐ„ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•  ๊ฒƒ -> Member ์—”ํ‹ฐํ‹ฐ ์•ˆ์—์„œ Team ์—”ํ‹ฐํ‹ฐ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๊ฐ์ฒด team๊ณผ DB์˜ Member ํ…Œ์ด๋ธ”์˜ FK์ธ Team_ID๋ฅผ ๋งคํ•‘ํ•˜์—ฌ PK๋ฅผ ํ†ตํ•ด Team ๊ฐ’์— ์ ‘๊ทผ ๋ฐ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์–‘๋ฐฉํ–ฅ ๋งคํ•‘

์ถœ์ฒ˜: T์•„์นด๋ฐ๋ฏธ
์–‘๋ฐฉํ–ฅ์˜ ๊ฒฝ์šฐ ๊ฐ์ฒด๊ฐ„ ์ฐธ์กฐ ๊ฐ’์˜ ํ˜ผ๋™์ด ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๊ด€๊ณ„์˜ ์ฃผ์ธ์ด ํ•„์š”ํ•˜๋‹ค

@OneToMany(mappedBy = "team") // ๊ด€๊ณ„์˜ ์ฃผ์ธ์€ Member๋‹ค
 List<Member> members = new ArrayList<Member>(); // Team ์—”ํ‹ฐํ‹ฐ์—์„œ Member ์—”ํ‹ฐํ‹ฐ ์ฐธ์กฐ
 
 // main
 // ์กฐํšŒ
 Team findTeam = em.find(Team.class, team.getId());
 int memberSize = findTeam.getMembers().size(); // ์—ญ๋ฐฉํ–ฅ ์กฐํšŒ

๋‘ ์—”ํ‹ฐํ‹ฐ ์ค‘ ํ•˜๋‚˜์˜ ์—”ํ‹ฐํ‹ฐ๊ฐ€ ์™ธ๋ž˜ ํ‚ค๋ฅผ ๊ด€๋ฆฌํ•ด์•ผ ํ•œ๋‹ค

์–‘๋ฐฉํ–ฅ์˜ ๊ฒฝ์šฐ ๊ฐ์ฒด๊ฐ„ ์ฐธ์กฐ ๊ฐ’์˜ ํ˜ผ๋™์ด ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๊ด€๊ณ„์˜ ์ฃผ์ธ์ด ํ•„์š”ํ•˜๋‹ค

๊ด€๊ณ„์˜ ์ฃผ์ธ

- ์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ๋งŒ์ด ์™ธ๋ž˜ ํ‚ค๋ฅผ ๊ด€๋ฆฌ(๋“ฑ๋ก, ์ˆ˜์ •)

- ์ฃผ์ธ์ด ์•„๋‹Œ ์ชฝ์€ ์ฝ๊ธฐ๋งŒ ๊ฐ€๋Šฅ

 

- HOW?

์ฃผ์ธ์ด ์•„๋‹Œ ์ชฝ์˜ ๊ฐ์ฒด์˜ ํ•„๋“œ์—, ๊ด€๊ณ„์˜ ๋Œ€์ƒ์„ ์ฐธ์กฐํ•˜๋Š” ํ•„๋“œ์— mappedBy ์†์„ฑ์„ ๋ถ€์—ฌํ•˜์—ฌ ๊ทธ ๋Œ€์ƒ์„ ์ฃผ์ธ์œผ๋กœ ํ•œ๋‹ค.

 

- Who is Owner?(๊ถŒ์žฅ)

์™ธ๋ž˜ ํ‚ค์™€ ์—ฐ๊ด€๊ด€๊ณ„๋กœ ๋งคํ•‘๋˜์–ด ์žˆ๋Š” ์—”ํ‹ฐํ‹ฐ๊ฐ€ ์ฃผ์ธ์ด๋‹ค.

 

์ถœ์ฒ˜: T์•„์นด๋ฐ๋ฏธ

์„ค๊ณ„๋Š” ๋‹จ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ๊ฐ€๋Šฅํ•ด๋„ ์ถฉ๋ถ„ํ•˜๋‹ค. ์–‘๋ฐฉํ–ฅ์€ ์ฐธ์กฐ ๊ฐ’ ์กฐํšŒ๋ฅผ ์กฐ๊ธˆ ๋” ํŽธํ•˜๊ฒŒ ํ•˜๋ ค๊ณ  ํ•˜๋Š” ๊ฒƒ์ผ ๋ฟ.

~ํ•„์ˆ˜ : ๋‹จ๋ฐฉํ–ฅ / ๋ถ€๊ฐ€ : ์–‘๋ฐฉํ–ฅ

 

์–‘๋ฐฉํ–ฅ ๋งคํ•‘์‹œ ์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ์— ๊ฐ’์„ ์ž…๋ ฅํ•ด์•ผ ํ•œ๋‹ค. (์ˆœ์ˆ˜ํ•œ ๊ฐ์ฒด ๊ด€๊ณ„๋ฅผ ๊ณ ๋ คํ•˜๋ฉด ํ•ญ์ƒ ์–‘์ชฝ๋‹ค ๊ฐ’์„ ์ž…๋ ฅํ•ด์•ผ ํ•œ๋‹ค.)

// main
Team team = new Team();
 team.setName("TeamA");
 em.persist(team);
 Member member = new Member();
 member.setName("member1");
 // ์ฃผ์ธ์˜ ๋ฐ˜๋Œ€ํŽธ์— ์—ฐ๊ด€๊ด€๊ณ„ ์„ค์ •
 team.getMembers().add(member);
 
 // ์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ์— ๊ฐ’ ์„ค์ •
 member.setTeam(team); //**
 em.persist(member);

์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘ ์–ด๋…ธํ…Œ์ด์…˜ + 

๋‹ค๋Œ€์ผ

์ผ๋Œ€๋‹ค

์ผ๋Œ€์ผ

๋‹ค๋Œ€๋‹ค

@JoinColumn : ์™ธ๋ž˜ ํ‚ค ๋งคํ•‘ ์‹œ ์‚ฌ์šฉ

@JoinTable

 

์ƒ์†๊ด€๊ณ„ ์–ด๋…ธํ…Œ์ด์…˜

@Inheritance

@DiscriminatorColumn

@DIscriminatorValue

@MappedSuperclass

 

๋ณตํ•ฉํ‚ค ์–ด๋…ธํ…Œ์ด์…˜

@IdClass

@EnbeddedId

@Embeddable

@Mapsld

728x90