Using @SuperBuilder and @With annotations

Skip to first unread message

Mike Roberts

Jul 22, 2021, 11:03:38 AM7/22/21
to Project Lombok

I have a db model class that extends from a base class so we are using SuperBuilder on it. The model has fields with @Id and @Version annotations, which should be filled by MongoDB on insert.  Since we're trying to keep the models immutable, everything is final. The right thing seems to be to add the @With annotation so Mongo can update the fields (create a copy with the id/version fields set). Using both @With and @SuperBuilder causes build errors. I've tried putting @With on the class and on the individual fields, with same error resulting.

Anyone got any ideas how to make this work? Our current (undesirable) workaround is to just make the fields mutable.

Thanks in advance,

Example class

@EqualsAndHashCode(callSuper = true)
@SuperBuilder(toBuilder = true)
@Document(collection = "examples")
public class ExampleDocument extends CommonDataModel {
    @Id private final ObjectId id;
    @Nonnull private final String field1;
    @Nonnull private final String field2;
    @Version private final Long version;

Example error:

error: constructor ExampleDocument in class ExampleDocument cannot be applied to given types;
    @Id private final ObjectId id;
  required: ExampleDocumentBuilder<?,?>
  found: ObjectId,String,String,Long
  reason: actual and formal argument lists differ in length

Floris Kraak

Jul 22, 2021, 11:29:57 AM7/22/21

One workaround I would suggest is simply making the base class an '@Embeddable' annotated class, embed it as a '@Embedded' annotated field, and stop using inheritance.

Simply not having things inherit if not needed makes a lot of things a lot simpler. By a surprising amount.

You received this message because you are subscribed to the Google Groups "Project Lombok" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit
Reply all
Reply to author
0 new messages