-->
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: really no final method allowed in entity?
PostPosted: Fri Jan 07, 2011 7:43 am 
Newbie

Joined: Fri Jan 07, 2011 7:23 am
Posts: 19
According to Hibernate's (3.6.0) spec section 21.1.3 ("Single-ended association proxies"), such a proxy can't be constructed
by Hibernate if it contains "any final methods".
My question is, does this restriction apply to getters/setters of persistent fields only or really to any method in an entity class?
So, does a method like this one:
Code:
public final String toString() {
   return this.getClass().getSimpleName() + id;
}

really prevent the creation of a (CGLIB or Javassist) proxy for this entity?
Does it matter if field-based or property access is used? Since CGLIB was replaced by Javassist, does this provide any more features in this direction?

I like to use inheritance in my entity hierarchy and hence the requirement to define some final methods,
for example, in the base class to prevent subclasses from overriding those methods.

thanks in advance!

Robin.


Top
 Profile  
 
 Post subject: Re: really no final method allowed in entity?
PostPosted: Sat Feb 05, 2011 1:12 pm 
Newbie

Joined: Fri Jan 07, 2011 7:23 am
Posts: 19
No one?


Top
 Profile  
 
 Post subject: Re: really no final method allowed in entity?
PostPosted: Fri Jul 08, 2011 9:20 am 
Newbie

Joined: Fri Jan 07, 2011 7:23 am
Posts: 19
By the help of the Hibernate mailing list (thanks Emmanuel Bernardt!) I'm able to answer my own question, the summary is:
Final methods do not prevent Hibernate from creating a proxy in general but unless those methods don't use any state of the entity this is highly inadvisable.

Some background information: Hibernate doesn't use bytecode enhancement neither with cglib nor with Javassist so, in order for a proxy to initialize its target entity lazily it has to intercept any method which may use the state of that target entity. Now it's perfectly ok to have a final method like this
Code:
public final doSomething(String a, Integer b ) {
  // do complicated stuff using only a and b (no instance members accessed!)
}

but as soon as this method uses any persistent field either directly or through another instance method this would bypass the proxy and thus lead to unexpected behaviour.

As a sidenote this is the same reason why you should not access fields of other instances directly, for example in an entities equals method:
Code:
// XXX bad code!
public boolean equals(Object o) {
  if (this == o) return true;
  if (!(o instanceof Profile)) return false;
  Profile profile = (Profile) o;
  // XXX this bypasses a possible proxy, use profile.getName() instead!
  return (name == null ? profile.name == null : name.equals(profile.name));
}


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.