我是HQL的初学者,我一直坚持使用HQL检索VesselOperator记录
我的代码在下面看起来很像。
CompanyId.java
@Embeddable
public class CompanyId {
public CompanyId(){
}
@Column(name="COMPANY_CODE")
private String companyCode;
@Column(name="BRANCH_CODE")
private String branchCode;
//Getters and Setters
}
Company.java
@Entity
@Table(name = "COMPANY")
public class Company{
public Company(){
}
@EmbeddedId
private CompanyId companyId;
@OneToMany(mappedBy = "company")
private Collection<VesselOperator> vesselOperator = new ArrayList<VesselOperator>();
public CompanyId getCompanyId() {
return companyId;
}
public void setCompanyId(CompanyId companyId) {
this.companyId = companyId;
}
public Collection<VesselOperator> getVesselOperator() {
return vesselOperator;
}
public void setVesselOperator(Collection<VesselOperator> vesselOperator) {
this.vesselOperator = vesselOperator;
}
@Column(name = "COMPANY_NAME")
private String companyName;
//Getters & Setters
}
VesselOperator.java
@Entity
@Table(name = "VESSEL_OPERATOR")
public class VesselOperator{
public VesselOperator(){
}
@Id
@Column(name="OPERATOR_CODE")
private String operatorCode;
@ManyToOne
@JoinColumns({@JoinColumn(name="INTEGRATED_COMPANY_CODE", referencedColumnName="COMPANY_CODE"),
@JoinColumn(name="INTEGRATED_BRANCH_CODE", referencedColumnName="BRANCH_CODE")
})
private Company company;
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
@Column(name="OPERATOR_NAME")
private String operatorName;
@Column(name="CONTACT_PERSON_NAME")
private String contactPersonName;
//Getters and Setters
}
从我的DAO课程中,我尝试使用Query列出所有VesselOperators,如下所示
Query query = (Query) session.createQuery("SELECT " +
"v.company.companyId.companyCode" +
"v.company.companyId.branchCode" +
"from VesselOperator v");
List<Object[]> rows = query.list();
for (Object[] row : rows) {
String cmpnyCode = (String) row[0];
String cmpnyBrnch = (String) row[1];
System.out.println("cmpnyCode "+cmpnyCode +" cmpnyBrnch "+cmpnyBrnch );
}
我最终会遇到以下异常。
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: unexpected token: VesselOperator near line 1, column 74 [SELECT v.company.companyId.companyCodev.company.companyId.branchCodefrom VesselOperator v]
非常感谢帮助。谢谢
答案 0 :(得分:2)
读取错误消息会立即识别错误:
SELECT v.company.companyId.companyCodev.company.companyId.branchCodefrom
您是否发现查询中缺少某些空格?