简体   繁体   English

项目 Lombok + Hibernate 没有实体的默认构造函数

[英]Project Lombok + Hibernate No Default Constructor for Entity

I have an Employer class looking like this:我有一个看起来像这样的雇主类:

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;
  }
}

Sending GET request on localhost:8080/employers gets me:在 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

Even though there's clearly an Lombok Annotation that defines this no argument constructor, but still I get this error.即使显然有一个 Lombok Annotation 定义了这个无参数构造函数,但我仍然收到这个错误。 Is there any contraindications to using Lombok with Hibernate :)?将 Lombok 与 Hibernate 一起使用是否有任何禁忌症:)?

Lombok is invoked during the compilation (its and annotation processor technically). Lombok 在编译期间被调用(技术上是它和注释处理器)。 Whereas Hibernate is used much later in runtime.而 Hibernate 在运行时使用的时间要晚得多。

In fact from the standpoint of any runtime framework there is no difference between the no-arg constructor that was generated using lombok and "manually" created no-arg constructor.事实上,从任何运行时框架的角度来看,使用 lombok 生成的无参数构造函数和“手动”创建的无参数构造函数之间没有区别。

So to directly answer your question: No, there is no contradiction.所以直接回答你的问题:不,没有矛盾。

Now, I believe you use something like maven where you've added the lombok as a dependency and its supposed to be enough in case Maven compiles your classes.现在,我相信您使用了类似 Maven 的东西,其中您已将 lombok 添加为依赖项,并且在 Maven 编译您的类的情况下它应该足够了。 However, if you're using lombok with IDE (like, running the main class from within your ide) you might need to do additional steps:但是,如果您在 IDE 中使用 lombok(例如,从您的 IDE 中运行主类),您可能需要执行其他步骤:

For example, in IntelliJ you should go Settings -> Compiler -> Annotation Processors and enable the anotation post processing.例如,在 IntelliJ 中,您应该转到Settings -> Compiler -> Annotation Processors并启用 annotation post processing。 This question was already asked here so I don't have much to add.这个问题已经在这里过了,所以我没有太多要补充的。

Its also good to install lombok plugin.安装 lombok 插件也很好。 It has a "delombok" integration so that you could see what exactly has been generated by lombok right from within your IDE.它具有“delombok”集成,因此您可以直接从您的 IDE 中查看 lombok 究竟生成了什么。

Can you please change the order of the annotations like this?您能像这样更改注释的顺序吗?


@Builder
@Data
@Entity
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor
public class Employer {

You can use it.你可以使用它。 This work for my project.这项工作适用于我的项目。

@Entity
@Table
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Employer {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM