Don't select all columns of child

Hello,
I’m using hibernate models for articles.
I know there are hints for hibernate to “ignore” fields and such, or create groups to only select specific fields from a model, but the issue I’m having is a recursive issue:
My articles have a few recommended other articles.

This is a simple Many to many in a Set with a maximum of 5 other Articles.

However because those articles also have recommended articles; this makes it very slow.

(side note: I’m using it with the play framework and json serializing, so all nodes are visited, no way to lazy load it in that regard)

What I’d want to do is have hibernate only go 1 layer deep into recommendeds (but still get the Author of a recommend article), or somehow be able to merge a group of what fields I want with the childs specifically.

As far as I’ve found out I can only apply a grouping to the base Model.

I can’t seem to find anyway to limit just the childs of my Article, without modifying the base.

Thanks in advance!

If you only have 5 recommended articles then you can just set 5 FK in each article.

Having a many-to-many association makes sense when the number of combinations is variable.

Anyway, you can still achieve your goal with JPQL or native SQL.

  1. You can fetch the root articles, get their ids, and pass them when querying the recommended table which you limit to 5 results.
  2. You can use a UNION ALL and include the logic above while executing just one query.

Thank you for your reply!

I have it working perfectly with the 5 maximum limit, which is not the issue. Hibernate queries it very well itself too.

My concern was that I wanted to retrieve the parent Article with recommended, but then the child Set without their recommended.

Or do you mean I can also do this with a UNION ALL query?
I thought selecting only a few fields would change whatever you queried into an Object.
Also how would I write this query then?

Something along:
SELECT author, title, content,date,recommended.title,recommended.date, WHERE …

Would that work with sets?

You can use a DTO projection to avoid using Object arrays.

Using a ResultTransformer allows you to fetch child rows too.