-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hql join and group by problem
PostPosted: Wed Dec 29, 2010 9:01 am 
Newbie

Joined: Wed Dec 29, 2010 8:58 am
Posts: 10
Hibernate version: 3.6.0

Hello, I am trying to make a query in hql. I have these beans:

Bean1

Code:
    @Entity
    @Table(name = "bean1")
    public class Bean1 {
       
           ...
           private List<Bean2> bean2;
    ...
    @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

Code:
    @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:

Code:
    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:

Quote:
org.springframework.orm.hibernate3.HibernateQueryException:
illegal attempt to dereference
collection


I tried anothe query:

Code:
    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:

Quote:
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??
Thanks in advance


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.