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.