java 在实体上使用 lomboks @Data 和 @Builder

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40516058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:17:21  来源:igfitidea点击:

Using lomboks @Data and @Builder on entity

javalombok

提问by Jan Galinski

I am using the following:

我正在使用以下内容:

@Entity
@Data
@Builder
@NoArgsConstructor(force = true)
public class User {
    private String id;
    private String firstName;
    private String lastName;
}

what I want to achieve: for JPA usage, I need a POJO with noArgConstructor, getters/setters and equals/hashCode/toString.

我想要实现的目标:对于 JPA 使用,我需要一个带有 noArgConstructor、getter/setter 和 equals/hashCode/toString 的 POJO。

For instance creation (for example in tests) I want to use User.builder().build();

例如创建(例如在测试中)我想使用 User.builder().build();

Problem: it does not compile, there seems to be an issue with the NoArgConstructor vs. RequiredFieldsConstructor:

问题:它无法编译,NoArgConstructor 与 RequiredFieldsConstructor 似乎存在问题:

Error:(15, 1) java: constructor User in class x.y.z.User cannot be applied to given types;
required: no arguments
found:    java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String
reason: actual and formal argument lists differ in length

Update: The error occurs when I try to create a new entity via new... the builder() works.

更新:当我尝试通过new... builder() 工作创建新实体时发生错误。

What do I miss? Isn't it possible to use @Data, @Entity and @Builder at the same time?

我想念什么?不能同时使用@Data、@Entity 和@Builder 吗?

采纳答案by Jan Galinski

I will answer my own question by summerizing the comments.

我将通过总结评论来回答我自己的问题。

First of all, thanks to @RoelSpilker, you can use Builder and Data together on one Pojo if you explicitly provide AllArgs- and NoArgs- constructors:

首先,感谢@RoelSpilker,如果您明确提供 AllArgs- 和 NoArgs- 构造函数,您可以在一个 Pojo 上同时使用 Builder 和 Data:

 @RequiredArgsConstructor
 @NoArgsConstructor
 @Data
 @Builder
 public class Person {...}

But: the builder created for this class will not know any inherited fields. For my use case (having some AbstractEntities) this renders the solution useless and I will stick with manual helpers/builders for now.

但是:为这个类创建的构建器不会知道任何继承的字段。对于我的用例(有一些 AbstractEntities),这使得解决方案毫无用处,我现在将坚持使用手动助手/构建器。

回答by ???

try this code with lombok version 1.16.18 over :

在 lombok 版本 1.16.18 上尝试此代码:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
public class User {
    private String id;
    private String firstName;
    private String lastName;
}

回答by Lubo

Beware of that data objects aren't entities! Simply put, there is problem with hashcode/equals (when it considers id fields) and also toString method with lazy loaded parts of entity. For reference you can check Vlad Mihalceas article.

当心数据对象不是实体!简而言之,hashcode/equals(当它考虑 id 字段时)和 toString 方法存在问题,并且实体的延迟加载部分。作为参考,您可以查看Vlad Mihalceas 文章

You should:

你应该:

  • exclude id fields from hashcode/equals
  • exclude association fields which are not managed in given entity from hashcode/equals
  • exclude all lazy loaded fields from toString method
  • exclude fields possibly causing circular references from toString method
  • 从 hashcode/equals 中排除 id 字段
  • 从 hashcode/equals 中排除给定实体中未管理的关联字段
  • 从 toString 方法中排除所有延迟加载的字段
  • 排除可能导致 toString 方法循环引用的字段

For sure read at least something on topic of how does JPA do "dirty checking" before beeing confident that your handwritten or generated equals/hashcode method is ok.

在确信您的手写或生成的 equals/hashcode 方法没有问题之前,一定要至少阅读一些关于 JPA 如何进行“脏检查”的主题。

回答by Xendar

In the Lombok documentation it is written: Person.builder().name("Adam Savage").city("San Francisco").job("Mythbusters").job("Unchained Reaction").build();

在 Lombok 文档中它是这样写的:Person.builder().name("Adam Savage").city("San Francisco").job("Mythbusters").job("Unchained Reaction").build();

Do you use this syntax for your purpose? According to your description it's not the case, and could explain the error you get?

您是否出于您的目的使用此语法?根据您的描述,情况并非如此,能否解释您遇到的错误?