无法在JPA类上找到合适的构造函数

时间:2016-06-29 14:43:04

标签: java jpa jpql

我收到以下错误:

java.lang.IllegalArgumentException: org.hibernate.QueryException: unexpected char:

 SELECT NEW com.classes.applicant.ApplicantEntry(app.indSsn, app.indivName, app.indAddrLocTx,app.indAddrCityNm,app.indAdrStateAb,app.indAddrZipCd, app.phoneNr,app.workPhoneNr) FROM TApplicant app WHERE app.indSsn = :ssn

类构造函数是正确的:

public ApplicantEntry(String indSsn, String indivName, String indAddrLocTx, String indAddrCityNm, String indAdrStateAb, String indAddrZipCd,
                                 String phoneNr, String workPhoneNr) {
        this.indSsn = indSsn;
        this.indivName = indivName;
        this.indAddrLocTx = indAddrLocTx;
        this.indAddrCityNm = indAddrCityNm;
        this.indAdrStateAb = indAdrStateAb;
        this.indAddrZipCd = indAddrZipCd;
        this.phoneNr = phoneNr;
        this.workPhoneNr = workPhoneNr;

    }

实体:

@Entity
@Table(name = "T_APPLICANT", schema = "APP")
public class TApplicant implements Serializable, Applicant {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "IND_SSN", columnDefinition = "CHAR")
    private String indSsn;

我不明白为什么它会在收到所有字符串时期待所有字符串时抱怨它。

我在intellij的持久性工具中运行查询。

存储库中的完整查询:

@Query("SELECT NEW com.classes.applicant.ApplicantEntry(app.indSsn, app.indivName, "
           +"app.indAddrLocTx,app.indAddrCityNm,app.indAdrStateAb,app.indAddrZipCd, app.phoneNr,app.workPhoneNr) "
           +"FROM TApplicant app "
           +"WHERE app.indSsn = :ssn ")
    ApplicantEntry getApplicantEntry(@Param("ssn") String ssn);

2 个答案:

答案 0 :(得分:1)

  1. ApplicantEntry构造函数有8个参数,而查询中只有6个字段。

  2. 查询看起来不像正确的jpa查询。我认为应该是

    SELECT NEW org.classes.applicant.ApplicantEntry(
        app.indSsn,                     
        app.adnlPhysExamCd,
        app.adnlPhysExamDt,
        app.adultDepnQy,
        app.adultDepnQy,
        app.advRankRsnCd,
        'placeholder',
        'placeholder'
        ) 
    FROM ApplicantEntry app WHERE app.indSnn = :ssn
    

答案 1 :(得分:0)

对于使用Lombok的任何人来说,类中字段的物理顺序决定了构造函数参数的顺序。类字段的物理顺序必须与SELECT子句的顺序匹配。

@AllArgsConstructor
public class Thing {
  private String name;
  private Date birthday;
}

// not the same as...

@AllArgsConstructor
public class Thing {
  private Date birthday;
  private String name;
}