-->
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: Component, Dependent or Composition Mapping - Help
PostPosted: Tue Feb 06, 2007 4:49 pm 
Newbie

Joined: Wed Jan 31, 2007 5:48 pm
Posts: 4
Location: Nashville
I have class PersonClass that embeds other classes Name and Address (composition). I save the name and address in separate tables from the parent, I am able to save the parent class and it saves the name and address (if any) but when I try to retrieve the parent, the name and address are null.

I believe I am totally misusing the Component and the One-to-One elements. I will appreciate any help or pointers to examples/documentation to read.

My schema looks like below:


table contact
(
[id] int not null identity,
[name_id] int,
[address_id] int
)

table name
(
[id] int not null identity,
[fname] varchar(25),
[lname] varchar(25)
)
table address
(
[id] int not null identity,
[street] varchar(25),
[zip] varchar(25)
)


<class name="Tests.PersonClass" table="contact">
<id name="id" type="Int32" >
<generator class="identity" />
</id>
<component name="person_name" class="Tests.NameClass ">
<property name="id" column="name_id" not-null="false" />
</component>
<component name="person_address" class="Tests.AddressClass ">
<property name="id" column="address_id" not-null="false"/>
</component>

<one-to-one name="person_name" class="Tests.NameClass" cascade="all" constrained="false" lazy="false"/>
<one-to-one name="person_address" class="Tests.Address" cascade="all" constrained="false" lazy="false"/>

</class>

<class name="Tests.NameClass" table="name">
<id name="id" column="name_id" type="Int32" >
<generator class="identity" />
</id>
<property name="fname" column="first_name"/>
<property name="lname" column="last_name"/>
</class>

Code:
public class PersonClass
    {
        private Nullable<int> _id;
        private NameClass _myName;
        private AddressClass _myAddress;

        public PersonClass() { }

        public virtual Nullable<int> id
        {
            get { return this._id; }
            set { this._id = value; }
        }
        public virtual NameClass person_name
        {
            get { return this._myName; }
            set { this._myName = value; }
        }
        public virtual AddressClass person_address
        {
            get { return this._myAddress; }
            set { this._myAddress = value; }
        }
       
        public virtual Nullable<int> name_id
        {
            get { return this._myName == null ? null : this._myName.id; }
            set { if (this._myName != null)this._myName.id = value; }
        }

        public virtual Nullable<int> address_id
        {
            get { return this._myAddress == null ? null : this._myAddress.id ; }
            set { if (this._myAddress!=null)this._myAddress.id = value; }
        }

    }
    public class NameClass
    {   
        private Nullable<int> _id;
        private string _fname;
        private string _lname;

       public NameClass(){}
        public virtual Nullable<int> id
        {
            get { return this._id; }
            set { this._id = value; }
        }
        public virtual string fname
        {
            get { return this._fname; }
            set { this._fname = value; }
        }
        public virtual string lname
        {
            get { return this._lname; }
            set { this._lname = value; }
        }

    }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 07, 2007 12:33 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
Do you really need to have the Name class in its own table? I ask because this seems to be overcomplicated. Think of a component as mapping data from within the same table as the parent class, but to different object representations. Personally I'd map your Name class this way in your parent mapping:
Code:
    <component name="person_name" class="Tests.NameClass">
      <property name="fname"/>
      <property name="lname"/>     
    </component>   



If you insist on maintaining the Name in a separate table and must have a one-to-one association, it might look like this:
Code:
<one-to-one name="person_name" class="Tests.NameClass" constrained="true" foreign-key="person_personname" />


It's either a component OR a one-to-one association.

Hope this helps
MIKE

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 16, 2007 11:18 am 
Newbie

Joined: Wed Jan 31, 2007 5:48 pm
Posts: 4
Location: Nashville
mike,
thanks a lot for your response. I still could not get it to work the way I wanted it though. Part of my problem is that the model is given to me and I can't change it.
But I worked around it by calling SaveOrUpdate() for the Name and Address objects in the Person before I call SaveOrUpdate on the Person object. Somehow I thought I could get NHibernate to do all that for me when I call SaveOrUpdate(personObj).


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 18, 2007 12:30 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
What does your "new" mapping looks like ? As Mike said, you need either a component or a one-to-one mapping, but not both. If you want Hibernate to save the objects in your collection automatically, you have to add a parent mapping to Name and Adress (Have a look at the reference documentation, chapter 17.1 and 17.2. There is a good example about parent/child relationships).

Regards,
Wolfgang


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.