开发者

Hibernate not loading associated object

i am trying to load a hibernate object ForumMessage but in it contain another object Users and the Users object is not being loaded.

My ForumMessage Mapping File:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jan 4, 2011 10:10:29 AM by Hibernate Tools 3.4.0.Beta1 -->
<hibernate-mapping>
    <class name="com.BiddingSystem.Models.ForumMessage" table="FORUMMESSAGE">
        <id name="ForumMessageId" type="long">
  开发者_高级运维          <column name="FORUMMESSAGEID" />
            <generator class="native" />
        </id>
        <property name="ForumMessage" type="java.lang.String">
            <column name="FORUMMESSAGE" />
        </property>
        <many-to-one name="User" class="com.BiddingSystem.Models.Users" fetch="join">
            <column name="UserId" />
        </many-to-one>
        <property name="DatePosted" type="java.util.Date">
            <column name="DATEPOSTED" />
        </property>
        <many-to-one name="Topic" class="com.BiddingSystem.Models.ForumTopic" fetch="join">
            <column name="TopicId" /> 
        </many-to-one>
    </class>
</hibernate-mapping>

and I am using the follwing code:

Session session = gileadHibernateUtil.getSessionFactory().openSession();

            SQL="from ForumMessage";


    System.out.println(SQL);
    Query query=session.createQuery(SQL);
    System.out.println(query.list().size());
    return new LinkedList <ForumMessage>(query.list());


<many-to-one name="User" class="com.BiddingSystem.Models.Users" fetch="join" lazy="false">

You need to add lazy="false" as well.


You can add lazy="false" to the many-to-one mapping which will load the users when the ForumMessage is loaded. Alternatively you could initialize the users list using Hibernate.initialize(). Just make sure you do this before you close the session.

Session session = gileadHibernateUtil.getSessionFactory().openSession();
string sql = "from ForumMessage";
Query query = session.createQuery(sql);
List results = query.list()
for(ForumMessage message : results)
{
    Hibernate.initialize(message.User);
}
return new LinkedList <ForumMessage>(results);

You should only do one of these though if you have a need to. Hibernate by default lazy loads objects to avoid unnecessary calls to the database. For example:

public LinkedList getMessages()
{
    //It's assumed the session is opened and closed elsewhere.
    string sql = "from ForumMessage";
    Query query = session.createQuery(sql);
    List results = query.list();
    //The overhead of extra calls to the database occur here.
    //This would have a similar impact if lazy load is set to false.
    for(ForumMessage message : results)
    {
        Hibernate.initialize(message.User);
    }
    return new LinkedList <ForumMessage>(results);
}

public void printMessages()
{
    LinkedList messages = getMessages();
    for(ForumMessage message : messages)
    {
        System.out.println(message.ForumMessage);
    }
}

In the above code sample the overhead is incurred for loading all the Users objects but those objects are never used. If Hibernate's lazy-loading were used then this extra overhead would not be incurred. In the following example the list of users isn't loaded until the list is used. This way calls are not made to the database until the data is actually needed.

public LinkedList getMessages()
{
    //It's assumed the session is opened and closed elsewhere.
    string sql = "from ForumMessage";
    Query query = session.createQuery(sql);
    List results = query.list();
    return new LinkedList <ForumMessage>(results);
}

public void printMessages()
{
    LinkedList messages = getMessages();
    for(ForumMessage message : messages)
    {
        //Hibernate will load the users objects here when they are accessed.
        for(Users user : message.User)
        {
            System.out.println(user);
        }
    }
}

One point to be careful of when lazy loading is all loading must be done in an active session. If you don't have an active session and you try and access something that has not yet been loaded Hibernate will throw a LazyInitializationException.

In addition, using Hibernate's lazy load functionality complies more with the idea of persistence ignorance where as using Hibernate.initialize() does not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜