ebean icon indicating copy to clipboard operation
ebean copied to clipboard

lombok @SuperBuilder is not compatible

Open lvjj opened this issue 1 year ago • 8 comments

The List of the Ebean 15.10.0 @OneToMany field needs to be initialized, but the Lombok @SuperBuilder initialization field is not recognized by Ebean,

lvjj avatar Apr 08 '25 07:04 lvjj

You will have to be more explicit.

What is an example of using @SuprtBuilder on an entity bean? What is the bytecode that is produced by Lombok for that? What does "the lombok @SuperBuilder initialization field is not recognized by Ebean" mean?

rbygrave avatar Apr 08 '25 10:04 rbygrave

@MappedSuperclass
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class BaseModel extends Model implements java.io.Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @WhenCreated
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime createTime;

    @WhenModified
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime updateTime;

    protected BaseModel(BaseModelBuilder<?, ?> b) {
        this.id = b.id;
        this.createTime = b.createTime;
        this.updateTime = b.updateTime;
    }

    public static BaseModelBuilder<?, ?> builder() {
        return new BaseModelBuilderImpl();
    }

    public static abstract class BaseModelBuilder<C extends BaseModel, B extends BaseModelBuilder<C, B>> {
        private Long id;
        private LocalDateTime createTime;
        private LocalDateTime updateTime;

        public B id(Long id) {
            this.id = id;
            return self();
        }

        public B createTime(LocalDateTime createTime) {
            this.createTime = createTime;
            return self();
        }

        public B updateTime(LocalDateTime updateTime) {
            this.updateTime = updateTime;
            return self();
        }

        protected abstract B self();

        public abstract C build();

        public String toString() {
            return "BaseModel.BaseModelBuilder(super=" + super.toString() + ", id=" + this.id + ", createTime=" + this.createTime + ", updateTime=" + this.updateTime + ")";
        }
    }

    private static final class BaseModelBuilderImpl extends BaseModelBuilder<BaseModel, BaseModelBuilderImpl> {
        private BaseModelBuilderImpl() {
        }

        protected BaseModelBuilderImpl self() {
            return this;
        }

        public BaseModel build() {
            return new BaseModel(this);
        }
    }
}
@Entity
@Getter
@Setter
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "stock")
public class Stock extends BaseModel implements Serializable {

    private String name;
    private String code;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "stock_app",
            joinColumns = {@JoinColumn(name = "stock_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "app_id", referencedColumnName = "id")})
    private List<App> apps;

}

Stock.class


@Entity
@Table(
    name = "stock"
)
public class Stock extends BaseModel implements Serializable {
    private String name;
    private String code;
    @ManyToMany(
        fetch = FetchType.LAZY
    )
    @JoinTable(
        name = "stock_app",
        joinColumns = {@JoinColumn(
    name = "stock_id",
    referencedColumnName = "id"
)},
        inverseJoinColumns = {@JoinColumn(
    name = "app_id",
    referencedColumnName = "id"
)}
    )
    private List<App> apps;

    @Generated
    protected Stock(final StockBuilder<?, ?> b) {
        super(b);
        this.name = b.name;
        this.code = b.code;
        this.apps = b.apps;
    }

    @Generated
    public static StockBuilder<?, ?> builder() {
        return new StockBuilderImpl();
    }

    @Generated
    public String getName() {
        return this.name;
    }

    @Generated
    public String getCode() {
        return this.code;
    }

    @Generated
    public List<App> getApps() {
        return this.apps;
    }

    @Generated
    public void setName(final String name) {
        this.name = name;
    }

    @Generated
    public void setCode(final String code) {
        this.code = code;
    }

    @Generated
    public void setApps(final List<App> apps) {
        this.apps = apps;
    }

    @Generated
    public Stock() {
    }

    @Generated
    public Stock(final String name, final String code, final List<App> apps) {
        this.name = name;
        this.code = code;
        this.apps = apps;
    }

    @Generated
    public abstract static class StockBuilder<C extends Stock, B extends StockBuilder<C, B>> extends BaseModel.BaseModelBuilder<C, B> {
        @Generated
        private String name;
        @Generated
        private String code;
        @Generated
        private List<App> apps;

        public StockBuilder() {
        }

        @Generated
        public B name(final String name) {
            this.name = name;
            return (B)this.self();
        }

        @Generated
        public B code(final String code) {
            this.code = code;
            return (B)this.self();
        }

        @Generated
        public B apps(final List<App> apps) {
            this.apps = apps;
            return (B)this.self();
        }

        @Generated
        protected abstract B self();

        @Generated
        public abstract C build();

        @Generated
        public String toString() {
            String var10000 = super.toString();
            return "Stock.StockBuilder(super=" + var10000 + ", name=" + this.name + ", code=" + this.code + ", apps=" + String.valueOf(this.apps) + ")";
        }
    }

    @Generated
    private static final class StockBuilderImpl extends StockBuilder<Stock, StockBuilderImpl> {
        @Generated
        private StockBuilderImpl() {
        }

        @Generated
        protected StockBuilderImpl self() {
            return this;
        }

        @Generated
        public Stock build() {
            return new Stock(this);
        }
    }
}

ebean-enhance> cls: invest/entity/Stock msg: Transform error ERROR: Unsupported initialisation of @OneToMany or @ManyToMany on: invest/entity/Stock fields: [apps] Refer: https://ebean.io/docs/trouble-shooting#initialisation-error

lvjj avatar Apr 08 '25 15:04 lvjj

@rbygrave Thanks for devoting time to this issue.

lvjj avatar Apr 09 '25 07:04 lvjj

This is effectively a bug in ebean-agent. Refer https://github.com/ebean-orm/ebean-agent/pull/224

rbygrave avatar Apr 09 '25 10:04 rbygrave

This is fixed in the ebean-agent / ebean-maven-plugin / ebean-gradle-plugin version 14.12.0 and 15.12.0

An updated ebean IntelliJ IDEA plugin has been published, that usually takes 2 days to be approved and then publicly available. To use the updated ebean-agent before then.

  • Download the ebean-agent 15.12.0 jar from https://repo1.maven.org/maven2/io/ebean/ebean-agent/15.12.0/ebean-agent-15.12.0.jar
  • mkdir ~/.ebean
  • Move the ebean-agent jar into ~/.ebean
  • Toggle OFF the ebean plugin
  • Toggle ON the ebean plugin (this will then look for an ebean agent in ~/.ebean and use one if it is found)

Running the application in IntelliJ will then use the ebean agent from ~/.ebean

rbygrave avatar Apr 09 '25 11:04 rbygrave

PR to bump the ebean-agent - https://github.com/ebean-orm/ebean/pull/3620

rbygrave avatar Apr 09 '25 11:04 rbygrave

Let us know if you can confirm that the lastest ebean-agent fixes the issue. Thanks Rob.

rbygrave avatar Apr 09 '25 12:04 rbygrave

i replace ebean-agent-15.20.0.jar => idea/plugins/ebean-idea/lib/ebean-agent-15.10.0.jar, it's worked, Thinks rgygrave

lvjj avatar Apr 10 '25 02:04 lvjj