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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:33:32+00:00 2026-06-15T00:33:32+00:00

I have 2 entities as Company and User. User –> ManyToOne –> Company SQL

  • 0

I have 2 entities as Company and User.

User –> ManyToOne –> Company

SQL
select * from user where companyid in (select companyid  from Company where companyName = ‘Company1′)

Now I can write this SQL in below 2 ways

1) Using Detached Criteria

     DetachedCriteria subCriteria = DetachedCriteria.forClass(Company.class);
     subCriteria.add(Restrictions.eq(" companyName ", companyName));
     subCriteria.setProjection(Property.forName("companyid") );

     Criteria criteria = session.createCriteria(User.class);
     criteria.add(Subqueries. propertyIn("companyid", subCriteria));
     criteria.list();

2) Using Criteria as

 session.createQuery("From User u Where u.company.name like 'Company1'").list();

Question here is How many Objects will be loaded in hibernate Session ?
Does Company Object will be loaded into hibernate session ?

  • 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-06-15T00:33:34+00:00Added an answer on June 15, 2026 at 12:33 am

    Both queries will load the found users in session. If the ManyToOne association is eagerly loaded (which is true by default), then each user’s company will also be loaded in the session. If it’s configured as lazy, then no company will be loaded in the session.

    Note that the first query is a criteria query, whereas the second one is a HQL query (and not a Criteria query as you’re saying). The first query is also too complex. You don’t need any subquery (as the HQL query shows). You could simply do

    Criteria criteria = session.createCriteria(User.class, "user");
    criteria.createAlias("user.company", "company");
    criteria.add(Restrictions.eq("company.name", theCompanyName));
    return criteria.list();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 entities, let's say Client and Company (both subclass of User ).
I have the following entities: User Company Organization Users need to be able to
I have 3 entities: Company, Department, and User User has a one to many
I have two entities: Company [companyId (PK)] and Employee [employeeId (PK), companyId (PK, FK)].
I have the following entities in my system: COMPANY , POSITION , APPLICANTS .
all my entities have a common property which specifies the company they belong to,
Good afternoon, I have three entities (that concern this question) Company (ID, etc..) CompanyAddress
I have a simple query select t.firstname, t.lastname, t.zipcode, t.city from name_data t where
In a typical organization application i create, entities User , Projects , Company ,
How to I represent the following datamodel in sql tables. I have 3 entities,

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.