Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- SpringBoot
- VUE
- MySQL시작하기
- NullPointerException
- MySQL
- CloutNative
- offset
- frontend
- SQL
- wappalyzer
- 이클립스
- Lombok
- Seek_Keyset
- Java
- 우분투에war배포
- windows10
- intellij
- String
- spring
- DB생성
- minikube
- gradle
- springMVC
- appleM1
- Postman
- pagination
- restful api
- 스프링에러
- K8S
- MYSQL에러
Archives
- Today
- Total
미운 오리 새끼의 우아한 개발자되기
[JPA] 연관관계 매핑 기초 (1) 단방향 연관관계 본문
1. 연관관계 매핑을 이해하기 위한 기초
- 방향(Direction): [단방향, 양방향]
- 다중성(Muliplicity) : [다대일(N:1), 일대다(1:N), 일대일(1:1), 다대다(N:M)]
- 연관관계의 주인(Owner): 객체를 양방향 연관관계로 만들려면 연관관계의 주인을 정해야함
2. 단방향 연관관계
- 객체 연관관계 vs. 테이블 연관관계
- 객체는 참조(Reference)로 연관관계를 맺음 -> 참조를 통한 연관관계는 언제나 단방향임. 즉, 양뱡향이라는 것도 서로 다른 단뱡향 2개를 양방향으로 보이게 만드는 것
- 테이블은 외래 키(Foreign Key)로 연관관계를 맺음
// 단방향
class A {
B b;
}
class B { }
// 양방향
class A {
B b;
}
class B {
A a;
}
- 객체 관계 매핑 (다대일(N:1), 단방향)
@Entity
public class Member {
@Id
@Column(name = "MEMBER_ID")
private Long id;
private String username;
// 연관관계 매핑
@ManyToOne // 다(Member)대일(Team)
@JoinColumn(name = "TEAM_ID") // 외래키를 매핑할 때 쓰는 annotation. 매핑할 외래키 이름을 씀
private Team team;
// 연관관계 설정
public void setTeam(Team team) {
this.team = team;
}
// Getter, Setter...
}
@Entity
public class Team {
@Id
@Column(name = "TEAM_ID")
private Long id;
private String name;
// Getter, Setter...
}
Reference : 자바 ORM 표준 JPA 프로그래밍 (김영한 저)
'Spring & Spring Boot > Spring' 카테고리의 다른 글
[JPA] 다양한 연관관계 매핑 (3) 일대일 (0) | 2023.04.11 |
---|---|
[JPA] 다양한 연관관계 매핑 (2) 일대다 (0) | 2023.04.11 |
[JPA] 다양한 연관관계 매핑 (1) 다대일 (0) | 2023.04.11 |
[JPA] 연관관계 매핑 기초 (2) 양방향 연관관계 (0) | 2023.04.09 |
[Spring MVC] Spring MVC 작동 원리 (0) | 2020.09.07 |