question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

multi layered entity inheritance not supported by JpaRepository

See original GitHub issue

Describe the bug Imagine a simple JpaRepo:

public interface ExampleRepo extends JpaRepository<ExampleEntity, Long> {
    Optional<ExampleEntity> findByNameAndId(String name, Long id);
}

Now dependending on the amount of layers between ExampleEntity and the definition of id, this repository works or doesn’t work:

  • works:
@MappedSuperclass public abstract class Base1 { @Id Long id; }
@Entity public class ExampleEntity extends Base1 { String name; }
  • doesn’t work:
@MappedSuperclass public abstract class Base1 { @Id id }
@MappedSuperclass public abstract class Base2 extends Base1 { }
@Entity public class ExampleEntity extends Base2 { String name; }

Expected behavior

  • id - property is found by JpaReposiory, regardless of hierarchy size.

Actual behavior

  • build error io.quarkus.spring.data.deployment.UnableToParseMethodException: Entity ExampleEntity does not contain a field named: Id. Offending method is findByNameAndId
    • repo looks for property Id (note the uppercase “i”) instead of id

To Reproduce See archive: entity_hierarchy_demonstrator.zip

Environment (please complete the following information):

  • Output of uname -a or ver: win 10
  • Output of java -version: 11
  • GraalVM version (if different from Java): /
  • Quarkus version or git rev: 1.8.3.Final
  • Build tool (ie. output of mvnw --version or gradlew --version): Apache Maven 3.6.3

/cc @famod

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
geoandcommented, Oct 19, 2020

Thanks for the contribution!

It will most likely be part of Quarkus 1.9.1

2reactions
famodcommented, Oct 16, 2020

I found the problem and might be able to create a PR on sunday evening.

But in case you @aureamunoz want to do it here is the root cause: This is wrong: https://github.com/quarkusio/quarkus/blob/1.8.3.Final/extensions/spring-data-jpa/deployment/src/main/java/io/quarkus/spring/data/deployment/MethodNameParser.java#L590

For each loop it calls this:

ClassInfo superClass = indexView.getClassByName(entityClass.superName());

but entityClass never changes. This is correct:

ClassInfo superClass = indexView.getClassByName(superClassType.name());

(also leading to a simplification further down)

/cc @geoand

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handling entities inheritance with Spring Data JPA
We want to write three Repository classes (aka DAO), one for each entity, following the hierarchy structure so all repository's methods ...
Read more >
JPA Repository Inheritance - Extend Multiple Interfaces
Guess that it is a tricky thing to do because it actually would be multi inheritance thing which Java does not support, for...
Read more >
Complete Guide: Inheritance strategies with JPA and Hibernate
Hibernate and JPA support 4 inheritance strategies which map the entities to different table models. But which one is the best for your...
Read more >
The best way to use entity inheritance with JPA and Hibernate
All you need is the @MappedSuperclass annotation, but that's not entity inheritance since the object hierarchy is only visible in the OOP ...
Read more >
Hibernate Inheritance Mapping - Baeldung
Using the MappedSuperclass strategy, inheritance is only evident in the class but not the entity model. Let's start by creating a Person class ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found