簡體   English   中英

JPQL:EclipseLink 和 Hibernate 的區別

[英]JPQL: Difference between EclipseLink and Hibernate

我已經詢問了我的情況,但沒有找到合適的解決方案。 經過一些額外的搜索,我想我知道源問題,盡管不知道如何解決它。 如前所述,我有:

@Table(name = "role__parent")
@IdClass(RoleAssociationKey.class)
@Data
public class RoleAssociation implements Serializable {

    @Id
    @JoinColumn(name = "has_parent_role_id")
    private Role node;

    @Id
    @JoinColumn(name = "is_parent_for_role_id")
    private Role parent;
    ...
}
@Data
class RoleAssociationKey implements Serializable {
    private static final long serialVersionUID = 1L;
    private int node;
    private int parent;
}

我有

@Table(name = "role")
@Data
public class Role implements IRole {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @OneToMany(mappedBy = "node", orphanRemoval = true, cascade = { CascadeType.ALL })
    private List<RoleAssociation> parentRoles;
    ...

到目前為止,我認為沒有什么特別的。 我有查詢:

@NamedQuery(name = "Role.findParents", query = 
   "SELECT r FROM Role r JOIN RoleAssociation ra ON r.id = ra.parent.id WHERE ra.node.id = :id")

目的是為了所有的父母。 當我編譯它時,Hibernate 抱怨left and right hand sides of a binary logic operator were incompatible [integer : component[node,parent]]

由於該語句在 EclipseLink 中有效,我不知道如何將其更改為有效的 Hibernate 語句。 幫助將不勝感激。

經過一番努力,我終於找到了這個問題的根本原因。 我知道 SQL 可能會得到改進,但我在其他類似的地方失敗了。

Hibernate 需要為@OneToMany關系提供匹配對。

在我的查詢中,我指的是我角色的parent 解決辦法是

@Data
public class RoleAssociation implements Serializable {

    @Id
    @JoinColumn(name = "has_parent_role_id")
    @ManyToOne // <<<<==== rerequired for Hibernate
    private Role node;

    @Id
    @JoinColumn(name = "is_parent_for_role_id")
    @ManyToOne // <<<<==== rerequired for Hibernate
    private Role parent;

我不知道為什么 Hibernate 會抱怨而 EclipseLink 可以獲取所需的信息。 不過有了這個額外的注釋,代碼就可以工作了!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM