【问题标题】:Problem with Spring Data JPA @Query Annotation (error in SQL syntax)Spring Data JPA @Query Annotation 出现问题(SQL 语法错误)
【发布时间】:2019-03-31 04:11:54
【问题描述】:

编辑:我想检查某个用户是否在组中具有特权。如果有更好的解决方案,无需查询他的所有权限,请告诉我。

以下查询给出了一个我无法理解的错误:

@Repository
public interface PrivilegeRepository extends JpaRepository<Privilege, Long> {

    @Query("select p from Privilege p " +
            "inner join p.userTypes " +
            "inner join UserGroup u " +
            "where u.user.id=:uid and u.group.id=:gid")
    List<Privilege> getPrivilegesOfUser(@Param("uid") Long uid, @Param("gid") Long gid);

}

错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where usergroup1_.user_id=28 and usergroup1_.group_id=18' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_144]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_144]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_144]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_144]

用户组实体

@Entity
@Table(name = "user_to_group")
public class UserGroup {

    @EmbeddedId
    private UserGroupId id;

    @ManyToOne(fetch = FetchType.LAZY,
            cascade = {CascadeType.DETACH, CascadeType.MERGE,
                    CascadeType.REFRESH, CascadeType.PERSIST})
    @MapsId("userId")
    @JoinColumn(name = "user_id", insertable = false, updatable = false)
    private User user;

    @ManyToOne(fetch = FetchType.LAZY,
            cascade = {CascadeType.DETACH, CascadeType.MERGE,
                    CascadeType.REFRESH, CascadeType.PERSIST})
    @MapsId("groupId")
    @JoinColumn(name = "group_id", insertable = false, updatable = false)
    private Group group;

    @ManyToOne(fetch = FetchType.LAZY,
            cascade = {CascadeType.DETACH, CascadeType.MERGE,
                    CascadeType.REFRESH, CascadeType.PERSIST})
    @JoinColumn(name = "user_type_id")
    private UserType userType;

    @Column(name = "is_blocked",
    insertable = false)
    private boolean isBlocked = false;

我首先尝试了 MYSQL Workbench 中的等效查询,并且工作得很好。与 Jpa 存储库实现无关。 这是适用于 MYSQL 的代码:

select p.name from privilege p 
    inner join user_type_to_privilege utp 
        on p.id=utp.privilege_id
    natural join user_to_group utg
    where utg.user_id=16 and
        utg.group_id=9;

这是我使用的方言:spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect 这是过时的还是什么?

【问题讨论】:

  • 您的 JPQL 查询解析并编译为无效的 MySQL 查询,它不应该这样做。打开org.hibernate.SQLtrace 级别的日志记录,以获取生成的查询并在Hibernate's JIRA 提交错误报告。如果它是一个无效的 JPQL 查询(可能没有明确的连接条件),那么它应该在到达数据库之前失败。

标签: java hibernate spring-data-jpa spring-data jpql


【解决方案1】:

问题出在第二个内部连接上。这是模棱两可的,不知道要与哪一列匹配。 有效的查询:

@Query("select p from Privilege p " +
            "inner join p.userTypes t " +
            "inner join UserGroup u on t.id=u.userType.id "+
            "where u.user.id=:uid and u.group.id=:gid")
    List<Privilege> getPrivilegesOfUser(@Param("uid") Long uid, @Param("gid") Long gid);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多