2

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?

Rik Peeters
  • 31
  • 1
  • 6

3 Answers3

1

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 ...

Sudip Bhattarai
  • 1,082
  • 9
  • 23
  • No, Hibernate doesn't exactly work like this, it doesn't call the setters but uses reflection to set the fields. – Balázs Németh Apr 06 '20 at 15:55
  • 1
    I requested this as a new feature almost two years ago: https://jira.spring.io/browse/DATACMNS-1397. Unfortunately it never got a response from the maintainers. – Jan Rieke Apr 07 '20 at 11:10
1

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

@NoArgsConstructor(access = AccessLevel.PACKAGE)
BreedEntity
Balázs Németh
  • 6,222
  • 9
  • 45
  • 60
1

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.

serg
  • 29
  • 4