开发者

Hql join and group by problem

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) {
        th开发者_如何学Pythonis.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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜