Class 'Product' should have [public, protected] no-arg constructor
Нужна ваша помощь. У меня есть класс Product. К нему есть привязана аннотация @AllArgsConstrucrot. У меня есть показывается ошибка: Class 'Product' should have [public, protected] no-arg constructor.Данная аннотация не имеет решать эту проблему, или я ошибаюсь? В чем ошибка? Спасибо
package com.example_dmytrii_cherniak.java_application.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import javax.persistence.*;
@AllArgsConstructor
@Data
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(nullable = false, length = 99)
private String title;
private int price;
}
Ответы (2 шт):
@AllArgsConstructor - генерирует конструктор со всеми возможными аргументами @NoArgsConstructor - генерирует конструктор без аргументов
Добавь @NoArgsConstructor
Советую почитать документацию к ломбоку: https://projectlombok.org/features/constructor
В спецификации JPA п. 37.1.1 (https://docs.oracle.com/javaee/7/tutorial/persistence-intro001.htm#BNBQA) среди прочего указано
The class must have a public or protected, no-argument constructor. The class may have other constructors.
Т. е. сущность ОБЯЗАНА иметь конструктор без аргументов. Указывая @AllArgsConstructor, Вы оставляете единственный конструктор со всеми аргументами, т.е. в Вашем случае
public Product(int id, String title, int price) {
this.id = id;
this.title = title;
this.price = price;
}
Как указал Gleb Kuznetsov - выходом станет добавление классу аннотации @NoArgsConstructor