Search code examples
javahibernatejpajpql

Hibernate JPA generic IdField not detected after updating hibernate version


I'm migrating my app from an old version of hibernate to a more recent one (6.0.2.Final)

In my code, I have a parent entity class that group all columns that are generic between tables and define a generic getter to get an IdClass instance for each tables :

Parent class :

@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@MappedSuperclass
public abstract class Entity implements Serializable {

    @Getter
    @Setter
    @Column(name = "USER_CREATION", insertable = true, updatable = false)
    private String auditUtilisateurCreation;

    ...

    //Refer to the IdClass object of the classes 
    public abstract Object getId();

    ...
}

Entity class :

@Entity
@Table(name = "PARAM_CODES")
@IdClass(ParamCodesId.class)
@Data
public class ParamCodes extends Entity {
    @Id
    @Column(name = "FIELD")
    private String field;

    @Id
    @Column(name = "CODE")
    private String code;

    @Column(name = "ORDER")
    private Long order;
    
    ...

   
    @Override
    public ParamCodeLibelleId getId() {
        return new ParamCodesId(field, code);
    }
}

Id class :

@Data
@EqualsAndHashCode(of = {"field", "code" })
@ToString(of = { "field", "code" })
@NoArgsConstructor
@AllArgsConstructor
public class ParamCodesId implements Serializable {
    private String field;
    private String code;
}

After the upgrade I now have a message telling me that An entity cannot be annotated with both @Inheritance and @MappedSuperclass, @Inheritance will be ignored I've tried few things :

  • Keeping only the @MappedSuperclass on Entity => The Id column is not found in my query
  • Keeping only the @Inheritance on Entity => The columns from Entity (like USER_CREATION) are not found while updating the database
  • Adding a column named id as @Transient in the child class => Not queryable by my request
  • Adding a column named id as @Column(name = "id", insertable = false, updatable = false) in the child class => Unknown column id in field list

I'm unable to find a way to have both the generic fields like USER_CREATION and the id still be detected by requests like :

@Repository
public class JpaParamCodeLibelleRepository {

    ...

    @Transactional(propagation = Propagation.MANDATORY)
    public List<ParamCodes> getExisingEntity(List<ParamCodesId> ids) {
        Query querySelect = getEntityManager().createQuery("SELECT e FROM ParamCodes e WHERE e.id in (:ids)", ParamCodes.class);
        return querySelect.setParameter("ids", ids).getResultList();
    }
}

What's the right way to do this ?


Solution

  • The solution I've found is to remove the @Inheritance from the Entity class.
    Without it the HQL is not able to interpret the query bellow because the e.id is not mapped anymore to the getId from the Entity :

    SELECT e FROM ParamCodes e WHERE e.id in (:ids)
    

    Instead I just remove the .id since e by itself is already used as a ParamCodesId by hibernate so the query now look like :

    SELECT e FROM ParamCodes e WHERE e in (:ids)
    

    And the Entity class now only have the @MappedSuperClass annotation and the abstract getId method is removed since it's not used anymore :

    @MappedSuperclass
    public abstract class Entity implements Serializable {
    
        @Getter
        @Setter
        @Column(name = "USER_CREATION", insertable = true, updatable = false)
        private String auditUtilisateurCreation;
    
        ...
    }