简体   繁体   中英

hibernate path expected for join but path is set

I have an hql query:

"from User u inner join UserRole ur on ur.user_name = u.user_name and ur.user_role =ROLE_MANAGER "

And it shows an error though the path is set. I tried different variants of hql but error remains the same. I use those 2 entites for spring security login from db and it works fine. But when i'm trying to get user with specified role it doesn't work. My entities:

@Entity
@Table(name = "USERS")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id")
    private int user_id;


    @Column(name = "username", nullable = false, unique = true)
    private String username;

    @Column(name = "passwort", nullable = false)
    private String password;

    @Column(name = "email")
    private String email = "hromnikforever@gmail.com";

    @Column(name = "enabled", nullable = false)
    private int enabled = 1;

    @Autowired
    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set<UserRole> userRoles = new HashSet<UserRole>(0);

UserRole entity:

@Entity
@Table(name = "USER_ROLES")
public class UserRole {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id",unique = true, nullable = false)
    private int user_role_id;

    @Column(name = "username")
    private String username;

    @Column(name = "user_role")
    private String user_role;

If i change my HQL query to:

from User u inner join u.userRole ur on ur.user_name = u.user_name
and ur.user_role =ROLE_MANAGER "

it shows an error that

 could not resolve property: userRole of: com.webproject.User [from com.webproject.User u inner join u.userRole ur on ur.user_name = u.user_name and ur.user_role =ROLE_MANAGER ]

Instead explicit JOIN try with comma notation, and change the second JOIN table with UserRole , as follow:

from User u, UserRole ur
where ur.user_name = u.user_name
and ur.user_role = ROLE_MANAGER

If you want only User elements complete your query as follow:

select u from User u, UserRole ur
where ur.user_name = u.user_name
and ur.user_role = ROLE_MANAGER

You have a typo in your query, since your User entity doesn't have a userRole , but userRoles

from User u 
inner join u.userRoles ur on ur.user_name = u.user_name
                          and ur.user_role = ROLE_MANAGER

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