为什么要获得org.hibernate.hql.internal.ast.QuerySyntaxException:期望加入的路径!加入3张桌子时

时间:2019-02-15 08:40:52

标签: spring-boot spring-data-jpa spring-data

我有三个实体basicInfo,性别和部门。如果我加入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内部   加入com.employeeAttendance.employeeAttendance.model.GenderModel b   a.sexId = b.id内部联接a。部门上的DepartmentMode c = 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(本机方法)〜[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(未知来源)〜[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内部   加入com.employeeAttendance.employeeAttendance.model.GenderModel b   a.sexId = b.id内部联接a。部门上的DepartmentMode c = 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 个答案:

答案 0 :(得分:0)

您正在使用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";