Skip to content

Commit

Permalink
Merge pull request quarkusio#33265 from geoand/quarkusio#31774
Browse files Browse the repository at this point in the history
Properly support extracting fields from entities into projections
  • Loading branch information
geoand authored May 11, 2023
2 parents 3a10b2e + 10ae12a commit 046c31d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ private void generateCustomResultTypes(DotName interfaceName, DotName implName,
throw new IllegalArgumentException("Method " + method.name() + " of interface " + interfaceName
+ " is not a getter method since it returns void");
}
DotName fieldTypeName = getPrimitiveTypeName(returnType.name());
DotName fieldTypeName = returnType.name();

FieldDescriptor field = implClassCreator.getFieldCreator(propertyName, fieldTypeName.toString())
.getFieldDescriptor();
Expand All @@ -363,7 +363,7 @@ private void generateCustomResultTypes(DotName interfaceName, DotName implName,
ResultHandle newObject = convert.newInstance(MethodDescriptor.ofConstructor(implName.toString()));

ResultHandle entity = convert.getMethodParam(0);
final List<MethodInfo> availableMethods = entityClassInfo.methods();
final List<MethodInfo> availableMethods = availableMethods(entityClassInfo, index);
for (Map.Entry<String, FieldDescriptor> field : fields.entrySet()) {
if (!getterExists(availableMethods, field.getKey())) {
throw new IllegalArgumentException(field.getKey() + " method does not exists in "
Expand All @@ -381,6 +381,21 @@ private void generateCustomResultTypes(DotName interfaceName, DotName implName,
}
}

private static List<MethodInfo> availableMethods(ClassInfo entityClassInfo, IndexView index) {
List<MethodInfo> result = new ArrayList<>(entityClassInfo.methods().size());
while (true) {
result.addAll(entityClassInfo.methods());
if (entityClassInfo.superName() == null) {
break;
}
entityClassInfo = index.getClassByName(entityClassInfo.superName());
if ((entityClassInfo == null) || DotNames.OBJECT.equals(entityClassInfo.name())) {
break;
}
}
return result;
}

private boolean getterExists(List<MethodInfo> methods, String getterName) {
for (MethodInfo method : methods) {
if (method.name().equals(getterName)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.quarkus.it.spring.data.jpa;

import java.time.ZonedDateTime;

import jakarta.persistence.MappedSuperclass;

@MappedSuperclass
public abstract class AbstractPost {
private ZonedDateTime posted;

public ZonedDateTime getPosted() {
return posted;
}

public void setPosted(ZonedDateTime postedAt) {
this.posted = postedAt;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.quarkus.it.spring.data.jpa;

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -18,7 +17,7 @@

@Entity(name = "Post")
@Table(name = "post")
public class Post implements ByPassHolder {
public class Post extends AbstractPost implements ByPassHolder {

@Id
@SequenceGenerator(name = "postSeqGen", sequenceName = "postSeq", initialValue = 100, allocationSize = 1)
Expand All @@ -35,8 +34,6 @@ public class Post implements ByPassHolder {

private boolean bypass;

private ZonedDateTime posted;

private String organization;

public Long getId() {
Expand Down Expand Up @@ -71,14 +68,6 @@ public void setBypass(boolean bypass) {
this.bypass = bypass;
}

public ZonedDateTime getPosted() {
return posted;
}

public void setPosted(ZonedDateTime postedAt) {
this.posted = postedAt;
}

public String getOrganization() {
return organization;
}
Expand Down

0 comments on commit 046c31d

Please sign in to comment.