Hibernation criteria with a request that does not fulfill the request for @OneToMany mapping

I have a domain object, Expense, that has a field called initialFields.

It is annotated like this:

@OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL }, orphanRemoval = true) @JoinTable(blah blah) private final List<Field> initialFields; 

Now I'm trying to use Projections to retrieve specific fields only for performance reasons, but the initialFields is always null. This is the only OneToMany field and the only field I'm trying to extract with a projection that behaves this way. If I use a regular HQL query, then the initial fields are populated accordingly, but of course I cannot 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 initialFields output request is not created / started. Has anyone seen anything like this?

I just tried using the HQL projection by specifying each field that I want to pull out, and then manually creating the object. In this case, the SQL built by Hibernate is not valid for the initialFields field. expense1_.name as col_1_0_, . as col_2_0_, expense1_.account_id as col_3_0_ expense1_.name as col_1_0_, . as col_2_0_, expense1_.account_id as col_3_0_ . . as col_2_0_ is . as col_2_0_ is , where the initial fields will be displayed. I guess this should be expense1_.id as col_2_0_ .

Edit: I just deleted Transformer to check each property, and the initialFields property is really null.

+4
source share
1 answer

I had a similar problem, and worked for me explicitly, specifying joinType in the criteria.

 Criteria criteria = session.createCriteria(Payment.class); criteria.createAlias("expense", "e", CriteriaSpecification.LEFT_JOIN); 
+3
source

Source: https://habr.com/ru/post/1335150/


All Articles