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.

4

1 回答 1

3

这是由Hibernate引起的,它加载了一个代理(后缀为_javassist)而不是原始对象。如果您使用 IDE 调试代码并尝试即时调用 getter,您将获得真正的价值,即使它似乎是null针对该属性的。

为什么这样做?因为为 ORM 工具加载代理而不是真实对象要快得多。Hibernate保留一个包含已加载对象的缓存,而不是一次又一次地访问数据库。

如果你想避免延迟加载,你可以在 Hibernate 的 Session 上使用 aget而不是方法。load同样对于它的关系,您可以将它们标记为lazy="false",因此 Hibernate 会将它们作为真实对象加载。如果您想直接取消代理已加载的实例,也有一些方法可以实现。

但是,除非您严格要求,否则不要这样做。正如我之前所说,这将使 Hibernate 从 DB 加载更多信息,从而降低效率。

于 2013-11-12T16:59:44.517 回答