-->
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.  [ 5 posts ] 
Author Message
 Post subject: NHibernate.Mapping.Attributes and derived properties
PostPosted: Tue Mar 13, 2007 4:45 pm 
Newbie

Joined: Sun Aug 20, 2006 8:09 pm
Posts: 8
Hi !
I use nhibernate 1.2 CR 1 with the one-table-per-concrete-class mapping paradigm. I want to use NHibernate.Mapping.Attributes to auto-generate mapping files. The problem is, that for inherited properties from base classes, no mappings will be generated. Only for the properties defined in the current class the mappings are created by NHibernate.Mapping.Attributes.

An example:

Code:
    [Class]
    public class PersistenceBase
    {
        private int persistenceId;

        [Id]
        public virtual int PersistenceId
        {
            get { return persistenceId; }
            set { persistenceId = value; }
        }
    }

    [Class]
    public class Child: PersistenceBase
    {
        private string name;

        [Property()]
        public virtual string Name
        {
            get { return name; }
            set { name = value; }
        }
    }


The output xml is:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="HiberTest.PersistenceBase, HiberTest">
    <id/>
  </class>
  <class name="HiberTest.Child, HiberTest">
    <property name="Name" />
!! ID would be needed here
  </class>
</hibernate-mapping>


Is there a way to enable NHibernate.Mapping.Attributes to also map the inherited fields to child-classes ?

Thanks for your help.
Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 13, 2007 8:33 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
i don't use Attributes, but wouldn't

Code:
[Class]
public class Child: PersistenceBase
{

be something like:

Code:
[Subclass]
public class Child: PersistenceBase
{

if I were writing the mapping files by hand, it would look something like:

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="HiberTest.PersistenceBase, HiberTest">
    <id/>
  </class>
  <subclass name="HiberTest.Child, HiberTest">
    <property name="Name" />
</hibernate-mapping>

(doing it from memory so might not be 100% correct...)

-d


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 13, 2007 8:46 pm 
Newbie

Joined: Sun Aug 20, 2006 8:09 pm
Posts: 8
This mapping with <subclass> can be used if you want to have one table for all of your inheritance tree. But I use <class> tags for every of my classes ( no matter where they are within the inheritance tree) and I have a seperate table for each concrete class.

Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 14, 2007 9:03 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
right, i meant <joined-subclass> sorry about that. i use a combination of table-per-class-hierarchy and table-per-subclass.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 18, 2007 8:36 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
NHMA does include mapping from base classes.
However, it stops when the base class is also mapped.

In your case, is PersistenceBase an entity ?
Do you load/save instances of this class ?

If you remove its attribute [Class], then you will have the Id in the mapping of the Child class.

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


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