Lombok SuperBuilder Inheritance

3.1k Views Asked by At

I've a class BaseEntity:

enter image description here

and an extended class:

enter image description here

I'm trying to build a seeder using the builder:

enter image description here

I want Lombok to create a constructor in the BreedEntity with the base class baseEntity.

I read the documentation and it works just fine when I delete the @entity anotation SuperBuilder Docs

Can someone explain in more detail why this is happening?

3

There are 3 best solutions below

0
On

try this way

@Getter
@MappedSuperclass
@SuperBuilder(toBuilder = true)
public abstract class BaseEntity

and

@Getter
@Entity
@Table(...)
@NoArgsConstructor
@SuperBuilder(toBuilder = true)
public class BreedEntity extends BaseEntity

it works in my case. If it does not work for you for some reason. You can investigate these examples and understand how to fix your issue.

2
On

As the error says, There must be a public no-argument constructors for Entity.

Spring library is designed that way. Let's say you make a query BreedRepo.findById(...), following things happen

  • Hibernate accesses the database driver and get the query result.
  • A new class instance of BreedEntity is created. (You need the no arg constructor for this)
  • Then all the cloumns registerd in BreedEntiry are set using the setter methods. (You also need to make the setter methods for each @Column)

Conclusion: Lombok builder is not compatible with Spring JPA. Use @Data instead

You will have to do this in not so cool looking way, new then setX, setY ...

0
On

I would suggest to add a package private constructor for Hibernate, then you can almost achieve the desired functionality.

@NoArgsConstructor(access = AccessLevel.PACKAGE)
BreedEntity