【发布时间】:2020-03-11 18:53:35
【问题描述】:
我有一个如下所示的 Employer 类:
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
public class Employer {
private @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "employer")
private List<Offer> offerList = new ArrayList<>();
private String name;
private String location;
private String description;
private int companySize;
public Employer(String name, String location, String description) {
this.name = name;
this.location = location;
this.description = description;
}
}
在 localhost:8080/employers 上发送 GET 请求会得到我:
ERROR 6154 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: No default constructor for entity: : com.krdkta.internship_for_you.model.Employer; nested exception is org.hibernate.InstantiationException: No default constructor for entity: : com.krdkta.internship_for_you.model.Employer] with root cause
即使显然有一个 Lombok Annotation 定义了这个无参数构造函数,但我仍然得到这个错误。将 Lombok 与 Hibernate 一起使用是否有任何禁忌症:)?
【问题讨论】:
-
这不是很奇怪。要回答您的问题,不,将 Lombok 与 Hibernate 一起使用没有任何禁忌症,我在我的所有实体上都使用它,其中一些完全按照您在此处使用它们的方式进行了定义。由于它显然没有按您期望的方式工作,我将首先编写一个测试来简单地验证无参数构造函数是否存在。如果没有,那么您可能需要将 lombok 注释处理器添加到您的 pom 中(假设您使用的是 maven)。
-
你确定 Lombok 真的在运行吗?它可能需要一些设置,包括安装 IDE 插件。
-
显然解决方案是从终端运行:mvn clean 然后 mvn spring-boot:run,但我真的不知道为什么。
-
如果您使用 Gradle,请在此处考虑我的回答:stackoverflow.com/questions/55304021/…
标签: java spring hibernate spring-boot lombok