Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3611950
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:56:19+00:00 2026-05-18T21:56:19+00:00

I am trying to make a query in hql. I have these beans: 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:56:19+00:00Added an answer on May 18, 2026 at 9:56 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a query that looks at a single table to see
I am trying to make a query to get some results: I have a
I'm trying to make a query in HQL that see if the id of
I am trying to make a simple query to a small MYSQL table, but
I have question about the sql query. I am trying to make the query
I am trying to make a query for SQL Compact It is basically these
I'm trying to make this query work, in my model Workshop I have a
I am trying to make a query string $sql = 'select * from table
I'm trying to make a query in two tables: SIMPLE_PERSON with 3 fields (name,
I'm trying to make query in a table that has a column named First

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.