• Home
  • RSS Feed
  • Log in

Advanced Hibernate: Proxy Pitfalls
Posted by Maarten Winkels in the late evening: March 8th, 2008

Most programmers come into contact with Hibernate proxies when they face the ominous LazyInitializationException. The bad taste is hard to wash away, but Proxies are a necessary “evil” when working with Hibernate. This article will dig into why proxies are important. It will also point out some pitfalls that occur often when working with proxies.

What are proxies?

Proxies are the mechanism that allows Hibernate to break up the interconnected cloud of objects in the database into smaller chunks, that can easily fit in memory.

Let’s look at an example.

PERSON
ID NAME BOSS
1 Daan NULL
2 Vincent 1
3 Eelco 1
4 Jan 2
5 Maarten 3
6 Erik 3
public class Person {
  private Long id;
  private String name;
  private Person boss;
  private Set<Person> employees;
}

This is a very simple example. The class on the right is mapped to the table on the left. So what happens if Hibernate loads the row with NAME=’Maarten’? To store the data in memory, Hibernate will create a new Person object and set the columns to the fields. It stores the ID column in the id field and the NAME column in the name field.

Coming to the BOSS column, Hibernate faces a problem: The type of the column is NUMERIC, but the type of the property is Person. This is of course a foreign key, to another row in the PERSON table. Now, Hibernate could simply load the data from the associated row and create a new Person object and store it in the field, but this would cascade eventually to all rows in the table (and possibly other tables in the database), especially taking into account the employees property as well.

image001.gif

Instead of loading the associated row, Hibernate will create a Person object and set the id property to the value found in the BOSS column. This Person object is a specialized Person object, that will load the associated data if necessary. Initially the fields of this new Person object are not set, since the data is not yet loaded. When a method is invoked on the object, Hibernate will fetch the data from the column and populate the object. This is the proxy mechanism.

To add this new behavior (the loading of the data when a method is invoked), Hibernate will create a dynamic subclass of Person using CGLib and add the desired functionality. We need a subclass of Person here, to comply with the type of the boss field.

image002.jpg

So what does this look like? Looking at a proxy in a debugger is quite interesting. To the left you find an image of the eclipse debugger displaying a Person object loaded from the database, with the boss property set to a Hibernate proxy. A Hibernate proxy can be easily identified by the class name, which will contain the "$EnhancerByCGLIB$" marker. This is due to the fact that the class is generated at runtime by CGLib.

The proxy class is a subclass of the Person class, so it contains the same fields (as can be seen in the debugger), but it also contains some added fields. The most important is CGLIB$CALLBACK_0, which is set to an instance of CGLIBLazyInitializer, which is a Hibernate class. This is the object that enables the proxy to load the data when needed. It contains a "target" field, that will contain the Hibernate loaded object when the data is loaded.

In the debugger, we can do funny things with this proxy. By clicking the proxy itself, the debugger will invoke the toString() method on the object, effectively loading the data itself, and setting the target field in the LazyInitializer to a Person object with the data loaded. This can change the behavior of applications and unittests severely! The same holds by the way for Hibernate collections that are not yet loaded; clicking it in a debugger will load the collection.

Proxy Pitfall 1: Field Access

Coming to the first pitfall with proxies: When a proxy is loaded, the data is not stored in thr proxy object, but in the "target" object. The fields in the proxy object will remain null forever (or any value that they have been initialized with), since all methods invoked on the proxy will be delegated to the target object.

The main area where this Pitfall arises is the equals(..) method. A very common implementation would be something like this:

  @Override
  public boolean equals(Object obj) {
    if (obj == null) {
      return false;
    }
    if (obj == this) {
      return true;
    }
    if (!(obj instanceof Person)) {
      return false;
    }
    return name.equals((Person)obj).name);
  }
 

checking the name field of the two Person objects to be the same. This will fail however when we invoke it like personObject.equals(personProxy). In the last line of the method we use direct field access on the other object to look at the name field. Since the other object is a proxy, the field will always be null, even if the proxy is in fact encapsulating the personObject itself. Although the equals(..) method is a typical problem area for this pitfall, it is definitely not limited to that. Every method that takes the declaring class as parameter (and thus allows access to fields on that parameter) has the same problem.

To prevent this, use getters and setters in these occasions. The proxy will be loaded when the getter is invoked and the data will be accessible.

Proxy Pitfall 2: instanceOf

image003.gif

A proxy will be a subclass of the field type that is required. Hibernate will look at the type of the field and create a dynamic class with that type, adding the necessary functionality. This means that if the field type is not the actual implementation type, but an interface or superclass, the type of the proxy will be different than the type of the actual object: If the field-type is the superclass of the actual implementation, the proxy-type and the implementation-type will be siblings in the type hierarchy, both extending the superclass.

The proxy object will thus not be an instance of the implementing type, application code depending on this check will fail.

To prevent this pitfall, switch of proxying for the hierarchy, by setting lazy="false" on the top level class. This will prevent Hibernate from using the proxy mechanism and always load objects of this type or subtypes.

Conclusion

Proxying is one of the most important features of Hibernate. Although it takes some time to see its importance and to work with its features, doing so will help you develop applications with Hibernate more easily. Keeping in mind the two pitfalls that are discussed in this blog will prevent hard to find bugs in your applications.

  • Share/Bookmark

Filed under Hibernate | 14 Comments »



14 Responses to “Advanced Hibernate: Proxy Pitfalls”



    Martin Says:
    Posted at: March 9, 2008 at 2:08 pm

    Nice article. Thank you.

    I was aware of the first pitfall you mentioned (I actually ran into that problem once and it took me half a day to find out what’s going on), but I’ve never wasted a thought on the instanceof operator pitfall in case of the reference type being a supertype of the actual implementation. That’s really useful information. Thanks again.

    But regarding your solution for preventing this pitfall: Settings lazy=”false” on the top level class will disable the proxy mechansism for each and every to-one relationship to this object. I think that should be kept in mind…

    Martin



    Peter Says:
    Posted at: March 9, 2008 at 10:01 pm

    Another problem caused by proxies is that you are forced to write an equal/hashcode for entities!

    If proxies were not possible, the default equals/hashcode of Object will do because you never want to have multiple instances of the same entity in a single session. So why be bother with writing an equals/hash (usually based on arguments that can change, so the equals/hashcode is broken).



    fletch Says:
    Posted at: March 10, 2008 at 1:38 am

    Another pitfall is when you want to detach the objects. In a web application it means having to use something like the OpenSessionInViewFilter which has its own problems, using Hibernate.initialise() all over the place (which gets messy if your domain objects are all connected to each other) or, finally, copying your domain objects to DTO’s that are a view on the domain objects. *sigh*. I can see why the hibernate team uses proxies but it is painful.



    Maarten Winkels Says:
    Posted at: March 10, 2008 at 9:57 am

    @Martin: Thanks for your response! Setting lazy=”false” will indeed load extra data for every fetch on a referring object, which could be a performance problem. Good catch!

    @Peter: Actually, you don’t need to write equals/hashcode for proxies for that reason: Hibernate will enforce that every entity is represented by a single instance in a session, also for proxies. It will lookup a proxy instance if needed in an internal map, that is bound to the session.

    @Fletch: This is not really a pitfall, but an artifact of why proxies are needed: Hibernate cannot load all data in one go. You have to configure Hibernate to stop loading data at the correct junctions in your domain model. Using the Hibernate configuration, you can already configure classes or associations to be eagerly loaded (either by join or by separate select), which means you don’t have to use Hibernate.initialize(..). A new feature (that I heard Emmanual Bernard talk about during Grails Exchange) is fetch groups. This would make it possible to configure explicitly which associations should be loaded in which situation.



    Medhat Saleh Says:
    Posted at: March 11, 2008 at 3:05 am

    Thanks a lot for these two. I was bitten before by the second one and could not figure out how to solve cleanly as you are suggesting, so I ended up with a very nasty hack in the domain code.



    Cédric Thiébault Says:
    Posted at: March 26, 2008 at 6:15 pm

    To know the class of the proxy object, you can use the method
    Class getClassWithoutInitializingProxy(Object object)
    form
    org.hibernate.proxy.HibernateProxyHelper
    /**
    * Get the class of an instance or the underlying class
    * of a proxy (without initializing the proxy!). It is
    * almost always better to use the entity name!
    */



    koensen Says:
    Posted at: April 16, 2008 at 5:57 pm

    Actually there are some architectural problems resulting from pitfall 2.
    There are some cases where you don’t want to expose a (visible) setter which could initialize the object…
    Read here:
    http://www.bb242.de/2008/04/02/how-to-live-with-hibernate-proxies/

    A pretty neat workaround some things is to work with get() instead of load().



    Christian Says:
    Posted at: June 9, 2008 at 11:46 am

    my 2 cents:
    pitfall #2 is just a design/implementation bug, where Hibernate has never admitted, that it is a bug Hey, its the world best ORM and everything else is crap

    The JPA spec has addressed this: 2.1.7. …Relationships are polymorphic.
    Nothing more to say, except that it sometimes does still occur in Hibernates JPA implementation (but now its a bug, not a feature)

    Regarding Proxies in general: The Hibernate approach is the we-can-implement-it-on-a-weekend approach. Whereas JDO has featured a smarter approach (available in many JPA implementations), i.e. use byte code enhancement to implement the proxy functionality in the class itself, which solves most of the problems. (But you can’t design and implement that on a weekend.)

    Christian



    Christian Says:
    Posted at: June 9, 2008 at 11:47 am

    my < and > got lost, it should read:
    <sarcasm>Hey, its the world best ORM and everything else is crap</sarcasm>



    Frits Jalvingh Says:
    Posted at: September 25, 2008 at 2:27 pm

    @Christian:
    Bytecode-enhancing the actual class would be great but in runtime is hard to do because they need to be enhanced before they are ever used which is complex.

    A simpler alternative is to create a proxy for the actual object itself and *not* have the “target” kludge. Simply use the fields in the proxy directly. This still does not properly solve the inheritance scheme but that is impossible with lazy loading anyway.

    The simpler alternative has way less problems than the current implementation and *can* be built in an afternoon (my replacement for Hibernate called SPF has it, and it took that long ;-)

    Sadly, Hibernate sucks bigtime and is not a good persistence framework at all; it is just the one that sucks the least…



    Pravin Says:
    Posted at: December 4, 2008 at 11:51 am

    Hi I am having problem with same with proxy i want to check whether it is the instance of a class out of two classes as the object is of Base class Like: C is base class and A,B are derived so i want to check whether new C() instanceof A or B.

    Please help me

    Pravin



    JPA implementation patterns: Lazy loading | Xebia Blog Says:
    Posted at: April 27, 2009 at 8:30 pm

    [...] when invoking the identifier getter method. My colleague Maarten Winkels has blogged about the pitfalls of these Hibernate proxies last [...]



    donamir Says:
    Posted at: October 21, 2009 at 11:12 am

    Useful article, tanks.



    Laziness (in proxies) is a virtue | Redfin Developers’ Blog Says:
    Posted at: December 29, 2009 at 6:51 pm

    [...] the lazy proxy is NOT the same as the actual object. If you depend on the datatype of the object, you may have problems, since the type of the proxy isn’t the same as the type of the actual object (e.g. a proxy [...]



Leave a Reply

Click here to cancel reply.

Deployment automation for Java application running on Websphere, WebLogic and JBoss

Archives

  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009

Xebia Sites

  • Xebia Corporate
  • Xebia France
  • Xebia India

Categories

  • Java (279)
  • Agile (109)
  • General (50)
  • Testing (42)
  • Performance (42)
  • Hibernate (36)
  • Scrum (33)
  • Podcast (31)
  • Architecture (31)
  • Spring (28)
  • SOA (24)
  • Maven (22)
  • Project Management (22)
  • Flex (17)
  • JPA (17)
    • JPA implementation patterns (13)
  • Eclipse (15)
  • Quality Assurance (14)
  • Middleware (19)
  • Frameworks (13)

Tag Cloud

    Functional Programming Introduction to Agile Groovy IntelliJ qcon Scala XML Spring Architecture fitnesse Ajax Grails Xebia esb Poppendieck Semantic Web Hibernate Seam product owner Maven Testing Agile Performance Lean JavaOne SOA Agile Awareness Workshop Java Scrum Closures
medicin depression buy phentermine without a perscription aricept generic hair loss help how do you prevent bone loss urinary tract infection symptoms viagra sex domination cialis viagra cures for throat infection buy sumycin acne care new medication for cancer treatment help for sleeping problems on-line pharmacies cure snoring medications to help clot blood what is aspirin buy zestoretic bronchitis vs pneumonia back pain muscle acne face medication muscle women pain behind knee fat blocker man health arthritis natural cure woman health women insomnia cheap phentermine online cats and irritable bowel syndrome buy cialis generic online nutritional diet for osteoporosis abnormal blood clots treatments for hair loss what is zyprexa dental whitening products impotence herbs drugs for diabetes allergy prevention buy canada levitra Mentax adhd in children hair loss in woman medicines for blood clot online imitrex viagra buy free dog products clindamycin drug how to stop hair loss chloramphenicol discount drug viagra what valium does permanent hair loss heart failure medicine avapro 150mg ordering viagra online food allergies order viagra online online viagra prescription carisoprodol mg improve your skin discount erectile dysfunction medication buy xanax online buy order viagra scabies teatments information allegra vitamine b1 diazepam breast cancer support free stop smoking cipro side effects ultram cheapest treatment attention deficit disorder discount vitamins supplements how to get viagra online synthroid buy cheapest cialis zyrtec online how to clear acne preventive osteoporosis immune stimulants what is hoodia On Line Viagra getting over the pain diflucan dosage health asthma online stores hair loss products blood clot drugs colon parasites hair loss products discount medicine pravastatin buy griseofulvin tablets order indomethacin dog health products how to take a beta-blocker diazapan is valium treating cold sores chronic pain drug what is osteoporosis stress drug tooth whitening lowering cholesterol naturally legality of buying cialis online order levitra treatment for insomnia cheapest cialis index depakote overdose alprazolam condom sales treatment of yeast infection xanax sales taking viagra after cialis how to control pain new birth control chest pain health prozac prescription blood clots viagra in mexico chlamydia pill cancer drugs cold flu drugs how do i order viagra online super viagra acyclovir medicine benadryl dosage erythromycin pregnancy buy contoured condom chronic muscle pain pet health dogs treatment attention deficit disorder dental teeth whitening asthma medicine free prescription drugs herpes drug diabetes treatment buy tooth whitening gel cheap fast valium generic levitra buy cheapest viagra online lopressor drug pharmacy drug prices ultram dosing treatments for bipolar disorder neurontin withdrawal parasite medication chlamydia tips for increasing breast size ways to enhance breast what is valium used for metformin tablet order birth control hair loss for men how does xanax work treatment hepatitis c rythmol cheap acai antioxidants nexium generic blood pressure pills levitra online no prescription Levitra Online medications on line motion sickness drugs bactrim online order roxithromycin nicotine where can i order viagra immune supplements buy erexin v bph prostate allopurinol xanax for depression drug new smoking stop cheap impotence drug generic cialis delivery new treatment for depression antibiotics for cat viagra china alternative medicine cholesterol viagra dose anxiety disorder treatment severe muscle pain treatment of cancer calcium carbonate penis enlargement without pill valium maximum dosage reasons for high blood pressure energy product breast enlargement info cheap effexor building your body wrinkle cream aricept dosage alpha blocker increasing female sex drive valium depression new pain meds no rx xanax drug trileptal mg imitrex avapro 150mg medicine drugs contraception female claritin pill medication for acne med orders buy viagra internet levitra effect treatment for blood clots order sominex buy creatine buy precose cheap viagra overnight lopressor drug body building info health drugs general health and medical what is diazepam eye infections in dogs online prescription pills diclofenac tablet new medication anxiety buy citalopram medication male enhancement enhancement fat blocker medicine for throat infection order cardizem about soma health remedies for dogs generic xanax cheap zyrtec for depression medicine viagra sex domination buy acne skin care product hypnosis help study cure vaginal yeast infection weight loss supplement program muscle pain in leg how to increase erection buy viagra what is cla augmentin doses gaining muscle mass health med online heart rate treatments lopressor drug dog ear canal phentermine without prescription viagra order online weight loss glipizide diabetes astelin generic fat blocker buy gel tooth whitening cheap wellbutrin online weight loss program buy antiox anti-biotics acne skin treatmen tramadole vpxl pill drugs affecting levitra immune system support augmentin hypothyroidism medication buy erexin v uy prescription medication without a prescription buy discount order osteo arthritis online buy pilocarpine cheapest place to buy phentermine parasite treatment impotence help body fat loss viagra herb alternative constipation supplements treatment dementia adhd and medications muscle spasm relief viagra online cheap relieve upper back pain stop hair loss discount viagra online menstrual cycle problems antifungal shampoo side effects ativan gabapentin medication where can i buy viagra diazepam buy soma online clonidine dosage viagra gel top hair loss fast antibiotics cure chlamydia skin fungal infections drug zofran give up smoking alternative medicine cholesterol sleeping help best online viagra scams prednisone 10mg viagra sex domination lotensin easy weight loss pain meds without prescription over the counter drugs new high blood pressure medic generic compazine cetirizine drug order phentermine best fat blockers woman enhancement supplement drug zofran buy precose new drug treatment for cancer how to increase fertility viagra in australia benadryl dosing buy alcoholism medications order l arginine buy diazepam generic for ativan ativan prescription drugs weight loss treatment for chest pain woman health where can i buy phentermine online skin fungal infection give up smoking viagra on line hoodia information how does osteoporosis occur buy viagra online buy alcoholism medications depakote overdose klonopin pill tetracycline capsules what is high blood pressure bladder control for dogs generic for lipitor glucophage online pharmacy gabapentin dosage treating yeast infections dog health info cymbalta anxiety cheap tramadol without prescription hydrea drugs used for cancer cure for high blood pressure alcohol and valium relief from constipation liver infection treatment cialis soft zantac medication help sleep problems all natural antibiotics order medication without prescription sleep problems free hypnotherapy gaining muscle mass cheap viagra order online natural help for pain how to buy viagra drug price celebrex information otc diuretic levitra 10 mg buy medicine online pets products relief foot pain cialis without prescription med care cheapest generic cialis rapid hair loss pain medications generic side effects meds without prescriptions cat anxiety buy simplicef natural cure arthritis effects of high blood pressure lowest price generic viagra how to get birth control new breast cancer drug buy topamax blood pressure meds when are beta blockers prescribed how to get pain meds order fosamax online viagra name order viagra viagra cialis cat's eye health how to relieve lower back pain treating ear infections diazapan is valium online pain doctors high blood pressure in elderly medication to stop smoking wellbutrin dosages diabetes blood sugar levels weight loss diet pill side effects of prescribed pain pills drug list high blood pressure buy cialis online in usa ultram cost how to help osteoporosis how to use clomid discount brand viagra wellbutrin cymbalta buy pills without a prescription buy pain medicine online tab tramadol depression symptoms treatment how levitra work hypertension medications beta blockers prevent premature ejaculation xanax interactions with other medicines purchase medicine on line does alli work xenical mexico prescriptions buy sumycin uy prescription medication without a prescription ambien cost methocarbamol effects cheap beta blockers cats bladder reduce cholesterol naturally metformin tablet scabies medicine breast enhancer pills body building over 50 order viagra cheap zestril medication how to buy prescription medications online pharma kamagra drugs depression ear infection symptoms big muscle controlling blood pressure pain meds and pregnancy buy diazepam without prescription skin allergies antibiotic zoloft buy weight loss nutrition program Buy Cialis breast increase meds without prescriptions blood clots medical edema treatment for flu best hangover remedy diabetes drugs