[SpringBoot/JPA] No default constructor for entity

삽질/트러블슈팅 2023. 5. 19. 22:08
728x90
728x90

에러 메세지

No default constructor for entity

 

 

 

 

원인

기본 생성자가 없다는 메세지

JPA를 사용하기 위해 default construct가 필요하다.

 

 

 

 

 

해결

1. @Builder 사용하지 않기

  - 빌더 패턴을 사용할 것이기 때문에 나의 경우에는 부적합함.

 

 

2. @AllArgsConstruct, @NoArgsConstruct 사용

  - @AllArgsConstruct : 모든 필드값을 파라미터에 넣은 기본 생성자를 생성해주는 애너테이션

  - @NoArgsConstruct : 파라미터가 없는 생성자를 만들어주는 애너테이션

 

@Build 애너테이션은 생성자를 생성하지 않으며 생성자 유무에 따라 다음과 같이 작동한다.
생성자가 없는 경우 : 모든 멤버 변수를 파라미터로 받는 기본 생성자 생성
생성자가 있을 경우 : 따로 생성자를 생성하지 않음
생성자 애너테이션과 같이 사용할 경우 빌더는 생성자이거나 메서드여야 한다.

 

 

경우에 따라 다양하게 해결할 수 있는데, 나의 경우 빌더 패턴을 활용할 것이기 때문에, 아래와 같이 해결했다.

 

 

@Data
@NoArgsConstructor
@Table(name = "User")
@Entity
public class UserEntity {
	
	@Builder
	public UserEntity(Long idx, String id, String pw, String nn, String email, String phone,
			String onoff, Integer cash, Date create_date, Date update_date) {
		this.idx = idx;
		this.id = id;
		this.pw = pw;
		this.nn = nn;
		this.email = email;
		this.phone = phone;
		this.onoff = onoff;
		this.cash = cash;
		this.create_date = create_date;
		this.update_date = update_date;
	}

 

 

조금 더 깊게 알게된다면 포스팅을 새로하던, 수정일자를 작성하고 첨삭을 하도록 해야겠다.

명확하게 100% 설명불가

 

728x90
300x250
mag1c

mag1c

2년차 주니어 개발자.

방명록