views:

130

answers:

3

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..

A: 

You can do that by using the lazy property which is not activated by default. See some examples here http://www.javalobby.org/java/forums/t20533.html

juniorbl
Lazy fetching is the default behaviour - as stated even in the article you linked.
Péter Török
You're right, my bad.
juniorbl
A: 

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.

Péter Török
+1  A: 

If I understand your question correctly, I believe you're looking for the many-to-one mapping (many Posts to one User). Add the following to your mapping for the Post object:

<many-to-one name="user" class="User" column="user_id" lazy="false" />
kem
Agree to above answer. The mapping is necessary to fetch User record.
Padmarag