-->
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: Related Classes (noob)
PostPosted: Wed May 02, 2007 9:45 am 
Newbie

Joined: Wed May 02, 2007 9:32 am
Posts: 17
Hi -

I have a 3 classes (Component, Address, Payment. Address and Payment are also tables in the the database and I have created the mapping files for them.

My Component class contains two properties: AddressInformation (Address) and PaymentInformation (Payment) and does not have a related table in the database. My question is how do I set up the mapping file for Component so that I can call Session.Save(Component) and have the adress and payment information saved?

Thanks
Joe


Top
 Profile  
 
 Post subject: Re: Related Classes (noob)
PostPosted: Wed May 02, 2007 11:29 am 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Need more info to assist.

What's the relationship between the Address and Payment, then (?-to-?)? How are they related to each other in the db? If MyComponent carries no persistent data, what purpose does it serve?


Top
 Profile  
 
 Post subject: What I was attempting
PostPosted: Wed May 02, 2007 12:45 pm 
Newbie

Joined: Wed May 02, 2007 9:32 am
Posts: 17
Was to see how/if child classes were saved. After some digging I modified my Comonent and was able to get a little further along

Database Table:

Component
Id
AddressId
PaymentId

Class Definition:

Component
Id
AddressInformation
PaymentInformation


Mapping File:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Orders.Composite, Orders" table="tmpCompositeDebug" lazy="false">
<id name="Id" column="Id" unsaved-value="0">
<generator class="native"/>
</id>
<many-to-one name="AddressInformation" class="Orders.Address, Orders" column="AddressId" cascade="all" />
</class>
</hibernate-mapping>



UsageCode snippet from a test stub:


Code:
            Composite loaded = _CurrentSession.Load<Component>(1002);
            Assert.IsNotNull(loaded);   //at this point everything is loaded ok

            string address1 = DateTime.Now.Ticks.ToString();
            Address newAddress = new Address();
            newAddress.AddressLine1 = "unit testAddresses";
            newAddress.City = address1;
            newAddress.State = "ZZ";
            newAddress.PostCode = "99999";

            loaded.AddressInformation = newAddress;
            Assert.AreEqual(loaded.AddressInformation.City, address1);

           
            _CurrentSession.Update(loaded);




When I call the update method I would like for my Address table to be updated. Is there a way to do this, or do I need to call Save/Update for all child classes?

Thanks
Joe


Top
 Profile  
 
 Post subject: Re: What I was attempting
PostPosted: Wed May 02, 2007 5:26 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
joejoejoe wrote:
Mapping File:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Orders.Composite, Orders" table="tmpCompositeDebug" lazy="false">
<id name="Id" column="Id" unsaved-value="0">
<generator class="native"/>
</id>
<many-to-one name="AddressInformation" class="Orders.Address, Orders" column="AddressId" cascade="all" />
</class>
</hibernate-mapping>

...
When I call the update method I would like for my Address table to be updated. Is there a way to do this, or do I need to call Save/Update for all child classes?

Thanks
Joe


I thought the cascade="all" would have taken care of that.


Top
 Profile  
 
 Post subject: Got it
PostPosted: Thu May 03, 2007 9:16 am 
Newbie

Joined: Wed May 02, 2007 9:32 am
Posts: 17
Needed to call flush.

Quote:

Component loaded = _CurrentSession.Load<Component>(1002);

string address1 = DateTime.Now.Ticks.ToString();
Address newAddress = new Address();
newAddress.AddressLine1 = "unit testAddresses";
newAddress.City = address1;
newAddress.State = "ZZ";
newAddress.PostCode = "99999";

loaded.AddressInformation = newAddress;

_CurrentSession.SaveOrUpdate(loaded);
_CurrentSession.Flush();


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.