JPA 查询连接错误:org.hibernate.hql.internal.ast.QuerySyntaxException:连接所需的路径

Posted

技术标签:

【中文标题】JPA 查询连接错误:org.hibernate.hql.internal.ast.QuerySyntaxException:连接所需的路径【英文标题】:JPA query join error: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join 【发布时间】:2017-03-28 14:17:30 【问题描述】:

我正在尝试加入以下 JPA 查询,但收到以下错误:

org.hibernate.hql.internal.ast.QuerySyntaxException:路径预期为 加入! [来自 com.crm.entity.User 用户加入 fetch Role 角色上 role.user_id = user.id 其中 user.deleted = false 和 user.enabled = true 和 user.username = :username]

这里是实现:

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

import org.springframework.stereotype.Repository;

import com.crm.entity.User;

@Transactional
@Repository
public class UserJpaDaoImpl implements UserJpaDaoCustom 

    @PersistenceContext
    private EntityManager em;

    @Override
    public User getUser(String username) 
        Query query = em.createQuery("from User user "
                                    + "join fetch Role role on role.userId = user.id "
                                    + "where user.deleted = false "
                                    + "and user.enabled = true "
                                    + "and user.username = :username", User.class);
        query.setParameter("username", username);
        return (User)query.getSingleResult();
    


User实体:

@Entity
@Table(name = "user")
public class User extends BaseEntity implements UserDetails, Visible 

    private static final long serialVersionUID = 1L;

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

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

    /* Spring Security fields*/
    @OneToMany
    @JoinColumn(name = "user_id")
    private List<Role> roles;
...

Role实体:

@Entity
@Table(name = "role")
public class Role implements GrantedAuthority, Identifiable 

    private static final long serialVersionUID = 1L;

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

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

    @Column(name = "user_id")
    private Integer userId;
...

我的查询中的连接有什么问题?

【问题讨论】:

JPQL 查询以“SELECT alias”开头。其他任何内容都不符合 JPA 规范 【参考方案1】:

这是 HQL 而不是 SQL:

   Query query = em.createQuery("from User user "
                                + "join fetch user.role "
                                + "where user.deleted = false "
                                + "and user.enabled = true "
                                + "and user.username = :username", User.class);

你必须处理对象结构而不是表格

【讨论】:

以上是关于JPA 查询连接错误:org.hibernate.hql.internal.ast.QuerySyntaxException:连接所需的路径的主要内容,如果未能解决你的问题,请参考以下文章

Spring JPA 和 Hibernate 的 JPQL 查询错误

Spring JPA使用page进行分页查询时遇到org.hibernate.QueryException: could not resolve property

Spring JPA使用page进行分页查询时遇到org.hibernate.QueryException: could not resolve property

使用JBoss AS 7连接到DB2 db的JPA-Hibernate:HHH000183:没有为查询类找到持久化类

无法打开 JPA EntityManager 进行事务处理;嵌套异常是 org.hibernate.exception.SQLGrammarException:无法获取 JDBC 连接

Jpa Join 查询与来自两个表的数据,org.hibernate.MappingException:没有 JDBC 类型的方言映射:2002