Lombok的SuperBuilder继承

huangapple go评论91阅读模式
英文:

Lombok SuperBuilder Inheritance

问题

我有一个名为 BaseEntity 的类:

Lombok的SuperBuilder继承

还有一个扩展类:

Lombok的SuperBuilder继承

我正在尝试使用构建器来构建一个种子:

Lombok的SuperBuilder继承

我希望 Lombok 在 BreedEntity 中创建一个构造函数,其中包含基类 baseEntity。

我阅读了文档,当我删除 @Entity 注解时,它能够正常工作。
SuperBuilder 文档

有人可以更详细地解释一下为什么会发生这种情况吗?

英文:

I've a class BaseEntity:

Lombok的SuperBuilder继承

and an extended class:

Lombok的SuperBuilder继承

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

Lombok的SuperBuilder继承

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?

答案1

得分: 1

根据错误信息,实体类必须有一个公共的无参构造函数。

Spring库是按照这种方式设计的。假设你执行一个查询 BreedRepo.findById(...),以下是发生的事情:

  • Hibernate访问数据库驱动程序并获取查询结果。
  • 创建一个新的 BreedEntity 类实例(需要无参构造函数)。
  • 然后使用setter方法设置在 BreedEntity 中注册的所有列(你还需要为每个 @Column 制作setter方法)。

结论: Lombok的构建器与Spring JPA不兼容。改用 @Data 注解。

你将不得不以不太酷的方式完成这个操作,即 new 然后 setXsetY...

英文:

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

答案2

得分: 1

我建议为Hibernate添加一个包私有构造函数,然后你几乎可以实现所需的功能。

@NoArgsConstructor(access = AccessLevel.PACKAGE)
BreedEntity
英文:

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

@NoArgsConstructor(access = AccessLevel.PACKAGE)
BreedEntity

答案3

得分: 1

尝试这个方式:

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


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

在我的情况下,这是有效的。如果出于某些原因对你不起作用,你可以调查这些 示例 并理解如何修复你的问题。

英文:

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.

huangapple
  • 本文由 发表于 2020年4月6日 23:27:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/61063263.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定