Hibernate 如何在hql中选择联接列
我使用的是hibernate,有三个表:版本、位置和Arc bean的版本是:Hibernate 如何在hql中选择联接列,hibernate,hql,Hibernate,Hql,我使用的是hibernate,有三个表:版本、位置和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>
@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
...
}
@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;
但执行此操作时,出现错误无法解析属性:versionid
:
select v.id,l.locationId from Version v, Location l where l.versionid=v.id;
执行此操作时,我也会遇到此错误不明确的版本ID
:
select a.id, l.locationId from Arc a, Location l where versionid = '1';
我的问题是如何使用hql在Location或Arc表中获取联接列versionid,以及如何区分具有相同名称versionid的两个联接列?
有什么想法吗?
非常感谢
有人建议我应该在位置表中添加versionId。但是,如果我这样做,我将得到重复列的错误,因为联接列已经自动创建了一个versionid列。代码中的
versionid
在哪里?我指的是宣言。“版本”表中声明了locationId
。但是没有versionid
。在你说的地方检查你的问题
`select v.id, l.locationId from Version v`
事实上,您的错误是“无法解析属性versionid”。因为您首先没有声明属性versionid。它不应该是
v.id=versionid
?另外,您的版本ID在代码中的什么位置?“版本”表中声明了locationId
。但是没有versionid
。locationId在location表中,versionid是在Version中声明的联接列。实际上,我无法在location表中添加versionid,因为它会导致列重复