JPQL:EclipseLink和Hibernate之间的区别 [英] JPQL: Difference between EclipseLink and Hibernate

查看:204
本文介绍了JPQL:EclipseLink和Hibernate之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经询问有关我的情况,但没有找到合适的解决方案.经过一些额外的搜索后,我认为我知道了源问题,尽管不知道如何解决.如前所述,我有:

I already asked about my situation and didn't find a proper solution. After some additional search I think I know the source problem although don't know how to resolve it. As mentioned I have:

@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;
    ...

到目前为止,我认为没有什么特别的.我有查询:

Up to this point I think nothing special. I have the query:

@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]]

with the purpose to all set parents. When I compile it Hibernate complainsleft and right hand sides of a binary logic operator were incompatible [integer : component[node,parent]]

由于该语句在EclipseLink中有效,所以我不知道如何将其更改为可用的Hibernate语句.帮助将不胜感激.

Since the statement works in EclipseLink I have no clue how to change it into a working Hibernate one. Help would be highly appreciated.

推荐答案

经过一番奋斗,我终于弄清了这个问题的根本原因.我知道SQL可能会得到改进,但是我在其他类似的地方却失败了.

After some struggels I finally figured the root cause of this problem. I'm aware that the SQL might be improved nevertheless I fail at other similar spots.

Hibernate需要具有用于@OneToMany关系的匹配对.

Hibernate requires to have a matching pair for @OneToMany relation.

在查询中,我指的是角色的parent.解决方法是

In my query I refer to the parent of my role. The solution is

@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可以获取所需的信息.然而,有了这个额外的注释,代码就可以工作了!

I have no clue why Hibernate complains while EclipseLink can fetch the required information. Nevertheless with this additional annoation the code works!

这篇关于JPQL:EclipseLink和Hibernate之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆