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: Collection key returns null
PostPosted: Tue Aug 26, 2008 8:04 am 
Newbie

Joined: Tue Aug 26, 2008 7:25 am
Posts: 12
Hello folks,

I need to find the foreign key (collection key) of a OneToMany relation.

I have the following collection owner mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Database.Core.TimeZone,Database" table="TimeZone">

<id name="TimeZoneId" column="TIME_ZONE_ID" type="Int16" unsaved-value="0">
<generator class="native"/>
</id>
<bag name="AnnotationListCreated" inverse="true" lazy="true" >
<key column="CREATION_TIME_ZONE_ID" />
<one-to-many class="Database.Core.Annotation,Database" />
</bag>
<bag name="AnnotationListModified" inverse="true" lazy="true" >
<key column="MODIFIED_TIME_ZONE_ID" />
<one-to-many class="Database.Core.Annotation,Database" />
</bag>

<property column="TIME_ZONE_NAME" type="String" name="TimeZoneName" not-null="true" length="100" />

</class>
</hibernate-mapping>

...and the Annotation class mapping xml is:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Database.Core.Annotation,Database" table="Annotation">

<id name="Annotationid" column="ANNOTATIONID" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property column="ANNOTATION" type="String" name="Annotation" length="1000" />
<property column="CREATION_TIME_UTC" type="DateTime" name="CreationTimeUtc" not-null="true" />
<property column="MODIFIED_TIME_UTC" type="DateTime" name="ModifiedTimeUtc" not-null="true" />
<many-to-one name="RecordingDetailId" column="RECORDING_DETAIL_ID" class="Database.Core.RecordingDetails,Database" />
<many-to-one name="CreationTimeZoneId" column="CREATION_TIME_ZONE_ID" class="Database.Core.TimeZone,Database" />
<many-to-one name="ModifiedTimeZoneId" column="MODIFIED_TIME_ZONE_ID" class="Database.Core.TimeZone.Database" />

</class>
</hibernate-mapping>

Now my problem is I can't get the column key from the 2 Collection (Bag) objects which has the Owner set to TimeZone and the element 'ReturnedClass' to Annotation. The Key property of the Collection object is always null.

How can I get the collection key?? Has it anything to do with the fact that the collections are marked as "inverse"? If yes, how could I get the collection key without changing anything in the mapping files?

Thanks,
Bogdan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2008 5:11 am 
Newbie

Joined: Tue Aug 26, 2008 7:25 am
Posts: 12
Hello again,

no answer, nobody knows or is it such a stupid question as it doesn't worth the effort?


Well, meantime I found that if I get the CollectionMappings list after the session is opened, the Key property (or getKey () in Hibernate) is no more null. Below are the lines I used, I need a sorted list of these collections, that's why I copy the CollectionMapping list in a sorted list.

(I use NHibernate, I forgot to mention, and the NHibernate 2.0 library)

<<
ISession s = SessionFactory.OpenSession();

m_sortedCollectionsByChildName = new List<Collection>(Config.CollectionMappings);

(m_sortedCollectionsByChildName as List<Collection>).Sort(new CollectionByChildComparer());

s.Close();
>>

If I remove the first and last lines (opening and closing the session), the Key property of every Collection object in the Config.CollectionMappings list is null. (the Config object is initialized as "new Configuration().Configure();")

Does anybody knows why the session must be opened in order to have the Collection.Key property not null? This way an extra time is introduced to connect to the session. CollectionMappings list should be part of metadata so it shouldn't have to connect to the session in order to create this property.


Thanks,
Bogdan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2008 5:23 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I suppose you have t build the session factory before you can access the meta information. Probably this is done implicitly in the OpenSession() call.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2008 9:16 am 
Newbie

Joined: Tue Aug 26, 2008 7:25 am
Posts: 12
Thanks for the tip,

Yes, finally it turned up that the session factory had to be build in order for that Key member to be not null. So not the session be opened but just the session factory be created (which, of course was created when I opened the session in my previous approach).

Anyways, all the other meta informations are available if I just use the new Configuration().Configure().CollectionsMapping, without creating the session factory first.
It's just the "TableCollection" and "Key" properties of every Collection object from the CollectionsMapping that are null if the session factory is not created before. Strange, maybe it's a bug, or has something to do with the "inverse" bag.


Thanks,
Bogdan


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.