티스토리 뷰

728x90

Entity를 생성할때 @Embbeded를 통해서 코드를 생성하는 경우가 빈번한데

Embeddable로 선언된 Class 내에 있는 모든 값이 전부 null일 경우에 Class 자체가 null이 되어버려서

해당 클래스를 사용하려고 할때 NullPointerException이 발생해버린다.

 

일단 문제를 해결하기 전에 ORM의 기본 스펙이 Embeddable은 null이 되어서는 안 된다는 것이다.

만약 원칙을 반드시 지키면서 하고자 한다면 설계를 다시 확인해보는 것이 좋을 것같다.

 

해결 방법

해결 방법은 크게 2가지가 있다.

 

절대 null일 수가 없는 값을 미리 만들어 넣는 방법

 

예를 들어 생성되는 날짜가 Embeddable 내에 반드시 포함되게 하여 최소한 값 하나는 null이 되지 않도록 하는 방법을 이용해서 하는 방법이 있을 수 있고

 

@Column(updatable = false)
@CreatedDate
private LocalDateTime createdDate;

 

더미 데이터를 Embeddable 내에 포함되게 하여 null이 되지 않도록 하는 방법이 있다.

@Formula("0")
private int dummy;

 

설정을 바꾸어 null이 허용될 수 있게 하는 방법

 

많은 사람들이 같은 문제를 겪어서인지 이에 대한 논의가 꽤 활발했었다.

 

https://hibernate.atlassian.net/browse/HHH-7610

 

[HHH-7610] - Hibernate JIRA

회사에서 관리하는 프로젝트에 참여하고 있습니다

hibernate.atlassian.net

 

그래서 결국 Hibernate 구현체에서 null관련하여 설정을 변경할 수 있게 2016년의 5.1패치로 바뀌었다.

 

@Embeddables and all null column values

Historically Hibernate would always treat all null column values for an @Embeddable to mean that the @Embeddable should itself be null. 5.1 allows applications to dictate that Hibernate should instead use an empty @Embeddable instance. This is achieved via an opt-in setting: hibernate.create_empty_composites.enabled.
See HHH-7610 for details.

 

https://in.relation.to/2016/02/10/hibernate-orm-510-final-release/

 

ORM 5.1 feature release - In Relation To

The Hibernate team is proud to announce the release of ORM 5.1 which includes a number of new features and enhancements, including:

in.relation.to

 

 

그래서 어떻게 하느냐??

 

application.properties에 아래 설정을 적용하면 끝이다.

spring.jpa.properties.hibernate.create_empty_composites.enabled=true

yml, manifest 어떤걸 사용해도 가능하겠지만 문법의 형태는 당연히 바뀌어야 할 것이다.

 

문제 해결!

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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 29 30 31
글 보관함