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
|