Skip to content

Commit

Permalink
Merge pull request #18266 from famod/spring-data-jpa-fix-inheritance
Browse files Browse the repository at this point in the history
Fix parse error on attribute from @Inheritance superclass in spring-data extension
  • Loading branch information
gsmet authored Jun 30, 2021
2 parents 31e8c06 + 228e3e4 commit b72f9c8
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;

Expand Down Expand Up @@ -86,6 +87,7 @@ public final class DotNames {

public static final DotName JPA_ID = DotName.createSimple(Id.class.getName());
public static final DotName VERSION = DotName.createSimple(Version.class.getName());
public static final DotName JPA_INHERITANCE = DotName.createSimple(Inheritance.class.getName());
public static final DotName JPA_MAPPED_SUPERCLASS = DotName.createSimple(MappedSuperclass.class.getName());
public static final DotName JPA_ENTITY = DotName.createSimple(Entity.class.getName());;
public static final DotName VOID = DotName.createSimple(void.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class MethodNameParser {
public MethodNameParser(ClassInfo entityClass, IndexView indexView) {
this.entityClass = entityClass;
this.indexView = indexView;
this.mappedSuperClassInfos = getMappedSuperClassInfos(indexView, entityClass);
this.mappedSuperClassInfos = getSuperClassInfos(indexView, entityClass);
}

public enum QueryType {
Expand Down Expand Up @@ -508,13 +508,13 @@ private boolean entityContainsField(String fieldName) {
}

private FieldInfo getFieldInfo(String fieldName, ClassInfo entityClass,
MutableReference<List<ClassInfo>> mappedSuperClassInfos) {
MutableReference<List<ClassInfo>> superClassInfos) {
FieldInfo fieldInfo = entityClass.field(fieldName);
if (fieldInfo == null) {
if (mappedSuperClassInfos.isEmpty()) {
mappedSuperClassInfos.set(getMappedSuperClassInfos(indexView, entityClass));
if (superClassInfos.isEmpty()) {
superClassInfos.set(getSuperClassInfos(indexView, entityClass));
}
for (ClassInfo superClass : mappedSuperClassInfos.get()) {
for (ClassInfo superClass : superClassInfos.get()) {
fieldInfo = superClass.field(fieldName);
if (fieldInfo != null) {
break;
Expand All @@ -524,13 +524,15 @@ private FieldInfo getFieldInfo(String fieldName, ClassInfo entityClass,
return fieldInfo;
}

private List<ClassInfo> getMappedSuperClassInfos(IndexView indexView, ClassInfo entityClass) {
private List<ClassInfo> getSuperClassInfos(IndexView indexView, ClassInfo entityClass) {
List<ClassInfo> mappedSuperClassInfoElements = new ArrayList<>(3);
Type superClassType = entityClass.superClassType();
while (superClassType != null && !superClassType.name().equals(DotNames.OBJECT)) {
ClassInfo superClass = indexView.getClassByName(superClassType.name());
if (superClass.classAnnotation(DotNames.JPA_MAPPED_SUPERCLASS) != null) {
mappedSuperClassInfoElements.add(superClass);
} else if (superClass.classAnnotation(DotNames.JPA_INHERITANCE) != null) {
mappedSuperClassInfoElements.add(superClass);
}

if (superClassType.kind() == Kind.CLASS) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.quarkus.it.spring.data.jpa;

import javax.persistence.DiscriminatorColumn;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
public class CatalogValue extends AbstractEntity {

private String key;
private String displayName;

public String getKey() {
return key;
}

public String getDisplayName() {
return displayName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.quarkus.it.spring.data.jpa;

import org.springframework.data.jpa.repository.JpaRepository;

public interface CatalogValueRepository extends JpaRepository<CatalogValue, Long> {

CatalogValue findByKey(String key);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.quarkus.it.spring.data.jpa;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

@Path("/catalog-value")
public class CatalogValueResource {

private final CatalogValueRepository repository;

private final FederalStateCatalogValueRepository federalStateRepository;

public CatalogValueResource(CatalogValueRepository repository,
FederalStateCatalogValueRepository federalStateRepository) {
this.repository = repository;
this.federalStateRepository = federalStateRepository;
}

@Path("/super/{key}")
@GET
@Produces("application/json")
public CatalogValue findByKey(@PathParam("key") String key) {
return repository.findByKey(key);
}

@Path("/federal-state/{key}")
@GET
@Produces("application/json")
public CatalogValue findFederalStateByKey(@PathParam("key") String key) {
return federalStateRepository.findByKey(key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.quarkus.it.spring.data.jpa;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@DiscriminatorValue("federalState")
public class FederalStateCatalogValue extends CatalogValue {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.quarkus.it.spring.data.jpa;

import org.springframework.data.jpa.repository.JpaRepository;

public interface FederalStateCatalogValueRepository extends JpaRepository<FederalStateCatalogValue, Long> {

// note: key is defined in superclass of FederalStateCatalogValue
FederalStateCatalogValue findByKey(String key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,9 @@ INSERT INTO employee(id, user_id, first_name, last_name, team_id) VALUES (102, '

INSERT INTO MotorCar(id, brand, model) VALUES (1, 'Monteverdi', 'Hai 450');
INSERT INTO MotorCar(id, brand, model) VALUES (2, 'Rinspeed', 'iChange');
INSERT INTO MotorCar(id, brand, model) VALUES (3, 'Rinspeed', 'Oasis');
INSERT INTO MotorCar(id, brand, model) VALUES (3, 'Rinspeed', 'Oasis');

INSERT INTO CatalogValue(id, key, displayName, type) VALUES (1, 'DE-BY', 'Bavaria', 'federalState');
INSERT INTO CatalogValue(id, key, displayName, type) VALUES (2, 'DE-SN', 'Saxony', 'federalState');
INSERT INTO CatalogValue(id, key, displayName, type) VALUES (3, 'DE', 'Germany', 'country');
INSERT INTO CatalogValue(id, key, displayName, type) VALUES (4, 'FR', 'France', 'country');
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.quarkus.it.spring.data.jpa;

import io.quarkus.test.junit.NativeImageTest;

@NativeImageTest
public class CatalogValueResourceIT extends CatalogValueResourceTest {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.quarkus.it.spring.data.jpa;

import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.containsString;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
public class CatalogValueResourceTest {

@Test
void findByKey() {
when().get("/catalog-value/super/DE-SN").then()
.statusCode(200)
.body(containsString("Saxony"))
.body(containsString("DE-SN"));
}

@Test
void findFederalStateByKey() {
when().get("/catalog-value/federal-state/DE-SN").then()
.statusCode(200)
.body(containsString("Saxony"))
.body(containsString("DE-SN"));
}
}

0 comments on commit b72f9c8

Please sign in to comment.