预期加入的hibernate路径但设置了路径

时间:2016-12-15 15:29:45

标签: java hibernate hql

我有一个hql查询:

  private void OnConnect(object obj)
    {
        _client = new TcpClient(IP_ADDRESS, PORT);
        _client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
        _master = ModbusIpMaster.CreateIp(_client);
        Connected = _client.Connected;

        ManualControlViewModel = new ManualControlViewModel(_master);
        SetupViewModel = new SetupViewModel(_master);          

        StartReadingInfo();
    }

虽然设置了路径但它显示错误。我尝试了不同的hql变种,但错误仍然相同。我使用这两个entites从db登录弹簧安全,它工作正常。但是,当我试图让用户具有指定角色时,它无法正常工作。  我的实体:

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

UserRole实体:

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

如果我将我的HQL查询更改为:

@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;

它显示错误

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

2 个答案:

答案 0 :(得分:1)

而是明确JOIN尝试使用逗号表示法,并使用JOIN更改第二个UserRole表格,如下所示:

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

如果您只希望用户元素完成您的查询,请执行以下操作:

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

答案 1 :(得分:0)

您的查询中有拼写错误,因为您的User实体没有userRole,但userRoles

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