-->
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: Components and lookup tables
PostPosted: Mon May 07, 2007 3:41 pm 
Newbie

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

I was wondering if someone could tell me the best practice / reccomendation for the following scenario


Domain
Quote:
Order
Id (int)
CreateDate (DateTime)
CategoryInformation (Category)

Category
Id (int)
Description (string)


Database
Quote:

Order
Id
CreateDate
CategoryId

Category
Id
Description



How would I go about setting up the mapping file so that I can change the the Order.CategoryId without causing(or allowing) an update to the Category table? Or shuld I just be concerned with tracking the CategoryId in my Order object, and looking up the CategoryInformation as I need it?

Thanks
Joe


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 08, 2007 11:41 am 
Newbie

Joined: Mon Jul 18, 2005 7:45 am
Posts: 16
I think you just need to use the following:

<many-to-one name="CategoryInformation" column="CategoryID" cascade="none" />

the cascade indicates that any changes to Category will not be persisted when Order is saved.

You might also want think about replacing the Category class with a simple enum. We have been looking at doing this for simple lookup values like this. The Description text can be assigned to the enum using a custom attribute. Attributes can also be used to mark enum values to be exported into a database table for reporting purposes.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 08, 2007 12:23 pm 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
sthorogood wrote:
I think you just need to use the following:

<many-to-one name="CategoryInformation" column="CategoryID" cascade="none" />

the cascade indicates that any changes to Category will not be persisted when Order is saved.



Actually, that's not what cascade="none" does. I think there are actually a lot of misunderstandings about how saving/updating works within NHibernate. If you load the Category and Order from the database in an NHibernate session any changes made to either will be persisted through to the appropriate table based on changes with the business objects. There is no need to call Save(), simply Flush() your changes. So assuming he isn't always using transient objects and then calling Save() or Update() or SaveOrUpdate() your described cascade won't really change the behavior.

That being said I have no clue why this question is even being asked. Why are you thinking the category table will be changed? If you change your order to reference a different category the appropriate order table will be updated with the categoryId of the new reference. Isn't that exactly what you want? If you didn't change the category it won't change the category table.

What issue are you currently seeing?


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.