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 6925019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:44:09+00:00 2026-05-27T10:44:09+00:00

I have the following tables: [ table : column1, column2 ] A : id,

  • 0

I have the following tables:

[ table : column1, column2 ]

A : id, name

B : id, name

AB : idA, idB

AB is a join table.

Then I have this method on hibernate class B

@OneToMany( fetch = FetchType.EAGER)
@JoinTable( name = "AB", 
            joinColumns = @JoinColumn( name = "idB"),
            inverseJoinColumns = @JoinColumn( name = "idA") )
public List<A> getAs(){
    //return the list of matching stuff
}

This works perfectly fine.

Now I want to do this sql query in hibernate:

select * from B inner join AB on B.id = AB.idB where AB.idA = 1234

Essentially, ‘list me all B’s that reference A with id 1234’

I could do straight sql, but that would defeat the purpose of getAs()

Is it possible to construct a Criterion/Restriction clause to achieve this?

  • 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-27T10:44:09+00:00Added an answer on May 27, 2026 at 10:44 am

    Relationship between A and B is not one-to-many in this case, but rather many-to-many. You should map it as such:

    @ManyToMany
    @JoinTable(name = "AB", 
                joinColumns = @JoinColumn( name = "idB"),
                inverseJoinColumns = @JoinColumn( name = "idA") )
    public List<A> getAs(){
        //return the list of matching stuff
    }
    

    Note that eagerly fetching a collection is not a good idea in most cases, hence fetch = FetchType.EAGER removed above. You can now do the same on A side to make relationship bidirectional:

    @ManyToMany(mappedBy='As')
    public List<B> getBs(){
        //return the list of matching stuff
    }
    

    Now getting all Bs for given A is just a matter of calling getBs() on that A instance. You can create criteria / write HQL to do that as well – from either side.

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

Sidebar

Related Questions

I have a table like this: ID Name Column1 Column2 Column3 1 ABC 202.2
I have the following 2 entities: @Entity(name = Employee) @Table(name = EMPLOYEE) public class
I have following DQL (Doctrine1.2): $dql->select('sum(t.column1) column1, t.column2') ->from('Table t') ->groupBy('t.column2') ->orderBy('column1'); But the
I have the following tables - groups, contacts, contacts_groups (habtm join table) groups &
I have the following tables: Table categories { -id- -name- 1 Action 2 Comedy
I have a Name table with the columns NameID Name TypeID With the following
I have the following tables defined declaratively (very simplified version): class Profile(Base): __tablename__ =
I have the following 3 tables: 1) Sweetness Table FruitIndex CountryIndex Sweetness 1 1
I have the following entity (getters and setters ommited)... @Entity @Table(name = TBL_PROJECT_RUN) public
I have the following tables in Oracle 11g: Table A SITE_ID SITE_NAME VECTOR .........MANY

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.