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!