我正在使用hibernate并有三个表:Version,Location和Arc。
Version 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的两个连接列? 有任何想法吗? 非常感谢。
有人建议我在Location表中添加versionId。但是,如果我这样做,我将得到重复列的错误,因为连接列已自动创建一个versionid列。
答案 0 :(得分:1)
代码中的versionid
在哪里?我的意思是宣言。在“版本”表中声明了locationId
。但没有versionid
。检查您的查询位置
`select v.id, l.locationId from Version v`
实际上您的错误显示“无法解析属性 versionid ”。因为您首先没有声明属性versionid。