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 :
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 ?
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;
...
}