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: Mapping to a Dictionary (using map tag)
PostPosted: Thu Aug 10, 2006 1:32 pm 
Beginner
Beginner

Joined: Tue Sep 14, 2004 1:03 pm
Posts: 33
Location: Calgary, Alberta Canada
I'm trying to set up the mapping for a couple of classes. The one class has a Dictionary<Guid, Factor> to hold a collection of factors. However, I seem to be getting some issues with the mapping for the Guideline. The issue seems to be the <map> tag as if I comment it out everything works fine. I am accessing the dictionary of factors through the field.

The mapping for the Factor is fine as that works on its own, so therefore it has to be in the <map> tag for the guideline.

Any help is appriciated.

Hibernate version: NHibernate 1.2 Alpha1

Mapping documents:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="amec.ee.sage.domain.gs.Guideline, Sage.Domain"
table="GS.GUIDELINES"
lazy="false">
<id name="GuidelineID"
column="GUIDELINE_ID"
type="System.Int32"
unsaved-value="0">
<generator class="identity" />
</id>
<property name="Author"
column="AUTHOR"
type="System.String"
not-null="true"
length="50"
unique="false" />
<property name="Date"
column="DATE"
type="System.DateTime"
not-null="true"
unique="false" />
<property name="IsRegulated"
column="IS_REGULATED"
type="System.Boolean"
not-null="true"
unique="false" />
<property name="Title"
column="TITLE"
type="System.String"
not-null="true"
length="200"
unique="false" />
<property name="RowGuid"
column="ROWGUID"
type="System.Guid"
not-null="true"
unique="true" />
<map name="factors"
table="GS.FACTORS"
inverse="false"
cascade="none"
sort="unsorted"
lazy="false"
access="field">
<key column="GUIDELINE_ID"/>
<index column="ROWGUID"
type="System.Guid" />
<one-to-many class="amec.ee.sage.domain.gs.Factor, Sage.Domain" />
</map>
</class>

<class name="amec.ee.sage.domain.gs.Factor, Sage.Domain"
table="GS.FACTORS"
lazy="false" >
<id name="FactorID"
column="FACTOR_ID"
type="System.Int32"
unsaved-value="0"
access="property">
<generator class="identity"/>
</id>
<property name="TimeStamp"
column="VERSION"
type="System.Byte[]"
not-null="true"
unique="true"/>
<property name="Name"
column="NAME"
type="System.String"
length="100"
not-null="true"
unique="false" />
<property name="Description"
column="DESCRIPTION"
type="System.String"
length="1000"
not-null="false"
unique="false" />
<property name="Value"
column="VALUE"
type="System.String"
length="150"
not-null="true"
unique="false" />
<property name="RowGuid"
column="ROWGUID"
type="System.Guid"
not-null="false"
unique="true"
access="property"/>
<component name="Type"
class="amec.ee.sage.domain.gs.FactorType, Sage.Domain"
insert="false"
update="false">
<property name="TypeID"
column="TYPE_ID"
type="System.String"
length="5"
not-null="true"
unique ="true" />
<property name="Name"
column="NAME"
type="System.String"
length="35"
not-null="true"
unique="true" />
<property name="Description"
column="DESCRIPTION"
type="System.String"
length="250"
not-null="false"
unique="false" />
</component>
</class>


Tables:
GS.GUIDELINES
pk GUIDELINE_ID
...

GS.FACTORS
pk FACTOR_ID
fk GUIDELINE_ID
...
[b]

[b]Code between sessionFactory.openSession() and session.close():
session.Load<Guideline>(1)


Full stack trace of any exception that occurs:
NHibernate.MappingException: Invalid mapping information specified for type amec.ee.sage.domain.gs.Guideline, check your mapping file for property type mismatches

at NHibernate.Persister.Entity.AbstractEntityPersister.SetPropertyValues(Object obj, Object[] values) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs:line 204
at NHibernate.Impl.SessionImpl.InitializeEntity(Object obj) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Impl\SessionImpl.cs:line 2844
at NHibernate.Loader.Loader.InitializeEntitiesAndCollections(IList hydratedObjects, Object resultSetId, ISessionImplementor session) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Loader\Loader.cs:line 518
at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Loader\Loader.cs:line 453
at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Loader\Loader.cs:line 182
at NHibernate.Loader.Loader.LoadEntity(ISessionImplementor session, Object id, IType identifierType, Object optionalObject, Type optionalEntityName, Object optionalIdentifier, IEntityPersister persister) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Loader\Loader.cs:line 1405
at NHibernate.Loader.Entity.AbstractEntityLoader.Load(ISessionImplementor session, Object id, Object optionalObject, Object optionalId) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Loader\Entity\AbstractEntityLoader.cs:line 46
at NHibernate.Loader.Entity.AbstractEntityLoader.Load(Object id, Object optionalObject, ISessionImplementor session) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Loader\Entity\AbstractEntityLoader.cs:line 41
at NHibernate.Persister.Entity.AbstractEntityPersister.Load(Object id, Object optionalObject, LockMode lockMode, ISessionImplementor session) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs:line 2055
at NHibernate.Impl.SessionImpl.DoLoad(Type theClass, Object id, Object optionalObject, LockMode lockMode, Boolean checkDeleted) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Impl\SessionImpl.cs:line 2700
at NHibernate.Impl.SessionImpl.DoLoadByClass(Type clazz, Object id, Boolean checkDeleted, Boolean allowProxyCreation) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Impl\SessionImpl.cs:line 2469
at NHibernate.Impl.SessionImpl.Load(Type clazz, Object id) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Impl\SessionImpl.cs:line 2337
at NHibernate.Impl.SessionImpl.Load[T](Object id) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Impl\SessionImpl.cs:line 2345
at amec.ee.sage.persistence.nhbr.NHibernateDao`1.get(Int32 id) in NHibernateDao.cs:line 148
at amec.ee.sage.persistence.test.nhbr.gs.TestGuidelineDao.testGetByID() in TestGuidelineDao.cs:line 25
[b]

[b]Name and version of the database you are using:
Sql2005


The generated SQL (show_sql=true):
NHibernate: SELECT guideline0_.GUIDELINE_ID as GUIDELINE1_0_, guideline0_.TITLE as TITLE2_0_, guideline0_.AUTHOR as AUTHOR2_0_, guideline0_.IS_REGULATED as IS4_2_0_, guideline0_.DATE as DATE2_0_, guideline0_.ROWGUID as ROWGUID2_0_ FROM GS.GUIDELINES guideline0_ WHERE guideline0_.GUIDELINE_ID=@p0
@p0 = '2'


Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 10, 2006 3:25 pm 
Beginner
Beginner

Joined: Tue Sep 14, 2004 1:03 pm
Posts: 33
Location: Calgary, Alberta Canada
OK, One problem was the field. I changed it to IDictionary<Guid, Factor> and that seemed to cure the last error. However, now I get the following:

NHibernate.ADOException: could not initialize a collection: [amec.ee.sage.domain.gs.Guideline.Factors#2]

at NHibernate.Loader.Loader.LoadCollection(ISessionImplementor session, Object id, IType type) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Loader\Loader.cs:line 1509
at NHibernate.Loader.Collection.CollectionLoader.Initialize(Object id, ISessionImplementor session) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Loader\Collection\CollectionLoader.cs:line 36
at NHibernate.Persister.Collection.AbstractCollectionPersister.Initialize(Object key, ISessionImplementor session) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Persister\Collection\AbstractCollectionPersister.cs:line 305
at NHibernate.Impl.SessionImpl.InitializeCollection(IPersistentCollection collection, Boolean writing) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Impl\SessionImpl.cs:line 4307
at NHibernate.Collection.AbstractPersistentCollection.ForceInitialization() in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Collection\AbstractPersistentCollection.cs:line 433
at NHibernate.Impl.SessionImpl.InitializeNonLazyCollections() in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Impl\SessionImpl.cs:line 4117
at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Loader\Loader.cs:line 188
at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Loader\Loader.cs:line 1642
at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Loader\Loader.cs:line 1593
at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet querySpaces, IType[] resultTypes) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Loader\Loader.cs:line 1587
at NHibernate.Hql.QueryTranslator.List(ISessionImplementor session, QueryParameters queryParameters) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Hql\QueryTranslator.cs:line 999
at NHibernate.Impl.SessionImpl.Find(String query, QueryParameters parameters, IList results) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Impl\SessionImpl.cs:line 1776
at NHibernate.Impl.SessionImpl.Find[T](String query, QueryParameters parameters) in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Impl\SessionImpl.cs:line 1744
at NHibernate.Impl.QueryImpl.List[T]() in J:\EDMGProjects\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Impl\QueryImpl.cs:line 45
at amec.ee.sage.persistence.nhbr.NHibernateDao`1.get(Guid rowguid) in NHibernateDao.cs:line 119
at amec.ee.sage.persistence.test.nhbr.gs.TestGuidelineDao.testGetByRowGuid() in TestGuidelineDao.cs:line 37

Sql from NHibernate:
NHibernate: select guideline0_.GUIDELINE_ID as GUIDELINE1_, guideline0_.TITLE as TITLE2_, guideline0_.AUTHOR as AUTHOR2_, guideline0_.IS_REGULATED as IS4_2_, guideline0_.DATE as DATE2_, guideline0_.ROWGUID as ROWGUID2_ from GS.GUIDELINES guideline0_ where (guideline0_.ROWGUID=@p0)
@p0 = '33233926-7565-4565-9b37-a57bbb5db64f'

NHibernate: SELECT factors0_.GUIDELINE_ID as GUIDELINE8___1_, factors0_.FACTOR_ID as FACTOR1_1_, factors0_.ROWGUID as ROWGUID__1_, factors0_.FACTOR_ID as FACTOR1_0_, factors0_.NAME as NAME3_0_, factors0_.VERSION as VERSION3_0_, factors0_.TYPE_ID as TYPE7_3_0_, factors0_.DESCRIPTION as DESCRIPT4_3_0_, factors0_.VALUE as VALUE3_0_, factors0_.ROWGUID as ROWGUID3_0_ FROM GS.FACTORS factors0_ WHERE factors0_.GUIDELINE_ID=@p0
@p0 = '2'


Top
 Profile  
 
 Post subject: Fixed
PostPosted: Thu Aug 10, 2006 6:13 pm 
Beginner
Beginner

Joined: Tue Sep 14, 2004 1:03 pm
Posts: 33
Location: Calgary, Alberta Canada
OK, I've managed to fix it. (the issue in the second post)

The with not initializing the collection was related to the Factor class and mapping. It seems that the <one-to-many> tag used in the map did not like the <component> tag in the factor mapping. As such, the Type is not mapped as a component anymore. Instead I've created a mapping for a FactorType and used the <many-to-one> tag to join Factors to their FactorTypes instead.

Do I get points for solving my own issue?

[b]Here is the corrected mapping:
<class name="amec.ee.sage.domain.gs.Guideline, Sage.Domain"
table="GS.GUIDELINES"
lazy="false">
<id name="GuidelineID"
column="GUIDELINE_ID"
type="System.Int32"
unsaved-value="0">
<generator class="identity" />
</id>
<property name="Author"
column="AUTHOR"
type="System.String"
not-null="true"
length="50"
unique="false" />
<property name="Date"
column="DATE"
type="System.DateTime"
not-null="true"
unique="false" />
<property name="IsRegulated"
column="IS_REGULATED"
type="System.Boolean"
not-null="true"
unique="false" />
<property name="Title"
column="TITLE"
type="System.String"
not-null="true"
length="200"
unique="false" />
<property name="RowGuid"
column="ROWGUID"
type="System.Guid"
not-null="true"
unique="true" />
<map name="factors"
inverse="false"
cascade="none"
sort="unsorted"
lazy="false"
access="field">
<key column="GUIDELINE_ID" />
<index column="ROWGUID"
type="System.Guid" />
<one-to-many class="amec.ee.sage.domain.gs.Factor, Sage.Domain" />
<!--<composite-element class="amec.ee.sage.domain.gs.Factor, Sage.Domain"/>-->
</map>
</class>

<class name="amec.ee.sage.domain.gs.Factor, Sage.Domain"
table="GS.FACTORS"
lazy="false" >
<id name="FactorID"
column="FACTOR_ID"
type="System.Int32"
unsaved-value="0"
access="property">
<generator class="identity"/>
</id>
<property name="TimeStamp"
column="VERSION"
type="System.Byte[]"
not-null="true"
unique="true"/>
<property name="Name"
column="NAME"
type="System.String"
length="100"
not-null="true"
unique="false" />
<property name="Description"
column="DESCRIPTION"
type="System.String"
length="1000"
not-null="false"
unique="false" />
<property name="Value"
column="VALUE"
type="System.String"
length="150"
not-null="true"
unique="false" />
<property name="RowGuid"
column="ROWGUID"
type="System.Guid"
not-null="false"
unique="true"
access="property"/>
<many-to-one name="Type"
column="TYPE_ID"
class="amec.ee.sage.domain.gs.FactorType, Sage.Domain" />
</class>

<class name="amec.ee.sage.domain.gs.FactorType, Sage.Domain"
table="GS.FACTOR_TYPES"
lazy="false">
<id name="TypeID"
column="TYPE_ID"
type="System.String"
length="5">
<generator class="assigned" />
</id>
<property name="Name"
column="NAME"
type="System.String"
length="35"
not-null="true"
unique="true" />
<property name="Description"
column="DESCRIPTION"
type="System.String"
length="250"
not-null="false"
unique="false" />
</class>
[b]


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.