-->
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: The unavoidable total loading of object structures.
PostPosted: Thu Jun 29, 2006 11:43 am 
Newbie

Joined: Thu Jun 29, 2006 10:58 am
Posts: 4
I'll admit to feeling pretty stupid asking this, but I hope the problem is with me and not hibernate.

What I'm trying to achieve is simple. When I call:

Object obj=session.get(MyClass,id);

...I want ONLY the entity represented by "id", and none of its related entities. I want to load those manually. The reason: A full cascade on .get() will result in many, many, many thousands of entities being pulled needlessly from the database.

Okay--so I'm stupid, right? Doing what I describe is going to be needed in exactly 100 percent of large systems using hibernate, so it MUST BE POSSIBLE! And there MUST BE DOCUMENTATION DESCRIBING HOW TO DO IT. Yet, I've come up empty.

Look:

@OneToOne(cascade = {CascadeType.PERSIST}, fetch= FetchType.EAGER)
@JoinColumn(name="columnName", nullable =true)
public SubEntity getSubEntity
{
return subEntity;
}

To me, the CascadeType.PERSIST indicates that this class should persist both "this" object and "subEntity", but not load them on session.get(class,id). But, that's not the case.

Okay, okay. So turn fetch to FetchType.LAZY, right? Wrong--that's totally unstable, as I want to guarantee that the entities are never fetched. With lazy fetching, a careless toString() method can actually act as a DAO!!!

So, what's the "trick" here? Anybody?

---

Side question. These two imports seem to go together...

import javax.persistence.OneToOne;
import javax.persistence.CascadeType;

...while import org.hibernate.annotations.CascadeType; doesn't seem to have a partner. Am I missing the Jar containing

org.hibernate.annotations.OneToOne


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 2:00 pm 
Newbie

Joined: Thu Jun 29, 2006 10:58 am
Posts: 4
I mean, I've looked into this. Here's where it all goes down in the hibernate source:


...This block gets the types and calls "resolve" on each one, filling in hydratedState.


Type[] types = persister.getPropertyTypes();
for ( int i = 0; i < hydratedState.length; i++ ) {
final Object value = hydratedState[i];
if ( value!=LazyPropertyInitializer.UNFETCHED_PROPERTY && value!=BackrefPropertyAccessor.UNKNOWN ) {
hydratedState[i] = types[i].resolve( value, session, entity );
}
}


...Then this guy calls resolveIdentifier, which fills in the subEntities.



public Object resolve(Object value, SessionImplementor session, Object owner)
throws HibernateException {

if ( isNotEmbedded(session) ) {
return value;
}

if (value==null) {
return null;
}
else {

if ( isNull(owner, session) ) return null; //EARLY EXIT!

if ( isReferenceToPrimaryKey() ) {
return resolveIdentifier( (Serializable) value, session );
}
else {
return loadByUniqueKey(
getAssociatedEntityName(),
uniqueKeyPropertyName,
value,
session
);
}
}
}


...The problem is clearly here:

if ( value!=LazyPropertyInitializer.UNFETCHED_PROPERTY && value!=BackrefPropertyAccessor.UNKNOWN )

...The value (even when I have fetch set to lazy, is never UNFETCHED_PROPERTY) and so it always resolves to the subentity, thus bringing in many 1000s of rows. I seriously feels like just hacking this code thus:

if (false)

...so that never gets called.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 2:01 pm 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
whats in tostring()

are you using something like reflectionToStringBuilder

thats a no no

also you might want to look into lazy mode = "extra"

_________________
Don't forget to rate


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.