-->
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.  [ 24 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Getting objects to refer to detached objects
PostPosted: Thu Feb 03, 2005 12:44 pm 
Newbie

Joined: Mon Dec 13, 2004 4:37 am
Posts: 8
Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 2.1.7



Hi

In our application on startup we load via hibernate a lot of read only objects which stay around in memory during the life time of the jvm. There are many other objects that are created as needed via HQL and have more standard lifespans. These objects reference the read only objects.

Our problem is when hibernate loads these more standard objects we need hibernate to make them reference to the already created read only objects. However by default hibernate will create new instances of these read only objects instead of reusig the ones we have already created.

Any ideas?

I thank you in advance


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 03, 2005 2:05 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
maybe there is another way to do this, but you could re attach (.lock(obj, LockMode.NONE), everytime you get persistant instance referencing your read only objects.

It might be easy to it automatically on the app level

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 03, 2005 2:05 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
i'm idiot, what about using second level cache?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 03, 2005 2:30 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
The 2nd level cache will always return a new instance though won't they?

What is the real issue with the new instances? The fact that they are new instances or that you end up going back to the DB to load up the data?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 03, 2005 2:51 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
The 2nd level cache will always return a new instance though won't they?



1- first time, read only instances (ROI) are loaded from an hibernate session and put in the second level cache
2- when detached, they still are in second level cache
3- querying persistant instance (PI) that refers to our ROI will only hit the db for what is needed, when fetching associated ROI, db won't be hit and there won't be new instances too

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 03, 2005 6:51 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
eh - gettings objects from 2nd level cache always create a new fresh instance ... it is build up from a "tuple" which is only read from the database once.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 03, 2005 8:15 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
I'm with max :D


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 04, 2005 3:18 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
sorry for the mistake ;)

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 06, 2005 4:06 am 
Newbie

Joined: Mon Dec 13, 2004 4:37 am
Posts: 8
Hi guys

Thanks for your replies.

The issue with re-attaching using the lock method is that there are a large amount of read only objects. And the application has no way of knowing which ones to re-attach. To re-attach them all on each session would be a performance issue.

So I am still left clueless as to how to do this.

Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 06, 2005 2:12 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
But do you actually need to lock them? You're not updating them (they're read only) so as long as you aren't cascading to them you don't need to lock them in your session.

The objects your're saving can still reference them via many-to-ones, etc. Am I missing something?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 07, 2005 4:50 am 
Newbie

Joined: Mon Dec 13, 2004 4:37 am
Posts: 8
I want the objects that I query out of the database to reference the read only objects and not new copies of the read only objects.

If I do not lock the read only objects to the session and I query via hibernate for some other object that is supposed to reference the read only object Hibernate will create a new instance or copy of the read only object.

The problem with this is that the read only object references a whole graph of other read only objects. So hibernate will start recreating the whole graph of read only objects


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 07, 2005 4:59 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I have no idea why your would want this. What is wrong with a new object out of the second-level cache.

Well, you can do it with a UserType, if you absolutely insist.

But requirements like this ring alarm bells that you are doing something else wrong.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 07, 2005 5:40 am 
Newbie

Joined: Mon Dec 13, 2004 4:37 am
Posts: 8
Gavin,

Thanks for your reply.

Our system is very similar to the system you describe in "Metadata driven applications"
http://blog.hibernate.org/cgi-bin/blosxom.cgi/2004/06/24#metadata-driven

The dynamic model is a dense graph. The Items reference the dynamic model. When Hibernate loads an Item we do not want Hibernate to rebuild the dense dynamic model in each session.

This is a Swing application that holds the dynamic model (an ontology) in memory across Hibernate sessions.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 07, 2005 5:48 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
So use custom types.

Have you actually tested the performance of rebuilding the model each time?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 07, 2005 12:18 pm 
Newbie

Joined: Thu Nov 04, 2004 10:24 am
Posts: 17
Quote:
So use custom types


In that case, UserTypes would be used for entities (Java classes that are mapped into the database under a <class> mapping tag). Is that possible? From what I've seen, UserTypes are used for value objects only.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 24 posts ]  Go to page 1, 2  Next

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.