gpt4 book ai didi

java - HQL 与 JoinColumns

转载 作者:行者123 更新时间:2023-12-02 06:00:10 25 4
gpt4 key购买 nike

我是 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 类中,我尝试使用如下查询列出所有 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] 

非常感谢您的帮助。谢谢

最佳答案

阅读错误消息可以立即识别错误:

SELECT v.company.companyId.companyCodev.company.companyId.branchCodefrom

您没有看到查询中缺少一些空格吗?

关于java - HQL 与 JoinColumns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22740267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com