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

  • Home
  • SEARCH
  • 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 3310396
In Process

The Archive Base Latest Questions

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

I have 2 classes which have many to many relationship. I take the ‘Question’

  • 0

I have 2 classes which have many to many relationship. I take the ‘Question’ and ‘Tag’ as an example to make the case more understandable.

For each question, you have several tags. The same as for tag.

What I would like to do is to get all questions (and their corresponding tags) if the question contain a tag says “hibernate”.

I can at most do it with a SQLQuery in the many-to-many table and return a list of the question ID. Then use a criteria with a restrictions.in and grab all questions. But it’s too clumsy and I bet there is a better way of doing it, is there?

  • 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-17T21:45:19+00:00Added an answer on May 17, 2026 at 9:45 pm

    Essentially, you need to create an alias and use the alias to query the child collection like so:

    List questions = sess.createCriteria(Question.class)
        .createAlias("Tags", "t")
        .add( Restrictions.eq("t.name", "hibernate") )
        .list();
    

    I’m assuming you don’t actually have a class that represents the “bridge” table to the tags table in this scenario, otherwise you’d need to create 2 aliases eg:

    List questions = sess.createCriteria(Question.class)
            .createAlias("QuestionTag", "qt")            
            .createAlias("qt.Tags", "t")
            .add( Restrictions.eq("t.name", "hibernate") )
            .list();
    

    You can find out more from the docs:

    http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/querycriteria.html#querycriteria-associations

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 domain classes which are mapped by many-to-many relationship. I followed the
I have two related classes which share a common interface and are both stored
I have a few classes which do nothing except in their constructors/destructors. Here's an
I have a set of some classes which are all capable of being constructored
I have 6 classes, which are all document types. All the document types have
I have two Java classes: B, which extends another class A, as follows :
I have an interface called ICommand from which various classes inherit. These classes all
I have an enum which is used by multiple classes. What is the best
I have a set of XSDs from which I generate data access classes, stored
I have the following 3 rails classes, which are all stored in one table,

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.