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: One-to-one & foreign key & entity inheritance problem
PostPosted: Wed Sep 09, 2009 6:15 am 
Newbie

Joined: Wed Sep 09, 2009 6:05 am
Posts: 2
Hello, I have a problem with a mapping.

My (simplified) domain model is as follows (C# code):

Code:
     public abstract class Entity<TEntity, TId>
         where TEntity : Entity<TEntity, TId>
     {
         public virtual TId Id { get; protected set; }
         public override bool Equals(object obj)...
         ...
     }
     
     public class EntityA<EntityA, long> : Entity<EntityA, long>
     {
         public virtual EntityB B { get; private set; }
         /* ... */
     }
     
     public class EntityB<EntityA, long> : Entity<EntityB, long>
     {
         /* ... */
     }


Here I have a one-to-one relationship between EntityA and EntityB, ie, each EntityA must contain exactly one EntityB. I cannot just "copy" the fields of EntityB into EntityA. They must be 2 separate entites, and in the database they must be 2 separate tables.

So, I did the following mapping (using the Fluent NHibernate interface):


Code:
     public class EntityAMap : ClassMap<EntityA>
     {
         public EntityAMap()
         {
             Id(x => x.Id);
             HasOne(x => x.B)
                 .Cascade.All();
             /* ... */
         }
     }
     
     public class EntityBMap : ClassMap<EntityB>
     {
         public EntityBMap()
         {
             Id(x => x.Id)
                 .GeneratedBy.Foreign("Id");
             /* ... */
         }
     }


Then I create a valid entityA, ready to be saved:

Code:
     var entityA = EntityAFactory.CreateNewValidEntityA();
     session.SaveOrUpdate(entityA);


The problem is that NHibernate throws an exception: "Unable to resolve property: Id".

On my log (and DB) I can see that NHibernate correctly "inserted" entityA into the database, and by debuging the code I can see that it assigned the correct Id to my entityA.

Furthermore, from the stack trace, I can see that this exception was thrown when NHibernate was trying to save the "cascaded" EntityB:

Code:
     at NHibernate.Tuple.Entity.EntityMetamodel.GetPropertyIndex(String propertyName)
     at NHibernate.Tuple.Entity.AbstractEntityTuplizer.GetPropertyValue(Object entity, String propertyPath)
     at NHibernate.Persister.Entity.AbstractEntityPersister.GetPropertyValue(Object obj, String propertyName, EntityMode entityMode)
     at NHibernate.Id.ForeignGenerator.Generate(ISessionImplementor sessionImplementor, Object obj)
     ...
     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.Impl.SessionImpl.SaveOrUpdate(Object obj)
     at myproject...


It looks like NHibernate is having trouble to determine the foreign Id.
How can I solve this problem?

Thanks!


EDIT

I think I was misunderstanding the "GeneratedBy.Foreign". I created a property on my EntityB pointing back to my EntityA, and said that EntityB's Id is generated by EntityB.A's Id (GeneratedBy.Foreign("A")). However I still have no success. The same exception now says that NHibernate cannot access the "A" property.


Top
 Profile  
 
 Post subject: Re: One-to-one & foreign key & entity inheritance problem
PostPosted: Tue Aug 31, 2010 10:51 am 
Newbie

Joined: Tue Aug 31, 2010 10:23 am
Posts: 1
I think you solved this, but for other users sake:

LINK: Marek Blotny's Blog - Fluent NHibernate and Inheritance Mapping

Code:
Id(x => x.Id, "CustomerID").GeneratedBy.Foreign("Customer");
Map(x => x.Demographics);
Map(x => x.ModifiedDate);
Map(x => x.Contact, "ContactID");
HasOne(x => x.Customer).Constrained();

note the most important x => x.Customer and GeneratedBy.Foreign("Customer")
(yes, there are the same Customer column, 1 lambda and 1 magic string, which is unfortunate, but necessary)


Top
 Profile  
 
 Post subject: Re: One-to-one & foreign key & entity inheritance problem
PostPosted: Mon Sep 06, 2010 11:47 pm 
Newbie

Joined: Mon Sep 06, 2010 11:38 pm
Posts: 1
bfreis wrote:

I think I was misunderstanding the "GeneratedBy.Foreign". I created a property on my EntityB pointing back to my EntityA, and said that EntityB's Id is generated by EntityB.A's Id (GeneratedBy.Foreign("A")). However I still have no success. The same exception now says that NHibernate cannot access the "A" property.


I also think so.

_________________
watch movies online


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.