0

Well, i have class named "Consultation" that have a list of "ItemBudget", and i have a class named "Budget" that have a list of "ItemOrcamento" too. The relation Consultation<-->ItemBudget is ManyToMany, and relation Budget<-->ItemBudget is OneToMany.

In JSF i do the following:

 <p:dataTable rowKey="#{item.id}" var="item" value="#{consultationMB.consultation.budget.itensAsList}" selection="#{consultationMB.itemBudget}" >

I use method "getItensAsList" that return a ArrayList() instead of a HashSet() that primefaces dataTable tag can't read correctly

As you can see, my selection is "itemBudget", so in my ManagedBean called ConsultationMBImpl i try to execute the following:

 if (!itemBudget.getSituation().getCode().equals("WAITING_CONCLUSION")){
    //some code here
 }

When i try make the code above all fields that have reference to another class like: Situation, Dentist and others have this: "Dentist_javassist_32", "Situation_javassist_49"... And all fields are null or zero.

Aritz
  • 30,971
  • 16
  • 136
  • 217
Shelly
  • 1,035
  • 5
  • 27
  • 51

1 Answers1

3

That's caused by Hibernate, which loads a proxy (suffixed with _javassist) instead of the original object. If you debug your code with an IDE and try to call a getter on the fly, you'll get the real value, even it seems to be null for the property.

Why is it that done? Because it's much faster to load proxies rather than real objects for the ORM tool. Hibernate keeps a cache with already loaded objects instead of hitting the DB once and again.

If you want to avoid lazy loading, you could use a get instead of a load method over Hibernate's Session. Also for its relations, you can mark them as lazy="false", so Hibernate will load them as real objects. If you want to directly unproxy an already loaded instance, there are also some methods to achieve that.

However, don't do that unless you strictly require it. As I said before, that will make Hibernate load more information from DB and consequently, loose eficiency.

Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217
  • Very good explanation, reading this and analysing my code i understand my problem. Thanks @XTreme Biker. But i have a doubt: Why use Javassist instead of CGLIB as a Proxy ? – Shelly Nov 12 '13 at 17:12
  • That's an Hibernate [team's decission](http://in.relation.to/Bloggers/DeprecatedCGLIBSupport). – Aritz Nov 13 '13 at 07:19