更新 hibernate 版本后未检测到 Hibernate JPA 通用 IdField

问题描述 投票:0回答:1

我正在将我的应用程序从旧版本的 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
我尝试过一些事情:

  • 仅在实体上保留 @MappedSuperclass => 在我的查询中找不到 Id 列
  • 仅保留实体上的@Inheritance =>更新数据库时找不到实体中的列(如USER_CREATION)
  • 在子类中添加名为 id 的列作为 @Transient => 无法按我的请求查询
  • 在子类中添加名为 id 的列 @Column(name = "id", insertable = false, updatable = false) => 字段列表中的未知列 id

我无法找到一种方法来让 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();
    }
}

正确的做法是什么?

java hibernate jpa jpql
1个回答
0
投票

我找到的解决方案是从

Entity
类中删除 @Inheritance。
如果没有它,HQL 就无法解释下面的查询,因为
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;

    ...
}
© www.soinside.com 2019 - 2024. All rights reserved.