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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:14:09+00:00 2026-06-01T00:14:09+00:00

I have a user and a message table. User to Message are one-to-many relationships

  • 0

I have a user and a message table. User to Message are one-to-many relationships and Message to User are many-to-one relationships. I’ve marked one of the many-to-one as fetch join. When I ‘get’ a single Message, Hibernate runs a join query, but when I fetch all the messages, Hibernate runs select queries instead of a join. What could be the reason? Following are the details:

Relationship between them:

User

<set name="messagesForFromUserUid" lazy="true" table="message" inverse="true" cascade="save-update">
        <key>
            <column name="from_user_uid" not-null="true" />
        </key>
        <one-to-many class="repository.Message" />
    </set>
    <set name="messagesForToUserUid" lazy="true" table="message" fetch="select">
        <key>
            <column name="to_user_uid" not-null="true" />
        </key>
        <one-to-many class="repository.Message" />
    </set>

Message

<many-to-one name="userByFromUserUid" class="repository.User" fetch="join" lazy="false">
        <column name="from_user_uid" not-null="true" />
    </many-to-one>
    <many-to-one name="userByToUserUid" class="repository.User" fetch="select" lazy="proxy">
        <column name="to_user_uid" not-null="true" />
    </many-to-one>

When I fetch a single message object, Hibernate runs one join query as expected:

Message m = (Message) s.get(Message.class, 2);

Hibernate: 
select
    message0_.message_uid as message1_1_1_,
    message0_.from_user_uid as from2_1_1_,
    message0_.to_user_uid as to3_1_1_,
    message0_.message_text as message4_1_1_,
    message0_.created_dt as created5_1_1_,
    user1_.user_uid as user1_0_0_,
    user1_.user_name as user2_0_0_,
    user1_.user_password as user3_0_0_,
    user1_.email as email0_0_,
    user1_.first_name as first5_0_0_,
    user1_.last_name as last6_0_0_,
    user1_.created_dt as created7_0_0_ 
from
    hello.message message0_ 
inner join
    hello.user user1_ 
        on message0_.from_user_uid=user1_.user_uid 
where
    message0_.message_uid=?

But when I fetch all the messages in one go, Hibernate runs select queries instead:

List<Message> l = s.createQuery("from Message").list();

Hibernate: 
select
    message0_.message_uid as message1_1_,
    message0_.from_user_uid as from2_1_,
    message0_.to_user_uid as to3_1_,
    message0_.message_text as message4_1_,
    message0_.created_dt as created5_1_ 
from
    hello.message message0_
Hibernate: 
select
    user0_.user_uid as user1_0_0_,
    user0_.user_name as user2_0_0_,
    user0_.user_password as user3_0_0_,
    user0_.email as email0_0_,
    user0_.first_name as first5_0_0_,
    user0_.last_name as last6_0_0_,
    user0_.created_dt as created7_0_0_ 
from
    hello.user user0_ 
where
    user0_.user_uid=?

Hibernate: 
select
    user0_.user_uid as user1_0_0_,
    user0_.user_name as user2_0_0_,
    user0_.user_password as user3_0_0_,
    user0_.email as email0_0_,
    user0_.first_name as first5_0_0_,
    user0_.last_name as last6_0_0_,
    user0_.created_dt as created7_0_0_ 
from
    hello.user user0_ 
where
    user0_.user_uid=?
  • 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-01T00:14:11+00:00Added an answer on June 1, 2026 at 12:14 am

    Looks like Hibernate does not always use fetch strategies defined in the mapping, for HQL or Criteria queries. They are typically used for get/load. Found a reference here: https://forum.hibernate.org/viewtopic.php?f=1&t=957561

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

Sidebar

Related Questions

I have a Message table and a User table. Both are in separate databases.
I have one user who gets an error message when he closes his browser.
I have a User class with reference to a Message class. The message class
I have two models: class User end class Message belongs_to :sender, :class_name=> 'User' belongs_to
In my messages_controller I have the following private method: def find_message_or_404(slug) message = user.messages.find_by_slug(slug)
After user places an order I have to send detailed email message containing order
I have a WCF Service (called myservice.svc) that takes a message from a user
I have a website with a contact form. User submits name, email and message
I have a simple messaging system in Rails with a Message table for the
I have an MVC3 app, using EF, where one table winds up in a

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.