-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: eager fetching of lazy formulas via criteria api
PostPosted: Mon May 30, 2011 7:32 am 
Newbie

Joined: Mon May 30, 2011 7:00 am
Posts: 3
I have simple entity with lazy @Formula property:
Code:
public class TestEntity {
    public Long testId;

    @Column(name = "test_id")
    @Id
    public Long getTestId() {
        return testId;
    }

    public void setTestId(Long testId) {
        this.testId = testId;
    }

    private CollectionType custom;

    @Formula(value = "fa.GET_REASONS_NAMES(test_id)")
    @Type(type = "ru.esoft.Platform.hibernatetools.CollectionTypeDescriptor",
            parameters = @org.hibernate.annotations.Parameter(name = "columns", value = "String name"))
    @Basic(fetch = FetchType.LAZY, optional = true)
    @LazyToOne(value = LazyToOneOption.FALSE)
    public CollectionType getCustom() {
        return custom;
    }

    public void setCustom(CollectionType custom) {
        this.custom = custom;
    }
}


I made instrumentation for this class and it works fine.
Sample code for testing:
Code:
List list = session.createCriteria(TestEntity.class, "this").
                    setFetchMode("this.custom", FetchMode.JOIN). // fetch me custom please
                    add(Restrictions.idEq(489281L)).list();
            for (Object o : list)
                System.out.println(((TestEntity)o).getCustom());


Unfortunately, the method ((TestEntity)o).getCustom() invokes an additional select-statement for fetching formula:
Code:
Hibernate:
    select
        this_.test_id as test1_0_0_
    from
        instr_test this_
    where
        this_.test_id = ?
Hibernate:
    select
        fa.GET_REASONS_NAMES(testentity_.test_id) as formula0_
    from
        instr_test testentity_
    where
        testentity_.test_id=?
489281-10-0005, 489281-10-0004

When i use this code:
Code:
List list = session.createCriteria(TestEntity.class, "this").
                    setProjection(Projections.projectionList().
                            add(Projections.property("id")).
                            add(Projections.property("custom"))).
                    setFetchMode("this.custom", FetchMode.JOIN).
                    add(Restrictions.idEq(489281L)).list();

I receive only one select-query execution, but result is List of Object[]. This is not useful.
How can i get List of TestEntities with eagerly fetched custom fields?

PS. Sorry for my English.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.