简体   繁体   中英

JPQL: Difference between EclipseLink and 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;
}

and I have

@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")

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.

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 = "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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM