繁体   English   中英

Spring Data JPA @Query注释存在问题(SQL语法错误)

[英]Problem with Spring Data JPA @Query Annotation (error in SQL syntax)

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

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

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

UserGroup实体

@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这是过时的还是什么?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM