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

Ask A Question

Stats

  • Questions 311k
  • Answers 311k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The Activity Monitor relies on DMVs that were introduced in… May 13, 2026 at 10:29 pm
  • Editorial Team
    Editorial Team added an answer If you type sort - the command will accept input… May 13, 2026 at 10:29 pm
  • Editorial Team
    Editorial Team added an answer You can call Response.AppendHeader() at any time before headers are… May 13, 2026 at 10:29 pm

Related Questions

Let's say we have an interface 'IA' for which we have an implementation 'A'.
Let's say I have an interface representing a domain object: public interface IFoo {
Per an answer to a previous question (answer here: SQL/Database Views in Grails ),
Lets say I have some domain objects that will need to be serialized/packed using
In domain driven design, it appears to be a good practice to use Factories

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.