【发布时间】:2026-02-16 11:45:01
【问题描述】:
我正在使用 hibernate 并且有三个表:Version、Location 和 Arc。
版本 bean 是:
@Entity
@Table(name = "version")
public class Version {
private String id;
private List<Location> locations = new ArrayList<Location>();
private List<Arc> arcs = new ArrayList<Arc>();
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "versionid", nullable = false)
public List<Location> getLocations() {
return locations;
}
public void setLocations(List<Location> locations) {
this.locations = locations;
}
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "versionid", nullable = false)
public List<Arc> getArcs() {
return arcs;
}
public void setArcs(List<Arc> arcs) {
this.arcs = arcs;
}
//other setters and getters
...
}
位置 bean 是:
@Entity
@Table(name = "location")
public class Location {
private int id;
private String locationId;
//other getters and setters
...
}
Arc bean 是:
@Entity
@Table(name = "arc")
public class Arc {
private int id;
//other setters and getters
...
}
现在我正在尝试使用 hql 访问数据。我可以这样查询:
select v.id,l.locationId from Version v, Location l where versionid=v.id;
但是当我这样做时,我得到了错误could not resolve property: versionid:
select v.id,l.locationId from Version v, Location l where l.versionid=v.id;
当我这样做时,我也会收到此错误ambiguous versionid:
select a.id, l.locationId from Arc a, Location l where versionid = '1';
我的问题是如何使用 hql 获取 Location 或 Arc 表中的连接列 versionid 以及如何区分两个具有相同名称的连接列 versionid? 有任何想法吗? 非常感谢。
有人建议我应该在位置表中添加 versionId。但是,如果我这样做,我会得到重复列的错误,因为连接列已经自动创建了一个 versionid 列。
【问题讨论】:
-
不应该是
v.id = versionid吗?另外,您的 versionid 在代码中的什么位置?在“版本”表中声明了locationId。但没有versionid。 -
locationId在location表中,versionid是Version中声明的join列。