开发者

how can I determine whether hibernate has 'lazy' loaded the proxy or the real object?

New to hibernate here. I'm using Hibernate 3.5, which I understand should use lazy fetching by default. I have SQL logging enabled via

<property name="show_sql">true</property>

I'm requesting object A which has a reference to object B which holds the actual byte array of data. I pushed the data into object B so that the data doesn't get fetched from the DB unless really needed, but when I request object A, the heap jumps drastically, as if it's fetched the data anyway, and I get this output from hibernate SQL logging:

Hibernate: select attachment0_.id as id11_0_, attachment0_.data as data11_0_ from attachment_data attachment0_ where attachment0_.id=?

I'm unclear on how to interpret this, specifically the 'as' statement. The 'attachment0.data' seems to be the byte array in object B. Is hibernate saying that it's created a proxy for the array, or is this saying it's actually pulled the data from the DB? If it's just created a proxy, would I see no select output for the proxy?

So in summary, the main question of how can I determine whether I have a proxy or real object, and the related question of how to interpret the select statement?

I have dived into the hibernate doc开发者_如何学Gos, as well as searched on the web quite a bit, but most of the info seems to be a step above the basic knowledge I'm missing, so any help is appreciated.


The object you have can be a proxy with all the data loaded. If your want to unproxy the object use the following in Hibernate:

From an org.hibernate.impl.SessionImpl you can get the org.hibernate.engine.PersistenceContext and then

SessionImpl session = ...;
PersistenceContext persistenceContext = session.getPersistenceContext();
Object entity = persistenceContext.unproxy(maybeProxy);

The javadoc for unproxy

/**
 * Get the entity instance underlying the given proxy, throwing
 * an exception if the proxy is uninitialized. If the given object
 * is not a proxy, simply return the argument.
 */
public Object unproxy(Object maybeProxy) throws HibernateException;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜