简体   繁体   中英

Why I got HibernateProxy obj at run mode, but get domain in debug mode. How does that happened

I have domain Object like

public class Person{
  private String name ;
  private Integer age;
}

I have DAO criteria like

{
  Criteria c = sesseion.createCriteria(Person.class);
  c.setProjection(Projections.distinct(Projections.projectionList().add(Projections.property("age"))));
  List<Object> rows = c.list();
  for(Object obj:rows){
     if ((obj != null) && (obj instanceof HibernateProxy))
    {
      doSomething...
    }
  }
}

When I run it in Run mode, it works, but when I set debug point at "if" condition line, it shows the obj I get is Person class, not HibernateProxy. Then it failed in debug mode.

Why one object is appearing as different instance form with run mode and debug mode?

Hibernate loads the proxies of your classes initially (based on the loading strategy) and when you call some method on that proxy, it actually loads the original object. While in debug mode, eclipse uses introspection (calls the fields or setters or getters).

In your case toString() on proxy gets called, which internally uses other fields of that class, hence, hibernate loads the original object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM