quarkusio / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
13.54k stars 2.61k forks source link

Panache: MetaModel cannot determine ID field #41389

Closed melloware closed 2 months ago

melloware commented 2 months ago

Describe the bug

Quarkus PrimeFaces original report: https://github.com/quarkiverse/quarkus-primefaces/issues/215

PrimeFaces has an automatic JPA Table so you can just point a JPA Entity an it will handle all paging sorting filtering using Hibernate Criteria Query.

@Entity
@Table(name = "CAR")
public class CarEntity extends PanacheEntity {

    public String vin;
    public String make;
    public String model;
    @Column(name="makeyear")
    public int year;
    public String color;
    public BigDecimal price;
    @Version
    @Column(name = "modified_time")
    public Instant modifiedTime;

}

However when it uses the Hibernate Criteria to determine the object's ID its failing with ...

Code:

SingularAttribute<?, ?> idAttribute = entityType.getDeclaredId(idType.getJavaType());

Failure:

2024-06-24 08:02:44,362 SEVERE [org.pri.app.exc.PrimeExceptionHandler] (executor-thread-6) The id attribute is not declared on this type [com.melloware.insights.CarEntity]: java.lang.IllegalArgumentException: The id attribute is not declared on this type [com.melloware.insights.CarEntity]                                                                       
        at org.hibernate.metamodel.model.domain.AbstractIdentifiableType.getDeclaredId(AbstractIdentifiableType.java:169)                                                           
        at org.hibernate.metamodel.model.domain.AbstractIdentifiableType.getDeclaredId(AbstractIdentifiableType.java:46)
        at org.primefaces.model.JPALazyDataModel$Builder.build(JPALazyDataModel.java:457)
        at com.melloware.insights.CarView.init(CarView.java:26)
        at com.melloware.insights.CarView_Bean.doCreate(Unknown Source)
        at com.melloware.insights.CarView_Bean.create(Unknown Source)
        at com.melloware.insights.CarView_Bean.create(Unknown Source)

Expected behavior

The @Id field exists on the Panache entity and should be found by the MetaModel getDeclaredId

Actual behavior

It fails with the stack trace above

How to Reproduce?

  1. Unzip this reproducer: quarkus-pf-panache.zip

  2. Run mvn quarkus:dev and navigate to http://localhost:8080 you will see the error

Output of uname -a or ver

Microsoft Windows [Version 10.0.22631.3737]

Output of java -version

OpenJDK 64-Bit Server VM Temurin-17.0.11+9 (build 17.0.11+9, mixed mode, sharing)

Quarkus version or git rev

3.12.0

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.9.7 (8b094c9513efc1b9ce2d952b3b9c8eaedaf8cbf0)

Additional information

No response

quarkus-bot[bot] commented 2 months ago

/cc @FroMage (panache), @loicmathieu (panache)

FroMage commented 2 months ago

Hi,

I checked it out, and it appears the javadoc of getDeclaredId is wrong, because it follows the same principles as Java's reflection, where declared means there will be no super-type lookup: it will be limited to the entity type passed as parameter, and not look for its mapped superclasses. You should use getId to lookup IDs that can be defined in supertypes. I just checked it and that works.

Hope that helps.

melloware commented 2 months ago

i will verify and close this! thanks @FroMage !

FroMage commented 2 months ago

šŸ‘