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.  [ 2 posts ] 
Author Message
 Post subject: NH 2.0.1 - foreign id generated from <any /> property
PostPosted: Thu Oct 16, 2008 1:15 pm 
Beginner
Beginner

Joined: Wed Jul 05, 2006 12:45 pm
Posts: 21
I ran into an issue converting a 1.2 project to 2.0.1. I have a mapping which uses the foreign id generator, referencing a property which is an <any /> type. This worked just fine in 1.2, and the id value of the <any /> property was correctly used.

Since upgrading to 2.0.1, this seems to be broken - an exception is thrown if I try to use the <any /> property for the foreign generator.

It seems that the ForeignGenerator class expects an EntityType (with an explicit cast which fails because it gets an AnyType). Is this no longer supported?

Thanks,

-Sasha Borodin

Hibernate version: 2.0.1

Mapping documents:
<class name="Scope"
table="Scope">

<!-- Entity properties -->
<id name="ID" type="string" unsaved-value="null" access="property" >
<column name="id" sql-type="guid" not-null="true" />
<generator class="foreign" >
<param name="property">Original</param>
</generator>
</id>
<property name="IsActive" column="isActive" not-null="true" access="nosetter.camelcase" />
<property name="ModificationDate" column="modificationDate" not-null="true" access="nosetter.camelcase" />
<many-to-one name="ModificationUser" column="modificationUserID" not-null="true" access="nosetter.camelcase" />

<!-- regular properties -->
<property name="Name" column="name" />
<property name="Label" column="label" />
<property name="Type" column="type" />

<any name="Original" meta-type="DFWHC.GroupOne.GoRes.business.scope.StringScopeType, DFWHC.GroupOne.GoRes" id-type="String">
<column name="scopeOriginalType"/>
<column name="scopeOriginalID"/>
</any>

</class>

Full stack trace of any exception that occurs:
System.InvalidCastException was caught
Message="Unable to cast object of type 'NHibernate.Type.AnyType' to type 'NHibernate.Type.EntityType'."
Source="NHibernate"
StackTrace:
at NHibernate.Id.ForeignGenerator.Generate(ISessionImplementor sessionImplementor, Object obj)
at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.FireSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.SaveOrUpdate(String entityName, Object obj)
at NHibernate.Engine.CascadingAction.SaveUpdateCascadingAction.Cascade(IEventSource session, Object child, String entityName, Object anything, Boolean isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeToOne(Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeAssociation(Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeProperty(Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeOn(IEntityPersister persister, Object parent, Object anything)
at NHibernate.Event.Default.AbstractSaveEventListener.CascadeBeforeSave(IEventSource source, IEntityPersister persister, Object entity, Object anything)
at NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(Object entity, EntityKey key, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.AbstractSaveEventListener.PerformSave(Object entity, Object id, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.Save(Object obj)
at DFWHC.GroupOne.GoRes.business.common.EntityFactory.Create[T](T entity) in D:\Development\GroupOne\GoRes\trunk\GoRes.root\GoRes\library\business\common\EntityFactory.cs:line 109
InnerException:


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2008 7:52 pm 
Beginner
Beginner

Joined: Wed Jul 05, 2006 12:45 pm
Posts: 21
I have worked around this issue by implementing a custom IIdentifierGenerator (<generator class="..." />). Though my implementation is specific to this business class, it could easily be expanded to accept configuration parameters, which would make it re-usable elsewhere.

Code:
    public class ScopeIdentifierGenerator : IIdentifierGenerator
    {
        #region IIdentifierGenerator Members

        public object Generate(ISessionImplementor session, object obj)
        {
            Scope scope = (Scope)obj;

            return scope.Original.ID;
        }

        #endregion
    }


I admin that the logic of generating an ID based on an "any" property is a pretty unique scenario. Thus, I was glad to find an point of expandability to plug in my own implementation.

Hope the follow-up helps someone down the road.

-Sasha Borodin


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