Skip to content

Commit 0708445

Browse files
authored
Merge pull request #10208 from geoand/spring-data-primitive
Fix issue with primitive id types in Spring Data JPA
2 parents 9c8c2d9 + 3e4c59c commit 0708445

File tree

3 files changed

+12
-5
lines changed
  • extensions/spring-data-jpa/deployment/src/main/java/io/quarkus/spring/data/deployment/generate
  • integration-tests/spring-data-jpa/src/main/java/io/quarkus/it/spring/data/jpa

3 files changed

+12
-5
lines changed

extensions/spring-data-jpa/deployment/src/main/java/io/quarkus/spring/data/deployment/generate/StockMethodsAdder.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ private void generateSave(ClassCreator classCreator, FieldDescriptor entityClass
134134
BytecodeCreator idValueUnset;
135135
BytecodeCreator idValueSet;
136136
if (idType instanceof PrimitiveType) {
137+
if (!idType.name().equals(DotNames.PRIMITIVE_LONG)
138+
&& !idType.name().equals(DotNames.PRIMITIVE_INTEGER)) {
139+
throw new IllegalArgumentException("Id type of '" + entityDotName + "' is invalid.");
140+
}
141+
if (idType.name().equals(DotNames.PRIMITIVE_LONG)) {
142+
idValue = save.checkCast(idValue, int.class);
143+
}
137144
BranchResult idValueNonZeroBranch = save.ifNonZero(idValue);
138145
idValueSet = idValueNonZeroBranch.trueBranch();
139146
idValueUnset = idValueNonZeroBranch.falseBranch();

integration-tests/spring-data-jpa/src/main/java/io/quarkus/it/spring/data/jpa/Book.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@Entity
77
public class Book extends NamedEntity {
88

9-
private Integer bid;
9+
private int bid;
1010

1111
private Integer publicationYear;
1212

@@ -20,11 +20,11 @@ public Book(Integer bid, String name, Integer publicationYear) {
2020
}
2121

2222
@Id
23-
public Integer getBid() {
23+
public int getBid() {
2424
return bid;
2525
}
2626

27-
public void setBid(Integer bid) {
27+
public void setBid(int bid) {
2828
this.bid = bid;
2929
}
3030

integration-tests/spring-data-jpa/src/main/java/io/quarkus/it/spring/data/jpa/Cat.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class Cat {
99

1010
@Id
1111
@GeneratedValue
12-
public Long id;
12+
public long id;
1313

1414
private String breed;
1515

@@ -25,7 +25,7 @@ public Cat(String breed, String color) {
2525
this.color = color;
2626
}
2727

28-
public Long getId() {
28+
public long getId() {
2929
return id;
3030
}
3131

0 commit comments

Comments
 (0)