Hibernate criteria with projection not performing query for @OneToMany mapping
I have a domain object, Expense, that has a field called initialFields.
It's annotated as so:
@OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL }, orphanRemoval = true)
@JoinTable(blah blah)
private final List<Field> initialFields;
Now I'm trying to use Projections in order to only pull certain fields for performance reasons, but when doing so the initialFields field is always null. It's the only OneToMany field and the only field I am trying to retrieve with the projection that is behaving this way. If I use a regular HQL query initialFields is populated appropriately, but of course I can't limit the fields.
Partial projection code:
Criteria criteria = session.createCriteria(Payment.class);
criteria.createAlias("expense", "e");
ProjectionList properties = Projections.projectionList();
//Some restrictions and more fields
properties.add(Projections.property("e.initialFields"), "initialFields");
criteria.setProjection(properties);
criteria.setFetchMode("e.initialFields", FetchMode.JOIN);
criteria.setReadOnly(true);
criteria.setResultTransformer(Transformers.aliasToBean(Expense.class));
return criteria.list();
When I turn on debugging and turn on show sql, the query to pull the initialFields doesn't appear to be created/run. Anyone ever seen anything like this?
I've just tried using HQL projection, by specifying each field I want to pull and then manually building the object. In this case the SQL built by Hibernate is incorrect for the initialFields field. expense1_.name as col_1_0_, . as col_2_0_, expense1_.account_id as col_3_0_
. The . as col_2_0_ is
where the initialFields would be pulled. My guess is that it should be expense1_.id as col_2_0_
.
Edit: I just removed the Result Transformer to inspect each property and the ini开发者_如何学运维tialFields property is indeed null.
I had similar problem and for me was working explicitly specifying joinType in criteria.
Criteria criteria = session.createCriteria(Payment.class);
criteria.createAlias("expense", "e", CriteriaSpecification.LEFT_JOIN);
精彩评论