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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:13:12+00:00 2026-05-13T19:13:12+00:00

Say I have domain objects corresponding to Posts and Users . Nevertheless, I have

  • 0

Say I have domain objects corresponding to Posts and Users. Nevertheless, I have corresponding database tables containing information relevant to “posts” and “users”.

Currently I have properly set up the mapping in Hibernate so that I can pull the info from the “posts” table and save it as a Post object. However, I then added to my Posts domain object, a reference to a User object (so that each post can have a corresponding User object).

In my database structure, the “posts” table has a user_id column which is a foreign key into the “users” table.

My question is: when querying the “posts” table in my DAO, do I have to join on the “users” table and then somehow cast the returned user data into a User object? Or can I simply leave my query as it is (i.e. just querying the “posts” table), and somehow add a hibernate mapping so that the User attribute in the Posts table gets populated? (I guess I am wondering if Hibernate can automatically generate the join for me if I set up the mapping properly – examples would be great too!)

Thanks, and I hope I was clear enough.

Update: Just to clarify, here are some code snippets:

My Post Object:

public class Posts {

  private String title;
  ...
  private User user;

  //getters and setters here
}

My Post table columns:

post_id (primary key)
title
...
user_id (foreign key into User table)

My mapping (without taking into account the User attribute) currently looks like this:

<class name="com...Post" table="post">
    <id name="pId" column="post_id" />
    <property name="title" column="title" type="java.lang.String" />
    ...
            <!-- Need to add mapping here to join with user table?? -->
</class>

So basically, my DAO currently fetches a Post object without the private User user attribute (since I just added this). My question was how do I populate that attribute in the Post object (so that the fetched Post object also contains a User object)?

Sorry if the current posts already answered this…they were just slightly confusing to me..

  • 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-13T19:13:12+00:00Added an answer on May 13, 2026 at 7:13 pm

    Update: Well, you got a confusing answer first because you asked a confusing question… The answer to your renewed question is indeed to define a many to one mapping for your Post class (as others have already mentioned). Now, if you want to fetch the whole stuff with a single join query, you write:

    <many-to-one name="user" class="User" column="user_id" fetch="join" />
    

    Original post:

    By default, Hibernate fetches lazily. In fact, you need to touch the lazy property only if you want eager fetching. Rough example for the behaviour of the default lazy fetch plan:

    Post post = (Post) session.load(Post.class, new Long(123));
    // at this point, post refers to a proxy object created by Hibernate
    // in the background - no post or user data has been loaded from DB
    post.getId();
    // post still refers to the proxy object
    User user = post.getUser();
    // post is now loaded, but user not - it refers to a proxy object
    String name = user.getName(); // Now the user data is loaded from DB
    

    So if you are happy with multiple queries, you don’t need to do anything special. OTOH if you want to fetch all post and user data in a join query, you need to set the attribute fetch="join" in your mapping for theuser` property.

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

Sidebar

Related Questions

So let's say we have a domain object such as the following public class
Let's say I have an interface representing a domain object: public interface IFoo {
I have a basic domain object, say like Person or Campaign or Event that
Let's say you have a domain entity User and you want to support the
Let's say have something like: SELECT energy_produced, energy_consumed, timestamp1 AS timestamp FROM ( SELECT
Say we have the following method: private MyObject foo = new MyObject(); // and
Say I have the following file structure: app/ app.py controllers/ __init__.py project.py plugin.py If
Say you have an application divided into 3-tiers: GUI, business logic, and data access.
Say I have an ASMX web service, MyService. The service has a method, MyMethod.
Say I have a class named Frog, it looks like: public class Frog {

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.