为啥得到 org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join!加入 3 个表时

Posted

技术标签:

【中文标题】为啥得到 org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join!加入 3 个表时【英文标题】:Why getting org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! when joining 3 tables为什么得到 org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join!加入 3 个表时 【发布时间】:2019-07-09 08:46:29 【问题描述】:

我有三个实体基本信息,性别和部门。如果我加入 basicInfo 和性别模型,那么它工作正常,但是当想要同时加入部门和其他两个实体时,然后显示

.QuerySyntaxException: 需要加入的路径!

这里是我的三个实体类,

@Entity
@Table(name = "employeebasicinfo")
public class BasicInfoModel 
@Id private String employeeId;
@NotBlank private String firstName;
@NotBlank private String lastName;
@NotNull private Integer companyId;
@Transient private String companyName;
private String phoneNo;

@Column(name = "sex")
private char sexId;

@Transient private String sexName;

@Temporal(TemporalType.TIMESTAMP)
private Date birthDate;

@Temporal(TemporalType.TIMESTAMP)
private Date joiningDate;

@NotNull private Integer department;
@Transient private String departmentName;

@ManyToOne()
@JoinColumn(name = "sex", referencedColumnName = "id", insertable = false, updatable = false)
private GenderModel genderModel;

@ManyToOne
@JoinColumn(
  name = "department",
  referencedColumnName = "id",
  insertable = false,
  updatable = false)
private DepartmentModel departmentModel;
// getter setter
 

性别模型

 @Entity
 @Table(name = "gender")
 public class GenderModel implements Serializable 
 @Id
 @Column(name = "id")
 private char id;

 @Column(name = "name")
 private String name;
 //getter setter

并系模型

   @Entity
   @Table(name = "department")
   public class DepartmentModel 
   @Id private int id;
   private String name;
   //getter setter

我的 Dto 课程

public class BasicInfoDto 
  private String employeeId;
  private String firstName;
  private String lastName;
  private Integer companyId;
  private String companyName;
  private String phoneNo;
  private char sexId;
  private String sexName;
  private Date birthDate;
  private Date joiningDate;
  private Integer department;
  private String departmentName;


  public BasicInfoDto(char sexId, String sexName, int department, String departmentName) 
    this.sexId = sexId;
    this.sexName = sexName;
    this.department = department;
    this.departmentName = departmentName;

   //getter setter
  

我的存储库类如下:

@Repository
public interface BasicInfoRepository extends JpaRepository<BasicInfoModel, String> 


  @Query(
      value =
          "SELECT  new com.employeeAttendance.employeeAttendance.dto.BasicInfoDto ("
              + " a.sexId, b.name, a.department,c.name) FROM "
              + " BasicInfoModel a INNER JOIN GenderModel b on a.sexId = b.id" +
                   " INNER JOIN DepartmentMode c on a.department = c.id")
  List<BasicInfoDto> getBasicInfoList();

最后我得到了这个错误

引起:java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException:路径预期 加入! [选择新的 com.employeeAttendance.employeeAttendance.dto.BasicInfoDto (a.sexId, b.name, a.department,c.name) FROM com.employeeAttendance.employeeAttendance.model.BasicInfoModel 一个 INNER 加入 com.employeeAttendance.employeeAttendance.model.GenderModel b on a.sexId = b.id INNER JOIN DepartmentMode c on a.department = c.id] 在 org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final] 在 org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final] 在 org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final] 在 org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:713) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final] 在 org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final] 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191] 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_191] 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_191] 在 java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_191] 在 org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:350) ~[spring-orm-5.1.4.RELEASE.jar:5.1.4.RELEASE] 在 com.sun.proxy.$Proxy109.createQuery(Unknown Source) ~[na:na] 在 org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:87) ~[spring-data-jpa-2.1.4.RELEASE.jar:2.1.4.RELEASE] ...省略了76个常用框架 引起:org.hibernate.hql.internal.ast.QuerySyntaxException:加入路径预期! [选择新的 com.employeeAttendance.employeeAttendance.dto.BasicInfoDto (a.sexId, b.name, a.department,c.name) FROM com.employeeAttendance.employeeAttendance.model.BasicInfoModel 一个 INNER 加入 com.employeeAttendance.employeeAttendance.model.GenderModel b on a.sexId = b.id INNER JOIN DepartmentMode c on a.department = c.id] 在 org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final] 在 org.hibernate.hql.internal.ast.ErrorTracker.throwQueryException(ErrorTracker.java:93) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final] 在 org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:277) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final] 在 org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:191) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final] 在 org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:143) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final] 在 org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:119) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final] 在 org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:80) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final] 在 org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final] 在 org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:595) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final] 在 org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:704) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final] ...省略了84个常用帧

但是,如果我删除与部门的第二个加入,那么它工作正常。

【问题讨论】:

【参考方案1】:

您正在处理 jpql 中的实体属性而不是表格。所以你只告诉 jpql 使用哪个属性来加入:

"SELECT  new com.employeeAttendance.employeeAttendance.dto.BasicInfoDto ("
  + " a.sexId, b.name, a.department,c.name) FROM "
  + " BasicInfoModel a INNER JOIN a.genderModel b "
  + " INNER JOIN a.departmentModel c";

【讨论】:

以上是关于为啥得到 org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join!加入 3 个表时的主要内容,如果未能解决你的问题,请参考以下文章

为啥 NotificationManagerCompat::cancelAll() 得到 SecurityException?

为啥我没有得到任何回应

为啥我得到黑色纹理?

为啥我得到一个 nullPointerException? [复制]

为啥我得到 getApplicationcontext() null?

为啥我会得到这个错误的输出?