简体   繁体   中英

Hibernate 4.3.5 DetachedCriteria not fetching lazy list

I have a simple query that worked for a good time, now i changed some stuff in my code to:
(hibernate.cfg.xml)

<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.current_session_context_class">thread</property>

and the following code:

public UserEn findByIDFetch(Long id, String... fetches) {
    beginTransaction();

    DetachedCriteria dc = DetachedCriteria.forClass(UserEn.class);
    for (String fetch : fetches) {
        dc.createAlias(fetch, fetch, JoinType.INNER_JOIN);
    }

    dc.add(Restrictions.eq("id", id));

    UserEn result = (UserEn) dc.getExecutableCriteria(getSession()).uniqueResult();
    commitTransaction();
    return result;
}

after the commit, result object don't have the list loaded (that is comming from fetches )
if i do result.getAnyList().size(); before the commit, it keeps loaded, also, if i use HQL it works perfectly. (but it's not how it's supposed to work (let it open and load when used), createAlias should work fine as always)

It looks like DetachedCriteria's createAlias is not fetching the given path (in this case is characterEnList )

@EDIT
I found out that if i use setFetchMode in the desired path ( characterEnList in this case) the fetching works but if i use createAlias (as usual) it simply stops working, i don't know how, probably a hibernate bug or something, anyway, i'll wait for someone answer, maybe someone that had the same problem...

it is lazy loaded only when accessed during the transaction. So you must call the lazy loaded field during the transaction, for hibernate to fetch it. It will then be available in the view.

or of course make it eager.

Found as the same problem as follows: https://hibernate.atlassian.net/browse/HHH-7842 This is probably a bug, if i ask it to be inner join in the alias, it should be, i can do this with HQL, why can't i do with Criteria?

So, now i will have to use the LEFT_OUTER_JOIN type to solve this, or don't use alias at all (if i don't need to make a where clause or 2-depth fetch)

Hope it help someone, thanks for all your help!

example

dc.createAlias(fetch, fetch, JoinType.LEFT_OUTER_JOIN);

OR

dc.setFetchMode(fetch, FetchMode.JOIN);

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