问题
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 complainsleft 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:
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!
来源:https://stackoverflow.com/questions/59984513/jpql-difference-between-eclipselink-and-hibernate