Hi All !
Hibernate uses generating proxy in some cases. And proxy is initialized if invoked method is other than id (equals and hash are ovveride often)
http://www.hibernate.org/hib_docs/v3/reference/en-US/html/performance.html#performance-fetching-proxies
Quote:
Certain operations do not require proxy initialization
* equals(), if the persistent class does not override equals()
* hashCode(), if the persistent class does not override hashCode()
* The identifier getter method
But sometimes designer of model know that invoking specific method doesn't have to initialize proxy, eg instance of or generic stringTo.
http://www.hibernate.org/280.html
Code:
public boolean instanceOf(Class<?> otherClass) { return otherClass.isAssignableFrom(getPoliticianClass()); }
public Class<?> getPoliticianClass() { return Politician.class; }
public Class<?> getPoliticianClass() { return Democrat.class; }
Politician politician = ...; // code to find your (s)elected politician
if (politician.instanceOf(Democrat.class) {
// politician is a democrat
Democrat democrat = (Democrat) session.get(Democrat.class,
politician.getId())
// Use your democrat.
}
if using "x instance of Democrat" it doesnt initialize proxy but x.instanceOf(Democrat.class) initialize proxy which isn't required. It would be great to mark method some annotation like @NotInitializeProxy.
Other case which I meet is generic stringTo
Code:
interface IDescription {
String description();
}
@Entity
class Entity implments IDescription {
getId(); // not initialize proxy
String description() { return getClass.getName() + ":" + getId() } // unfortunately initialize proxy
}
genericStringTo {
[...] if (false == Hibernate.isInitizlied(field)) { respond.name("fielndName").value(field.description()) }
[...]
}
Also, method "description" would be marked as @NotInitializeProxy.
Is hibernate support that cases or maybe could be support in future this ?