使用 lombok 定义继承类的默认值
因此,我正在开发一个通知服务,目前只有一个 Notification
和一个 Email
实体,其中 Email 扩展了Notification。
Notification
实体有一个名为 type
的列,它存储通知的类型(即:EMAIL、MESSAGE、PUSH 等),我想知道是否有办法使用 lombok 时,为 Notification
的每个子项的 type
列定义默认值。
我发现一种常见的做法是在构造函数上设置类型,如下所示:
public Email() {
setType("EMAIL");
}
但我正在使用 lombok 的构建器来实例化 Email
,如下所示:
public static Email fromEmailRequest(final EmailRequest emailRequest) {
if (Objects.isNull(emailRequest)) {
return Email.builder().build();
}
Set<String> set = new LinkedHashSet<>(emailRequest.getRecipients());
return Email.builder()
.content(emailRequest.getContent())
.recipients(Lists.newArrayList(set))
.sender(emailRequest.getSender())
.subject(emailRequest.getSubject())
.type(NotificationTypeEnum.EMAIL)
.build();
}
我现在使用的方法将类型设置为请求 DTO 映射器,但我认为这个逻辑应该位于实体中的某个位置。
So I am developing a notification service which, for now, only has a Notification
and an Email
entity, where Email extends Notification.
The Notification
entity has a column named type
which stores the type of the notification (ie: EMAIL, MESSAGE, PUSH, etc) and I want to know if there is a way to define a default value for the type
column for each of the childs of Notification
while using lombok.
I see that a common practice is to set the type on the constructor, like this:
public Email() {
setType("EMAIL");
}
But I am using the builder from lombok to instatiate Email
, like this:
public static Email fromEmailRequest(final EmailRequest emailRequest) {
if (Objects.isNull(emailRequest)) {
return Email.builder().build();
}
Set<String> set = new LinkedHashSet<>(emailRequest.getRecipients());
return Email.builder()
.content(emailRequest.getContent())
.recipients(Lists.newArrayList(set))
.sender(emailRequest.getSender())
.subject(emailRequest.getSubject())
.type(NotificationTypeEnum.EMAIL)
.build();
}
The approach I am using now sets the type in the request DTO mapper, but I think this logic should be somewhere in the entities.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许有更好的方法,但您可以在
Notification
类中创建一个以类型作为参数的构造函数,然后为您的Email
类手动创建一个全参数构造函数。 Lombok 将使用此构造函数作为构建器,而不是生成一个。@SuperBuilder
使用起来有点复杂,但想法是相同的。您仍然可以自己编写通常由 lombok 生成的部分代码,并使用您自己的代码更改其行为。因此,您自己实现
build
方法并将type
设置为EMAIL
。Maybe there is a better way, but you can create a constructor inside your
Notification
class which has the type as parameter and then manually create an all args constructor for yourEmail
class. Lombok will use this constructor for the builder instead of generating one.@SuperBuilder
is a bit more complex to work with, but the idea is the same. You can still write parts of the code that is normally generated by lombok yourself and change its behavior with your own code.So you implement the
build
method yourself and set thetype
toEMAIL
.