Skip to content

Commit c0612dc

Browse files
committed
Support Lombok SuperBuilder for - Unsupported initialisation of @onetomany or @manytomany ...
ERROR: Unsupported initialisation of @onetomany or @manytomany on: test/entity/User fields: [roles] Refer: https://ebean.io/docs/trouble-shooting#initialisation-error] The SuperBuilder creates a constructor that takes a builder rather than the fields, and assigned the OneToMany/ManyToMany/DbArray via the builder with a GETFIELD followed by PUTFIELD
1 parent 2553418 commit c0612dc

File tree

4 files changed

+164
-2
lines changed

4 files changed

+164
-2
lines changed

ebean-agent/src/main/java/io/ebean/enhance/entity/ConstructorDeferredCode.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ enum State {
4646
KT_CHECKCAST, // optional kotlin state
4747
KT_LABEL, // optional kotlin state
4848
EMPTY,
49+
GETFIELD,
4950
MAYBE_UNSUPPORTED
5051
}
5152

@@ -222,15 +223,18 @@ boolean consumeVisitFieldInsn(int opcode, String owner, String name, String desc
222223
}
223224
}
224225
flush();
226+
if (opcode == GETFIELD) {
227+
state = State.GETFIELD;
228+
}
225229
return false;
226230
}
227231

228232
/**
229233
* Return true when a OneToMany or ManyToMany is not initialised in a supported manor.
230234
*/
231235
private boolean unsupportedInitialisation() {
232-
if (state == State.ALOAD) {
233-
// allow constructor initialisation of a OneToMany
236+
if (state == State.ALOAD || state == State.GETFIELD) {
237+
// allow constructor initialisation of a OneToMany via constructor arg or builder
234238
return false;
235239
}
236240
return state == State.MAYBE_UNSUPPORTED

test/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@
9494
<scope>test</scope>
9595
</dependency>
9696

97+
<dependency>
98+
<groupId>org.projectlombok</groupId>
99+
<artifactId>lombok</artifactId>
100+
<version>1.18.38</version>
101+
</dependency>
97102
</dependencies>
98103

99104
<build>
@@ -104,6 +109,11 @@
104109
<version>3.11.0</version>
105110
<configuration>
106111
<annotationProcessorPaths>
112+
<path>
113+
<groupId>org.projectlombok</groupId>
114+
<artifactId>lombok</artifactId>
115+
<version>1.18.38</version>
116+
</path>
107117
<path>
108118
<groupId>io.ebean</groupId>
109119
<artifactId>querybean-generator</artifactId>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package test.model.lombok;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.Id;
6+
7+
@Entity
8+
public class App {
9+
10+
@Id
11+
long id;
12+
13+
@Column
14+
String name;
15+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package test.model.lombok;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.FetchType;
5+
import jakarta.persistence.ManyToMany;
6+
import jakarta.persistence.Table;
7+
import lombok.Generated;
8+
9+
import java.util.List;
10+
11+
@Entity
12+
@Table(name = "stock")
13+
public class Msock {
14+
15+
private String name;
16+
private String code;
17+
@ManyToMany(fetch = FetchType.LAZY)
18+
private List<App> myapps;
19+
20+
@Generated
21+
public Msock() {
22+
}
23+
24+
@Generated
25+
protected Msock(Msock.StockBuilder<?, ?> b) {
26+
this.name = b.name;
27+
this.code = b.code;
28+
this.myapps = b.apps;
29+
}
30+
31+
@Generated
32+
public Msock(String name, String code, List<App> apps) {
33+
this.name = name;
34+
this.code = code;
35+
this.myapps = apps;
36+
}
37+
38+
@Generated
39+
public static Msock.StockBuilder<?, ?> builder() {
40+
return new Msock.StockBuilderImpl();
41+
}
42+
43+
@Generated
44+
public String getName() {
45+
return this.name;
46+
}
47+
48+
@Generated
49+
public void setName(String name) {
50+
this.name = name;
51+
}
52+
53+
@Generated
54+
public String getCode() {
55+
return this.code;
56+
}
57+
58+
@Generated
59+
public void setCode(String code) {
60+
this.code = code;
61+
}
62+
63+
@Generated
64+
public List<App> getApps() {
65+
return this.myapps;
66+
}
67+
68+
@Generated
69+
public void setApps(List<App> myapps) {
70+
this.myapps = myapps;
71+
}
72+
73+
@Generated
74+
public abstract static class StockBuilder<C extends Msock, B extends Msock.StockBuilder<C, B>> {
75+
@Generated
76+
private String name;
77+
@Generated
78+
private String code;
79+
@Generated
80+
private List<App> apps;
81+
82+
public StockBuilder() {
83+
}
84+
85+
@Generated
86+
public B name(String name) {
87+
this.name = name;
88+
return this.self();
89+
}
90+
91+
@Generated
92+
public B code(String code) {
93+
this.code = code;
94+
return this.self();
95+
}
96+
97+
@Generated
98+
public B apps(List<App> apps) {
99+
this.apps = apps;
100+
return this.self();
101+
}
102+
103+
@Generated
104+
protected abstract B self();
105+
106+
@Generated
107+
public abstract C build();
108+
109+
@Generated
110+
public String toString() {
111+
String var10000 = this.name;
112+
return "Stock.StockBuilder(name=" + var10000 + ", code=" + this.code + ", apps=" + String.valueOf(this.apps) + ")";
113+
}
114+
}
115+
116+
@Generated
117+
private static final class StockBuilderImpl extends Msock.StockBuilder<Msock, Msock.StockBuilderImpl> {
118+
@Generated
119+
private StockBuilderImpl() {
120+
}
121+
122+
@Generated
123+
protected StockBuilderImpl self() {
124+
return this;
125+
}
126+
127+
@Generated
128+
public Msock build() {
129+
return new Msock(this);
130+
}
131+
}
132+
}
133+

0 commit comments

Comments
 (0)