我正在将我的应用程序从旧版本的 hibernate 迁移到较新的版本 (6.0.2.Final)
在我的代码中,我有一个父实体类,它将表之间通用的所有列分组,并定义一个通用 getter 来获取每个表的 IdClass 实例:
家长课:
@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
@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 班级:
@Data
@EqualsAndHashCode(of = {"field", "code" })
@ToString(of = { "field", "code" })
@NoArgsConstructor
@AllArgsConstructor
public class ParamCodesId implements Serializable {
private String field;
private String code;
}
升级后,我现在收到一条消息告诉我
An entity cannot be annotated with both @Inheritance and @MappedSuperclass, @Inheritance will be ignored
我尝试过一些事情:
我无法找到一种方法来让 USER_CREATION 等通用字段和 id 仍然可以被如下请求检测到:
@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();
}
}
正确的做法是什么?
我找到的解决方案是从
Entity
类中删除 @Inheritance。e.id
不再映射到 Entity
中的 getId :
SELECT e FROM ParamCodes e WHERE e.id in (:ids)
相反,我只是删除
.id
,因为 e
本身已经被 hibernate 用作 ParamCodesId
,所以查询现在看起来像:
SELECT e FROM ParamCodes e WHERE e in (:ids)
并且
Entity
类现在只有 @MappedSuperClass
注释,并且抽象 getId
方法已被删除,因为它不再使用了 :
@MappedSuperclass
public abstract class Entity implements Serializable {
@Getter
@Setter
@Column(name = "USER_CREATION", insertable = true, updatable = false)
private String auditUtilisateurCreation;
...
}