-->
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: Use transient instances temporarily?
PostPosted: Wed May 14, 2008 1:49 pm 
Newbie

Joined: Wed May 14, 2008 1:21 pm
Posts: 4
I am creating a system with order-header and line-item tables. The line-item table has three many-to-one relationships with other tables (such as the Item table).

I expect a very large number of line-items (in the tens of thousands), so I need to optimize the performance of inserts. In order to do this, I have created a stored procedure which returns all the data I need to create the line-items. This data incudes all the IDs of the many-to-one relationships for each line-item.

I had hoped to simply be able to populate these IDs on the line item class, and save the line-item data. For example, I would like to do this:

LineItem.setItem(new Item(itemID);

If I try this, when I attempt to save the record I get:

org.hibernate.TransientObjectException: object references an unsaved transient instance

After some testing, I determined that I need a "real" Hibernate-managed Item instance to make this work. My problem is that I don't need the rest of the Item data. I already have the ID and this is all that needs to be populated in the LineItem table. I would rather not instantiate 10,000 or 20,000 Item instances if I don't need them. I also have two other many-to-one relationships, making this a real problem.

Is there any way around this, other than an HQL insert statement?

I also tried a StatelessSession.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 14, 2008 5:08 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
So, the item already exists in the database, but you are using the following code:

LineItem.setItem(new Item(itemID);

Why not:

LineItem.setItem(itemDAO.findById(itemID));

Would that solve the problem?

With LazyLoading, or even just swapping load and get methods on the finder implementation, you can avoid actually loading the associated objects.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 15, 2008 8:11 am 
Newbie

Joined: Wed May 14, 2008 1:21 pm
Posts: 4
Yes, you have understood exactly. Your code would solve the problem. The reason I am looking to avoid this is that for each line-item on my order, I have three associated objects that need to be set. On a 10,000 line order (not uncommon), this would mean that I would perform 30,000 "findById" calls. Even with LazyLoading, this is still a very large number of database lookups that I don't really need, since I already have a valid key for each of these associated objects.

Cameron McKenzie wrote:
So, the item already exists in the database, but you are using the following code:

LineItem.setItem(new Item(itemID);

Why not:

LineItem.setItem(itemDAO.findById(itemID));

Would that solve the problem?

With LazyLoading, or even just swapping load and get methods on the finder implementation, you can avoid actually loading the associated objects.


Top
 Profile  
 
 Post subject: Re: Use transient instances temporarily?
PostPosted: Thu Oct 01, 2009 3:11 pm 
Newbie

Joined: Wed Feb 07, 2007 8:26 pm
Posts: 17
Dears,

I am having the same problem. Did you find any solution.


Thanks
BISO


Top
 Profile  
 
 Post subject: Re: Use transient instances temporarily?
PostPosted: Thu Oct 01, 2009 4:37 pm 
Newbie

Joined: Wed May 14, 2008 1:21 pm
Posts: 4
I re-mapped the object replacing my many-to-one entries with property entries that represent the ID columns of the related objects. By doing so I could set the IDs of the related objects directly into the foreign key columns, without having the non-transient related objects in memory.

This solution is very bad from an ORM perspective, so I really wouldn't recommend it. One problem is that the object became an insert-only object, since it doesn't have the relationships that are needed for selecting all of the data. To get around this, you would have to map other objects to represent the same tables. This would probably lead to coding horrors, etc.

In retrospect, using a native SQL Query for the insert would probably be a much better idea.


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.