본문 바로가기
Dev/spring

Learn Spring 5( Boot 2, JPA, Thymeleaf, AOP...) - 18. @OneToMany and @ManyToOne Annotations for Relating Entities

by RoundRyun 2019. 12. 18.

* 이글은 Udemy의 https://www.udemy.com/course/spring-framework-web-development-2020/

불러오는 중입니다...

강좌를 듣고, 스스로 공부한 내용을 정리하기 위해 적는 글입니다. 정확하지 않거나 부분 부분 연결되지 않는 정보들이 있을수 있습니다.

 

 

 we've got a relationship between the employee table and the projects table. Right now when this application does not support that. So let's say we had to build that relationship. What would we have to do?

 

Well first things first, notice the table. These two tables have no foreign key relationship between each other.(현재까지는)(employee테이블이랑 project테이블) So we'd need to form some kind of a foreign key relationship. So if we want to assign an employee a given project, we'd of course have to have a project ID in the Employee table as a foreign key. So that's a one to many relationship between project and employee.(여기까지가 포린키 적용하는 일반적인 내용)

 

One project could be assigned to many employees.(오늘 우리가@OneTOMany @ManyToOne을 통해 증명하려는 문장) . So we'd have a project I.D. inside of the employee tables of the project, it would be the foreign key in the Employee table to build that one sided relationship. One to Many. One project could be assigned to many employees.

 

So how would we build that sort of relationship? Well spring data makes things very easy in the actual code

 

 

This is the employee Entity class right. And so because we made this an @entity, spring data was able to use hibernate to build out these tables in the actual database based on this class definition. So it created the first name, last name, email column, employee I.D. column. It took that information from this and created a table in the database.( @entity를 적용한 것만으로, 스프링데이터에서 자체적으로 하이버네이트를 작동하여 하나의 테이블로 인식하고 생성해줬다는 말)

 

So a similar thing was done for projects so let's go the project entity.

But we have not created a field for employees in project and that is why that relationship does not even exist in the actual database table. But let's say we needed to form that relationship. Let's say a particular project could be assigned to many employees. How would we form that relationship? Well we'd first need a list of employees right? So I could create a field.

 

private List employees;

 

So now we have a particular property (저 employees 변수) here that is going to represent a one to many relationship. one project maps to many employees for this particular project object that will be created will have many employees assigned. So this would be a one to many relationship.

 

So to make this relationship official, so that we can have many employees assigned to a given project, we actually have to give this property a particular annotation and to let the spring data hibernate JPA know that this is actually a relationship that had this this is not just some property it actually has a relationship in the database that we need to maintain. And so for that we can use as a an annotation for @one too many.

 

 

@one too many : So what is this saying this is saying that one project can have many employees assigned to it. 

 

그런데 이렇게만 하면 하이버네이트에서 브랜드 뉴 테이블을 생성해벌임. 그래서 localhost:8080/h2-console접속후 로그인해서 테이블보면 새로운 테이블이 생긴것을 확인할 수 있음... 음, 그렇게 하기보다는 아래에 OneToMany컬럼에서 mapperby 아규먼트 값을 설정해 줌으로서써 포린키기능을 적용하는게 더 낫다.

 

 Project 클래스에 프라이빗 변수로 employees 리스트를 설정한다음,  One to many적용해서 이제 많은 employee들이 이 프로젝트 하나에 할당될수있다는 표시를 할 수 있음.

 

제대로 적용하기 위해선 Employee 클래스 역시, @ManyToOne이라는 어노테이션을 지정해 두어야 함. 아래와 같이.

이 떄, Employee class 안의 @ManyToOne과 @JoinColumn(project_id라는 새로운 컬럼으로 인식하고 조인시킬) 어노테이션을 받아둔,

private project theProject 변수. 이 theProject의 이름은 Project class 의  employees 리스트변수에 설정해둔 @OneToMany어노테이션의 mappedby 아규먼트 값과 동일해야함. 아래와 같이.

자 여기까지 하고,  save  후, 다시 h2-console을 확인해보면,

 

EMPLOYEE와 PROJECT테이블에 각각 PROJECT_ID라는 컬럼이 생긴 것을 알 수 있음.

@OneTOMany @ManyToOne를 이용하여, Many employees could be assigned to the same project.(많은 employee들이 하나의 프로젝트에 할당될 수 있다) 를 배우고 적용한 오늘의 강의.

 

끝!