命名查询subselect中的HQL错误

时间:2013-07-17 08:46:23

标签: java hibernate jpa hql

这个HQL会导致非常有用的“命名查询错误”消息

    "FROM courseform c WHERE c.application.application IN 
(SELECT a.application FROM application a WHERE a.applicant=: applicant)"

CourseForm与应用程序具有OneToOne单向关系(如果有帮助,这可能会成为双向关系)。反过来,应用程序与申请人具有相同的单向OneToOne关系。一个申请人可以有很多申请。

以下是(删节)定义

CourseForm

@Entity
@NamedQueries({ 
                @NamedQuery(name = "CourseForm.findByApplicant",
 query = "FROM courseform c WHERE c.application.application IN 
(SELECT a.application FROM application a WHERE a.applicant=: applicant)") })
public class CourseForm implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
long id;

@Enumerated(EnumType.STRING)
private Career career;
private String academicPlan;
private String courseName;
@Enumerated(EnumType.STRING)
private ModeOfAttendance modeOfAttendance;

@OneToOne
@JoinColumn(name = "application_fk")
    private Application application;
...
}

应用

@Entity
public class Application implements Serializable {


@Id
@GeneratedValue(strategy = GenerationType.AUTO)
long applicationNumber;

@OneToOne
@JoinColumn(name = "applicant_fk")
private Applicant applicant;

@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
private LocalDateTime lastUpdate = LocalDateTime.now();
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
private LocalDateTime submitted = null;

public Application() {
}

申请人

@Entity
    @NamedQueries({ @NamedQuery(name = "Applicant.findByApplicantID", 
query = "FROM Applicant a WHERE a.applicantID= :applicantID") })
public class Applicant implements Serializable {

    private static final long serialVersionUID = -7210042752148566673L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    long id;
    private String applicantID;

    Applicant() {
    }

3 个答案:

答案 0 :(得分:1)

好吧,据马克说,正确答案是

 FROM courseform c WHERE c.application.applicant =: applicant

答案 1 :(得分:0)

我认为错误是FROM courseform c WHERE c.application.application。您在c.application.application子句中使用了where 它应该是FROM courseform c WHERE c.application

答案 2 :(得分:0)

您可以更改为:

FROM courseform c WHERE c.application.applicationNumber IN (SELECT a.applicationNumber FROM application a WHERE a.applicant=: applicant)