-->
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: Is Saving CategorizedItem Efficient?
PostPosted: Sun Sep 09, 2007 10:55 am 
Beginner
Beginner

Joined: Wed Jul 25, 2007 6:32 am
Posts: 22
I have read the book "Java Persistence with Hibernate" and studied the CaveatEmptor Native code.

But I have a question.

In order to save a CategorizedItem I need to do this:

CategorizedItem cItem = new CategorizedItem("Scott", Category, Item);

CategorizedItemDao.makePersistent(cItem)



But in order to delete a CategorizedItem I need to do this:
Category.getCategorizedItems().remove(cItem)

BTW, do I need to save the category then again, in order to delete the categorized item?

My Question is:
Is this efficient? Because when I have lots of items in my category, then all items are read from the database when I do: Category.getCategorizedItems()

Why can't I just do: CategorizedItemDao.makeTransient(cItem);
I think this is much simpler.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 09, 2007 12:15 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Of course you can do that but then the Category has outdated collection references in-memory, which will lead to other problems down the road. It can also conflict with any cascading options (e.g. if on category.categorizedItems() is cascading persistence enabled Hibernate wouldn't know if it should now save it or remove it).

If you have a reference to an entity in a collection, and that collection belongs to an instance which is in-memory, you need to update the collection by removing the reference when the entity is removed.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Is Category in memory?
PostPosted: Mon Sep 10, 2007 2:55 am 
Beginner
Beginner

Joined: Wed Jul 25, 2007 6:32 am
Posts: 22
Yes I understand what you mean.

But when I add a categorized item and I save it like this:

CategorizedItemDao.makePersistent(cItem), and I refresh the page again then the category is loaded again and has the new CategorizedItem in it.
Or am I wrong?

I don't fully understand when Category is in memory. In my opinion Category is only in memory in the same unit of work. After the unit of work is closed, Category is not in memory again after the page has been rendered.

Or is the Category still in memory after the webpage has been rendered?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 10, 2007 7:00 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I have no idea what is in your memory because I don't know what you are doing.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


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.