Hql加入并按问题分组 [英] Hql join and group by problem
问题描述
我试图在hql中进行查询。我有这些bean:
Bean1
@Entity
@Table(name =bean1)
public class Bean1 {
...
private List< Bean2> bean2;
...
@ManyToMany(
cascade = {CascadeType.ALL},
fetch = FetchType.LAZY)
@JoinTable(
name = bean1_bean2,
joinColumns = @ JoinColumn(name =bean1_id),
inverseJoinColumns = @ JoinColumn(name =bean2_id)
)
public List< Bean2> getBean2(){
return bean2;
public void setTags(List< Bean2> bean2){
this.bean2 = bean2;
}
...
}
Bean2
@Entity
@Table(name =bean2)
public class Bean2 {
private String bean2_id;
私人列表< Bean1> bean1;
@Id
@Column(name =bean2_id,length = 100,unique = true,nullable = false)
public String getBean2_id(){
return bean2_id;
}
public void setBean2_id(String bean2_id){
this.bean2_id = bean2_id;
@ManyToMany(
cascade = {CascadeType.PERSIST,CascadeType.MERGE},
fetch = FetchType.LAZY,
mappedBy =bean2
)
public List< Bean1> getBean1(){
return bean1;
}
public void setBean1(List< Bean1> bean1){
this.bean1 = bean1;
}
}
然后查询:
查询查询= session.createQuery(SELECT bean1 FROM+
((Class)Bean1.class.getName()+ bean1+
WHERE+bean1.bean1_id =?+
JOIN bean1.bean2.bean2_id =?+
GROUP BY bean1.bean2
);
但是我得到这个异常:
org.springframework.orm.hibernate3.HibernateQueryException:
非法尝试解引用
集合
$
$ b
Query query = session.createQuery(SELECT beab1.bean2 FROM+ b $ b ((Class)Bean1.class.getName()+bean1+JOIN bean1.bean2+bean2+WHERE+bean1.bean1_id =?+AND bean2.bean2_id =? +group by bean1.bean2.bean2_id =?);
另一个例外:
org.springframework.orm.hibernate3.HibernateQueryException:
非法尝试解引用
集合[bean10_.bean1_id.bean2]
带有元素属性引用
[bean2_id]
我真正想得到的是有多少bean2_id被重复同一个bean1与给定的bean1_id。
我以正确的方式?? Hibernate 3.6.0
预先感谢
如果我正确理解了你的问题,你应该这样做:
select b1 from Bean1作为b1将b1.bean2作为b2加入其中b1.id =?1和b2.id =?2 b2.id组中的b
然而,这可能不会起作用,因为您必须通过select中的某些东西进行组合。
但以下内容应该可以工作:
se从Bean1获取b2.id作为b1加入b1.bean2作为b2其中b1.id =?1和b2.id =?2 group by b2.id
如果您想要获取b1,但要通过b2.id进行分组,您可以尝试如下所示:
从Bean1中选择b1,b2.id作为b1将b1.bean2作为b2其中b1.id =?1和b2.id =?2 b2 .id组中的b2。b
请注意,您将在Bean1的索引0处得到结果数组。
I am trying to make a query in hql. I have these beans:
Bean1
@Entity
@Table(name = "bean1")
public class Bean1 {
...
private List<Bean2> bean2;
...
@ManyToMany(
cascade={CascadeType.ALL},
fetch=FetchType.LAZY)
@JoinTable(
name="bean1_bean2",
joinColumns=@JoinColumn(name="bean1_id"),
inverseJoinColumns=@JoinColumn(name="bean2_id")
)
public List<Bean2> getBean2() {
return bean2;
}
public void setTags(List<Bean2> bean2) {
this.bean2 = bean2;
}
...
}
Bean2
@Entity
@Table(name = "bean2")
public class Bean2 {
private String bean2_id;
private List<Bean1> bean1;
@Id
@Column(name = "bean2_id", length = 100, unique = true, nullable = false)
public String getBean2_id() {
return bean2_id;
}
public void setBean2_id(String bean2_id) {
this.bean2_id = bean2_id;
}
@ManyToMany(
cascade = {CascadeType.PERSIST, CascadeType.MERGE},
fetch=FetchType.LAZY,
mappedBy = "bean2"
)
public List<Bean1> getBean1() {
return bean1;
}
public void setBean1(List<Bean1> bean1) {
this.bean1 = bean1;
}
}
And the query:
Query query = session.createQuery("SELECT bean1 FROM " +
((Class) Bean1.class.getName() + " bean1 " +
" WHERE " + "bean1.bean1_id=? " +
" JOIN bean1.bean2.bean2_id=?" +
" GROUP BY bean1.bean2"
);
But I am getting this exception:
org.springframework.orm.hibernate3.HibernateQueryException:
illegal attempt to dereference
collection
I tried anothe query:
Query query = session.createQuery("SELECT beab1.bean2 FROM " + ((Class) Bean1.class.getName() + " bean1 " + " JOIN bean1.bean2 " + "bean2" + " WHERE " + "bean1.bean1_id=? " + " AND bean2.bean2_id=? " + " group by bean1.bean2.bean2_id=?");
Another exception:
org.springframework.orm.hibernate3.HibernateQueryException:
illegal attempt to dereference
collection [bean10_.bean1_id.bean2]
with element property reference
[bean2_id]
What I really want to get is how many bean2_id are repeated in the same bean1 with a bean1_id given.
Am I in the right way?? Hibernate 3.6.0
Thanks in advance
解决方案 If I understand correctly your question, you should do something like this:
select b1 from Bean1 as b1 join b1.bean2 as b2 where b1.id = ?1 and b2.id= ?2 group by b2.id
However, this will probably won't work, since you must put in group by something from the select.
But the following should work:
select b2.id from Bean1 as b1 join b1.bean2 as b2 where b1.id = ?1 and b2.id= ?2 group by b2.id
If you want to fetch b1, but to group by b2.id, you can try something like this:
select b1, b2.id from Bean1 as b1 join b1.bean2 as b2 where b1.id = ?1 and b2.id= ?2 group by b2.id
Pay attention that you'll get array in the result with Bean1 at index 0.
这篇关于Hql加入并按问题分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!