[JPA] @EmbeddedId 사용법

JPA Entity를 구현하려면 다음 규칙을 지켜져야한다.

  1. 모든 Entity 클래스는 @Id 설정이 필요하다.

    • 이때 @Id로 설정된 값에는 null값이 올 수 없다.

  2. 기본키가 복합키인 경우에는 @EmbeddedId 혹은 @IdClass를 사용한다.

  3. 테이블에 Primary Key가 없는 경우가 있을 수 있다. 이 경우에는 Unique한 컬럼을 기준으로 @Id annotation을 붙이면된다.


pk를 설정하지 않으려했지만 1번의 이유로 복합키를 아래와 같은 방식으로 적용함.


3.1. @Embeddable

@Embeddable
public class BookId implements Serializable {

    private String author;
    private String name;

    // standard getters and setters
}

3.2. @Entity and @EmbeddedId

@Entity
public class Book {

    @EmbeddedId
    private BookId id;
    private String genre;
    private Integer price;

    //standard getters and setters 

} 


<참고>

https://www.baeldung.com/spring-jpa-embedded-method-parameters

댓글

이 블로그의 인기 게시물

[Eclipse] publishes and runs j2ee and java ee web projects and server configurations to a local tomcat server