无法将 MS Sql 查询转换为 Hibernate @Query

Posted

技术标签:

【中文标题】无法将 MS Sql 查询转换为 Hibernate @Query【英文标题】:Could not translate MS Sql query to Hibernate @Query 【发布时间】:2019-11-23 04:49:29 【问题描述】:

我有以下完美运行的 MS SQL 查询。

select u.id, u.username, r2.authority, em.hrt02_first_name, em.hrt02_last_name from users as u
    inner join group_members gm
        on u.id = gm.user_id
    inner join groups g
        on gm.group_id = g.id
    inner join group_authorities ga
        on ga.group_id = g.id
    inner join roles r2
        on ga.role_id = r2.id
    inner join hrt02_employee_name em
        on em.id = u.id
where u.username = 'john'

输出如下

+----+----------+------------+------------------+-----------------+
| id | username | authority  | hrt02_first_name | hrt02_last_name |
+----+----------+------------+------------------+-----------------+
| 1  | john     | ROLE_ADMIN | fname            | lname           |
+----+----------+------------+------------------+-----------------+
| 1  | john     | ROLE_USER  | fname            | lname           |
+----+----------+------------+------------------+-----------------+

但是当我尝试将其转换为 Hibernate Query 或 `@Query(..., nativeQuery=true) 时,它会引发异常。 (延迟初始化和预期加入路径失败)。

这是我的架构设计

@Entity
public class Users 
    // id, username omitted

    @ManyToMany
      @JoinTable(name="group_members", joinColumns=@JoinColumn(name="user_id", referencedColumnName="id"), inverseJoinColumns=@JoinColumn(name="group_id", referencedColumnName="id"))
    private List<Groups> groups;


@Entity
public class Groups 
    // id omitted
    @ManyToMany
      @JoinTable(name="group_authorities", joinColumns=@JoinColumn(name="group_id", referencedColumnName="id"), inverseJoinColumns=@JoinColumn(name="role_id", referencedColumnName="id"))
    private List<Roles> roles;


@Entity
public class Roles 
    // id omitted, authority


@Entity
public class Hrt02EmployeeName 
    // id, firstname, lastname omitted

更新 1 - 我尝试过的查询,

这里的所有示例都会引发错误,但如果您在本机查询中编写它们并运行它们,它们会按预期工作。所以,我可能不知道如何将它从 nativeQuery 转换为 Hibernate Query。

public interface UsersRepository extends JpaRepository<Users, Long> 
    @Query(value =
            "select * from users as u" +
            "    inner join group_members gm" +
            "        on u.id = gm.user_id" +
            "    inner join groups g" +
            "        on gm.group_id = g.id" +
            "    inner join group_authorities ga" +
            "        on ga.group_id = g.id" +
            "    inner join roles r2" +
            "        on ga.role_id = r2.id" +
            " inner join hrt02_employee_name em" +
            "  on em.id = u.id" +
            "  where u.username = :qryusername", nativeQuery = true)
    public Users findRoleByUsername(@Param("qryusername") String username);

    @Query("select distinct u.username, r2.authority from Users as u " +
            " inner join group_members gm " +
            "  on u.id = gm.user_id " +
            " inner join Groups g " +
            "  on gm.group_id = g.id " +
            " inner join group_authorities ga " +
            "  on ga.group_id = g.id " +
            " inner join Roles r2 " +
            "  on ga.role_id = r2.id" +
            " inner join hrt02_employee_name em" +
            "  on em.id = u.id" +
            " where u.username = :username")
    public Users findRoleByUsername(@Param("username") String username);

    @Query(value =
            "select u from Users u" +
            " inner join GroupMembers gm" +
            "  on u.id = gm.user_id" +
            " inner join Groups g" +
            "  on gm.group_id = g.id" +
            " inner join GroupAuthorities ga" +
            "  on ga.group_id = g.id" +
            " inner join Roles r2" +
            "  on ga.role_id = r2.id" +
            " inner join hrt02_employee_name em" +
            "  on em.id = u.id" +
            " where u.username = :username")
    public Users findRoleByUsername(@Param("username") String username);

【问题讨论】:

你能展示一下jpql查询的尝试吗? 嗨@Andronicus,我已经更新了我的帖子。请参阅更新 1,谢谢 【参考方案1】:

由于您想将其转换为 User,因此您必须构造它,而不是 u.username, r2.authority。其次,您需要获取您正在使用的内容以避免LazyInitializationException

@Query("select distinct u from Users u " +
        " left join fetch u.groups g "
        " left join fetch g.roles r " +
        "  ... "
        " where u.username = :username")
public Users findRoleByUsername(@Param("username") String username);

这是开始,因为您的实体Role 没有映射。您必须以与点所在的其他实体相同的方式编写。

【讨论】:

以上是关于无法将 MS Sql 查询转换为 Hibernate @Query的主要内容,如果未能解决你的问题,请参考以下文章

如何将访问查询转换为MS SQL

如何将 MS SQL 查询转换为 MySQL 查询

如何将选择 sql 查询的结果转换为 ms 访问中的新表

MS Access 查询转换为 Sql Server

将 mysql 查询转换为 MS SQL

如何将 MS Access“IIF”查询转换为 Sql Server 查询?