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: Lightweight Objects For Associations
PostPosted: Tue Nov 20, 2007 10:59 am 
Beginner
Beginner

Joined: Fri Jan 12, 2007 1:08 am
Posts: 41
I have a screen where I can manage (add / edit /delete) Orders. Each order is related to a Customer. On the screen I also display a list of Orders. In the list I also display fields from other entities, including the Customer. I am using a DTO (flattened, screen specific view) to populate the list.

When I add a new Order I need to create a relationship with the Customer. Rather than obtain the associated Customer from the database (which seems wasteful) I am simply creating a new() Customer and setting its ID to the appropriate value (I know this from a drop down). The Order and the Customer relationship are persisted correctly. My problem is that for the new() Customer the data in the DTO is null (apart from the ID). This makes sense. A Customer with the appropriate ID and all null fields is in the first level cache.

I can display the correct data by evicting the new() Customer from the cache. My question however is what is the recommended way of obtaining entities that are going to be used to create relationships?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 6:45 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
Bad idea. NHibernate will think you are attempting to create a new, transient entity, and it will try to insert it (unless you evict it). This will cause a database constraint violation (duplicate primary key).

You will get into other trouble even if you try to prevent NHibernate from considering it transient by the "eviction trick". Just load the Customer by its ID and assign it to your Order. Don't worry about the "wasteful" select to create the entity Customer entity. If you are really that concerned about minimizing the number of selects, use the 2nd level cache.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 11:35 pm 
Beginner
Beginner

Joined: Fri Jan 12, 2007 1:08 am
Posts: 41
Thanks for the reply Nels.

Actually you are not correct regarding NHibernate trying to INSERT a new entity, at least with the mappings set up the way that I have them (and I use this technique in a lot of places).

I can live with the SELECT but when need I make associations to lots of different entities it does seem rather wasteful.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 30, 2007 4:52 pm 
Beginner
Beginner

Joined: Wed Jun 29, 2005 10:40 am
Posts: 30
Location: denver, co
One option would be to use ISession.Load, since it will return a proxy (if you have lazy loading enabled) that will not hit the database when your entity is not present in the session. If you access any properties of that object, it will then be loaded transparently.

Your process would then be as follows:
  1. Fetch the primary key from your select list.
  2. Call ISession.Load(typeof(Customer), primaryKey)
  3. Now, when/if you access properties of that customer proxy, it will be loaded from the database without any interaction from you.


Note that your nhibernate session must be left open for this lazy loading proxy behavior to work properly.


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.