简体   繁体   中英

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:

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. Is there any contraindications to using Lombok with Hibernate :)?

Lombok is invoked during the compilation (its and annotation processor technically). Whereas Hibernate is used much later in runtime.

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.

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. However, if you're using lombok with IDE (like, running the main class from within your ide) you might need to do additional steps:

For example, in IntelliJ you should go Settings -> Compiler -> Annotation Processors and enable the anotation post processing. This question was already asked here so I don't have much to add.

Its also good to install lombok plugin. It has a "delombok" integration so that you could see what exactly has been generated by lombok right from within your IDE.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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