1

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 :)?

krdkta
  • 21
  • 1
  • 5
  • 2
    Well isn't that peculiar. To answer your question, no, there are no contraindications to using Lombok with Hibernate, I use it on all of my entities, some defined exactly the way you have them here. Since it's obviously not working the way you expect, I would start by writing a test that simply verifies that the no argument constructor exists. If it does not, then you probably need to add the lombok annotation processor to your pom (assuming you're using maven). – lane.maxwell Mar 11 '20 at 19:29
  • Are you sure that Lombok is actually getting _run_? It can require a bit of setup, including installing IDE plugins. – chrylis -cautiouslyoptimistic- Mar 11 '20 at 20:08
  • Apparently the solution is to run from terminal: mvn clean and then mvn spring-boot:run, but I don't really know why. – krdkta Mar 20 '20 at 23:19
  • If you are using Gradle please consider my answer here: https://stackoverflow.com/questions/55304021/groovy-lombok-noargsconstructor-not-creating-default-constructor/71304755#71304755 – SonjaJan Mar 01 '22 at 15:11

3 Answers3

2

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.

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • Enabling anotation processing didn't give any effect :/ I do have lombok installed as plugin in Intellij Idea. It is also included as dependency in pom.xml. Furthermore, Lombok is added through Global Libraries <- I downloaded lombok.jar and put it in one of the folders in my project and link it up. – krdkta Mar 14 '20 at 12:25
  • If you have intellij set up, use delombok (available in UI) and make sure that the no-args public constructor was indeed generated – Mark Bramnik Mar 14 '20 at 12:53
-1

Can you please change the order of the annotations like this?


@Builder
@Data
@Entity
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor
public class Employer {
reflexdemon
  • 836
  • 8
  • 21
  • 3
    The order of the annotations does not matter. Plus he didn't use the `@Builder` annotation. – Willem Mar 11 '20 at 19:12
-1

You can use it. This work for my project.

@Entity
@Table
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Employer {}
Dạ Nhật
  • 24
  • 1
  • 4