JPQL:EclipseLink 和 Hibernate 之间的区别

huangapple go评论80阅读模式
英文:

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抱怨左右操作数不兼容的二元逻辑运算符 [integer : component[node,parent]]

由于该语句在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 = &quot;role__parent&quot;)
@IdClass(RoleAssociationKey.class)
@Data
public class RoleAssociation implements Serializable {

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

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

and I have

@Table(name = &quot;role&quot;)
@Data
public class Role implements IRole {

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

	@OneToMany(mappedBy = &quot;node&quot;, orphanRemoval = true, cascade = { CascadeType.ALL })
	private List&lt;RoleAssociation&gt; parentRoles;
    ...

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

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

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

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

答案1

得分: 1

在一些挣扎后,我终于找到了这个问题的根本原因。我知道SQL可能需要改进,但我在其他类似的地方也失败了。

Hibernate要求在@OneToMany关系中要有匹配的一对。

在我的查询中,我引用了角色的“parent”。解决方案是

@Data
public class RoleAssociation implements Serializable {

    @Id
    @JoinColumn(name = "has_parent_role_id")
    @ManyToOne // <<<<<<==== Hibernate需要的
    private Role node;

    @Id
    @JoinColumn(name = "is_parent_for_role_id")
    @ManyToOne // <<<<<<==== Hibernate需要的
    private Role parent;

我不知道为什么Hibernate会抱怨,而EclipseLink可以获取所需的信息。不过,通过这个额外的注解,代码可以正常工作!

英文:

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 requires to have a matching pair for @OneToMany relation.

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

@Data
public class RoleAssociation implements Serializable {

    @Id
    @JoinColumn(name = &quot;has_parent_role_id&quot;)
    @ManyToOne // &lt;&lt;&lt;&lt;==== rerequired for Hibernate
    private Role node;

    @Id
    @JoinColumn(name = &quot;is_parent_for_role_id&quot;)
    @ManyToOne // &lt;&lt;&lt;&lt;==== rerequired for Hibernate
    private Role parent;

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

huangapple
  • 本文由 发表于 2020年1月30日 19:02:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/59984513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定