Using @SuperBuilder and @With annotations

112 views
Skip to first unread message

Mike Roberts

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

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,
Mike

Example class

@Getter
@EqualsAndHashCode(callSuper = true)
@SuperBuilder(toBuilder = true)
@Document(collection = "examples")
@With
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

unread,
Jul 22, 2021, 11:29:57 AM7/22/21
to project...@googlegroups.com

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 project-lombo...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/project-lombok/9f85a210-0b31-4933-aeab-8c45f8bc7039n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages