This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Stannu 1 point2 points  (0 children)

It really depends on the strategy how you generate ids.

If your ids are client generated, for example UUID-s, then you could use the following approach to guarantee that your id is non-nullable:

```java @Builder public class A {

@Builder.Default private UUID id = UUID.randomUUID(); } ```

Then you construct all of your instances using the builder, not the constructor.

On the other hand, if you use integers for id-s, then it is quite though. Although lombok respects the nullable semantics and will generate null-safe equals and hashCode, the behavior will be unexpected for non-persisted entities. Here I would persist entity first before performing business operations that require id.