ClassCastException Issue when Migrating from JEE javax to Jakarta EE with Hibernate

67 views Asked by At

Can you help me to correct this function. I have been having anomaly since I migrated from jee javax to jakartaee. It's work well on jee, but not on JakartaEE

public <T extends GenericInputTimesheet> List<T> findWhereItemToto(Class<T> entityClass, Integer value)

Here is my simplified Jakarta JPA data model. (I’m not including the getters/setters here for readability)

@MappedSuperclass
public abstract class GenericInputTimesheet<T extends GenericTimesheetItem> implements Serializable {

    @Id
    private long id;
    @NotNull
    @ManyToOne
    private T item;
}

@MappedSuperclass
public class GenericTimesheetItem implements Serializable {

    @Id
    private long id;
    private Integer toto;
}

@Entity
public class InputTimesheetMandat extends GenericInputTimesheet<Mandat> {

}

@Entity
public class Mandat extends GenericTimesheetItem {

}

Then I have this function

public <T extends GenericInputTimesheet> List<T> findWhereItemToto(Class<T> entityClass, Integer value) {
    return entityManager.createQuery("Select e From " + entityClass.getSimpleName() + " e Where e.item.toto=:toto", entityClass)
            .setParameter("toto", value)
            .getResultList();
}

My launcher :

public void displayToto(Integer value) {
    try {
        List<InputTimesheetMandat> inputs = pocService.findWhereItemToto(InputTimesheetMandat.class, value);
        System.out.println(inputs.size() + " InputTimesheetMandat Toto = " + value + " founds");
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
}

I catch this exception :

java.lang.ClassCastException: class org.hibernate.metamodel.model.domain.internal.MappedSuperclassTypeImpl cannot be cast to class org.hibernate.metamodel.model.domain.EntityDomainType (org.hibernate.metamodel.model.domain.internal.MappedSuperclassTypeImpl and org.hibernate.metamodel.model.domain.EntityDomainType are in unnamed module of loader '[email protected]' @304f8087)

1

There are 1 answers

0
olivier P On

I fix the probleme

@MappedSuperclass
public abstract class GenericInputTimesheet<T extends GenericTimesheetItem> implements Serializable {

    @Id
    private long id;

    public abstract T getItem();

    public abstract void setItem(T genericTimesheetItem);
}

@MappedSuperclass
public class GenericTimesheetItem implements Serializable {

    @Id
    private long id;
    private Integer toto;
}

@Entity
public class InputTimesheetMandat extends GenericInputTimesheet<Mandat> {

    @NotNull
    @ManyToOne
    private Mandat item;

    @Override
    public Mandat getItem() {
        return item;
    }

    @Override
    public void setItem(Mandat item) {
        this.item = item;
    }

}

@Entity
public class Mandat extends GenericTimesheetItem {

}

//Work well Now
public <T extends GenericInputTimesheet> List<T> findWhereItemToto(Class<T> entityClass, Integer toto) {
    return entityManager.createQuery("Select e From " + entityClass.getSimpleName() + " e Where e.item.toto=:toto", entityClass)
            .setParameter("toto", toto)
            .getResultList();
}