Spring #3

ims·2020년 10월 22일
0

BlueDot-Spring

목록 보기
4/13

@OneToMany @ManyToOne

mappedBy

위와 같은 그림에서

Post.java

class Post {
...

@ManyToOne(fetch=FetchType.LAZY)
private User user;
}

User.java

class User{
...
@OneToMany(mappedBy="user")
private List<Post> postList;
}

User가 갖고 있는 PostList의 주인은 Post안에 있는 외래키 user라고 생각하면 이해가 빠를 듯 싶다. DB에는 없지만, @OneToMany쪽 List을 spring에서는 추가로 명시해주어야 한다고 생각하면 이해가 된다.

Fetch type

@ManyToOne 어노테이션에 fetch 타입을 줄 수 있다.

https://ict-nroo.tistory.com/132

ManyToOne 쪽이 실제 외래키를 갖고 있는 실체라고 생각해서 이해하고 있다.

follow table mapping 방법

https://stackoverflow.com/questions/44331694/following-followers-in-spring

@GeneratedValue(strategy = GenerationType.IDENTITY)

IDENTITY : 기본 키 생성을 데이터베이스에 위임하는 방법 (데이터베이스에 의존적)

https://ithub.tistory.com/24

@GeneratedValue는 주키의 값을 위한 자동 생성 전략을 명시하는데 사용한다.

https://jsaver.tistory.com/entry/Id%EC%99%80-GeneratedValue-%EC%95%A0%EB%85%B8%ED%85%8C%EC%9D%B4%EC%85%98

즉, id 값을 null로 하면 DB가 알아서 AUTO_INCREMENT 해준다

https://gmlwjd9405.github.io/2019/08/12/primary-key-mapping.html>>

profile
티스토리로 이사했습니다! https://imsfromseoul.tistory.com/ + https://camel-man-ims.tistory.com/

0개의 댓글