Hql加入并按问题分组

编程入门 行业动态 更新时间:2024-10-14 18:17:38
本文介绍了Hql加入并按问题分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图在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加入并按问题分组

本文发布于:2023-11-22 03:47:06,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并按   Hql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!