-->
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.  [ 3 posts ] 
Author Message
 Post subject: Collection property not populating but records exist--SOLVED
PostPosted: Sun Jun 12, 2005 12:16 am 
Newbie

Joined: Sat Jun 11, 2005 11:49 pm
Posts: 2
I'm going nuts trying to get NHibernate do one of the most basic things - automatically having a collection property of a persisted "parent" object populated with associated "child" objects. I can load the parent object from the database using ISession.Load, but when I access the set of children, it's always empty, even though records exist in the database.

I have an Oracle database that has "Campus" and "Building" tables. A Building is associated with exactly one Campus via a foreign CampusId key in the Building table, and a Campus can have many Buildings. Right now I just have one campus with ID 7 and two buildings with CampusId = 7.

I have successfully been able to create both Campus and Buildings with parent-child associations and persist them using NHibernate. But I haven't been able to just fetch the collection of Buildings on a Campus object - it's always empty when I load a Campus.

My entity classes look like this:
Code:
public class Building
  {
    private long buildingId;
    private Campus campus;
    private string buildingName;

    public Building()
    {
    }

    public long BuildingId
    {
      get { return buildingId; }
      set { buildingId = value; }
    }

    public Campus Campus
    {
      get { return campus; }
      set { campus = value; }
    }

    public string BuildingName
    {
      get { return buildingName; }
      set { buildingName = value; }
    }
  }

  public class Campus
  {
    private long campusId;
    private string campusName;
    private ISet buildings;

    public Campus()
    {
    }

    public long CampusId
    {
      get { return campusId; }
      set { campusId = value; }
    }

    public string CampusName
    {
      get { return campusName; }
      set { campusName = value; }
    }

    public ISet Buildings
    {
      get { return buildings; }
      set { buildings = value; }
    }

    public void AddBuilding( Building b )
    {
      if( Buildings == null )
        Buildings = new HashedSet();
      Buildings.Add( b );
      b.Campus = this;
    }
  }
The mapping files look like this:
Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="gbrownc.LockerService.Persistence.Building, gbrownc.LockerService" table="Building">
    <id name="BuildingId" unsaved-value="0" type="Int64">
      <generator class="native">
        <param name="sequence">Building_seq</param>
      </generator>
    </id>
    <many-to-one name="Campus" column="CampusId"/>
    <property name="BuildingName" type="String" length="64"/>
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="gbrownc.LockerService.Persistence.Campus, gbrownc.LockerService" table="Campus">
    <id name="CampusId" unsaved-value="0" type="Int64">
      <generator class="native">
        <param name="sequence">Campus_seq</param>
      </generator>
    </id>
    <property name="CampusName" type="String" length="64"/>
    <set name="Buildings" inverse="true" lazy="false">
      <key column="CampusId"/>
      <one-to-many class="gbrownc.LockerService.Persistence.Building, gbrownc.LockerService"/>
    </set>
  </class>
</hibernate-mapping>


I'm just doing this:
Code:
Campus campus = session.Load( typeof( Campus ), 7 ) as Campus;
buildingCount = campus.Buildings.Count; // always 0! :(
I've tried several things: using an IList instead of ISet, playing around with the "lazy", "inverse", and "outer-join" settings, using an IEnumerator on the set instead of the Count property... Everything I've tried gives me a set (or list) with zero elements.

I must be making some really stupid mistake! Does anyone have any ideas?


Last edited by vtowel on Sun Jun 12, 2005 12:49 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Reason has to do with Int64/Int32 incompatibility
PostPosted: Sun Jun 12, 2005 12:49 am 
Newbie

Joined: Sat Jun 11, 2005 11:49 pm
Posts: 2
I solved it!

It has to do with the fact that the CampusId is declared as long/Int64 in the class and in the mapping file, but when I loaded it using Session.Load, I passed a CampusId of type int/Int32 - the literal "7". (I found out this was the problem by surrounding the Session.Load command with an ITransaction and committing it at the end, which produced a HibernateException saying that the Campus object had its CampusId changed from Int32 to Int64 during the session.)

When I passed Session.Load a CampusId of type long/Int64 - the literal "7l" (7 el) - the collection of Buildings was finally loaded properly. Phew!

I wonder if this happens in Hibernate too? I'm not sure whether Java distinguishes ints and longs the same way .NET does.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 12, 2005 4:22 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
In Java you would have to box it manually, i.e. choose between new Integer(7) or new Long(7L), so it would be kind of obvious.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.