Skip to content

Commit

Permalink
Merge pull request #4459 from geoand/#4456
Browse files Browse the repository at this point in the history
Allow @id to be part of the class hierarchy in Spring Data JPA
  • Loading branch information
gsmet authored Oct 9, 2019
2 parents 2d967cd + a32f7fd commit 29c3c41
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public final class DotNames {
public static final DotName LIST = DotName.createSimple(List.class.getName());
public static final DotName STREAM = DotName.createSimple(Stream.class.getName());
public static final DotName OPTIONAL = DotName.createSimple(Optional.class.getName());
public static final DotName OBJECT = DotName.createSimple(Object.class.getName());

private DotNames() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -890,21 +890,28 @@ private boolean canTypesBeConsideredSame(Type candidate, Type target) {
}

private AnnotationTarget getIdAnnotationTarget(DotName entityDotName, IndexView index) {
ClassInfo entityClassInfo = index.getClassByName(entityDotName);
if (entityClassInfo == null) {
throw new IllegalStateException("Entity class " + entityClassInfo + " was not part of the Quarkus index");
return getIdAnnotationTargetRec(entityDotName, index, entityDotName);
}

private AnnotationTarget getIdAnnotationTargetRec(DotName currentDotName, IndexView index, DotName originalEntityDotName) {
ClassInfo classInfo = index.getClassByName(currentDotName);
if (classInfo == null) {
throw new IllegalStateException("Entity " + originalEntityDotName + " was not part of the Quarkus index");
}

if (!entityClassInfo.annotations().containsKey(DotNames.JPA_ID)) {
throw new IllegalArgumentException("Currently only Entities with the @Id annotation are supported. " +
"Offending class is " + entityDotName);
if (!classInfo.annotations().containsKey(DotNames.JPA_ID)) {
if (DotNames.OBJECT.equals(classInfo.superName())) {
throw new IllegalArgumentException("Currently only Entities with the @Id annotation are supported. " +
"Offending class is " + originalEntityDotName);
}
return getIdAnnotationTargetRec(classInfo.superName(), index, originalEntityDotName);
}

List<AnnotationInstance> annotationInstances = entityClassInfo.annotations().get(DotNames.JPA_ID);
List<AnnotationInstance> annotationInstances = classInfo.annotations().get(DotNames.JPA_ID);
if (annotationInstances.size() > 1) {
throw new IllegalArgumentException(
"Currently the @Id annotation can only be placed on a single field or method. " +
"Offending class is " + entityDotName);
"Offending class is " + originalEntityDotName);
}

return annotationInstances.get(0).target();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package io.quarkus.it.spring.data.jpa;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Movie {

@Id
@GeneratedValue
private Long id;
public class Movie extends MovieSuperclass {

private String title;
private String rating;
Expand All @@ -24,10 +18,6 @@ public Movie(String title, String rating, int duration) {
this.duration = duration;
}

public Long getId() {
return id;
}

public String getTitle() {
return title;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.quarkus.it.spring.data.jpa;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

@MappedSuperclass
public abstract class MovieSuperclass {

@Id
@GeneratedValue
private Long id;

public Long getId() {
return id;
}
}

0 comments on commit 29c3c41

Please sign in to comment.