-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: bag property fetched through sql-query is null.
PostPosted: Sat Jun 14, 2008 10:16 am 
Newbie

Joined: Tue Jun 10, 2008 3:51 am
Posts: 10
My class Status is defined as

Code:
public class Status implements Serializable {
    /**
     * Represents the entity id.
     */
    private Long Id;

    /**
     * Represents the list of the next possible statuses.
     */
    private List<Status> statuses;

    /* Accessors */


hbm file is
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="my">
   <class name="Status" table="status" lazy="true">
      <id name="Id" column="id"
         type="long" />

      <bag name="statuses">
         <key column="id" />
         <one-to-many
            class="my.Status" />
         <loader query-ref="LoaderChildCollection" />
      </bag>
   </class>

   <sql-query name="LoaderChildCollection">
      <return alias="statuses" class="my.Status">
      </return>
      SELECT * FROM
      status WHERE status.id <> ?
   </sql-query>
</hibernate-mapping>


The LoaderChildCollection should return all statuses whose id is not the entity id fetch. But in result, Status.statuses is null.

What is wrong with my configuration file? Thanks

BTW: I can get non-null result when execute LoaderChildCollection with EntityManager.createNamedQuery


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 15, 2008 5:22 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
I'm afraid this won't work. You're defining a one-to-many _relationship_ between a parent status object and its children. However, this relationship simply doesn't exist in your database.

You've told hibernate to use the ID field from status to identify the parent object (key column="id"). This is what hibernate does:

1. load the parent object, say status.1
2. run the query to initialize the collection, say status.2 and status.3 returned
3. use the ID field from status.2 to find the parent - returns status.2
4. add child status.2 to collection of parent status.2

Because your query excludes the parent object you'll get an empty list every time.

BTW, I think you're getting a null pointer exception rather than an empty list because you need to include a load-collection element in the sql-query. This is from the docs ch16:
Code:
<set name="employments" inverse="true">
    <key/>
    <one-to-many class="Employment"/>
    <loader query-ref="employments"/>
</set>

<sql-query name="employments">
*****
    <load-collection alias="emp" role="Person.employments"/>
*****
    SELECT {emp.*}
    FROM EMPLOYMENT emp
    WHERE EMPLOYER = :id
    ORDER BY STARTDATE ASC, EMPLOYEE ASC
</sql-query>


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 15, 2008 7:56 am 
Newbie

Joined: Tue Jun 10, 2008 3:51 am
Posts: 10
So it is impossible to ask hibernate retrieve status.ToStatuses auomatically with current database tables definitations. We must have two status table, one is fromStatus and the other is toStatus? Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 16, 2008 7:46 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Yes, the statuses can't be loaded as part of the object model without a relationship in place. You don't need 2 status tables, just a parent-child mapping table.

However, if the relationship is as simple as your query suggests (i.e. all statuses except this one), why not just stick to running a HQL query separate from loading the status object?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.