Query result "shape" #4917
Replies: 7 comments 14 replies
-
IMO we should apply the unique filter only for single entity selects OR not at all (if we prefer consistency). Applying the unique filter for single entity selects makes sense IMO, because I think that when people use a single entity select, duplicity doesn't matter to them. If duplicity would matter, people could request the type If we decide to not do unique filtering at all, we should re-introduce the Overall, I think it's manageable for users to understand that unique filtering is only enabled for single entity selects and that this can be overridden by using a requested type like |
Beta Was this translation helpful? Give feedback.
-
In my opinion when the query selects just the entity id we should apply the distinct filter automatically as well. |
Beta Was this translation helpful? Give feedback.
-
I may be a bit dense, but... why would you need to "make" the result of these queries unique to begin with? Aren't these results already unique? I wouldn't expect duplicate orders in either case: final String selectOrderHql = "select o from Order o where o.customer.country.id = 1";
// Here, Orders will be uniqued
final List<Order> orders = session.createQuery( selectOrderHql, Order.class ).list();
// Here, however, they will not
final List<Order> orders = session.createQuery( selectOrderHql, Object[].class ).list(); Are we talking about more complex queries than this, with explicit joins on mutli-valued associations? Or a more complex model with multiple rows per entity (Envers comes to mind)? Or about duplication caused by how the query is executed internally (implicit cross-joins for collections, ...)? Also... this should be obvious, but considering that I didn't understand the rest, I'd rather check: the query using - final List<Order> orders = session.createQuery( selectOrderHql, Object[].class ).list();
+ final List<Object[]> orders = session.createQuery( selectOrderHql, Object[].class ).list(); |
Beta Was this translation helpful? Give feedback.
-
Not a bad idea in general. I very much dislike it returning a
But why? Specifically, why is a DISTINCT needed? What are these use cases that say I select a single item, but want to keep duplicates? Real use cases... Especially if that selection is an entity; to me any single selection. If I have multiple selections, perhaps I am defining a cross-join / product and want all pairs.. that's a valid argument for me and why I only applied it to single selections. To me, a single selection wrapped in an array / Type is akin to syntactic sugar, mainly for backwards compatibility. If the consensus is that array / Tuple magically implies non-uniqueness, then so be it. And really, if the consensus is that we should not do any implicit unique-ing regardless of the selected expressions (entity versus scalar, e.g.) or requested result type... again, so be it. But then imo we should not do any in-memory unique-ing, period[1]. DISTINCT should just get passed to the database. [1] Though @yrodiere's suggestion about a new method (plus a new matching hint imo) are good. This would trigger in-memory de-duplication |
Beta Was this translation helpful? Give feedback.
-
So consensus -
|
Beta Was this translation helpful? Give feedback.
-
Another thing to consider... I've had to fix quite a number of tests regarding the now-removed de-duplication of single entity results. Where this is extremely useful is the case of collection fetches.
This results in a List containing duplicate Customer entries, one per number of orders they have. I think this is something we should not remove. This is beyond simply "useful or convenient" imo |
Beta Was this translation helpful? Give feedback.
-
I just commented over on the issue, that I think dupe removal should only happen to undo the effect of a It should not happen in any other case. |
Beta Was this translation helpful? Give feedback.
-
This comes from the work done in 6.0 overall, but specifically in relation to #4890
Specifically concerned we do not always have the best outcomes for the shape of Query results and how uniqueness is applied. Much of this goes back to the feature added in 6.0 for selection of a single entity. To help best explain this, let's consider a simple Customer/Order domain model and ways we might query it. Some of the discussion comes down to "requested result type" also.
The main concern I have is that the same exact query will behave differently depending on the exact "requested result type". Let's look at a few examples...
Explicit entity selection
You can define the result here as either a single selection (
Order
) or as a multi-select (Object[]
orTuple
). And the outcome is different for each case in terms of uniqueness -This is the one that got me thinking about this in terms of a possible inconsistency.
Explicit scalar selection
This one (specifically selecting the entity versus selecting its id and potentially getting back different number of results) also made me think about it being a possible inconsistency.
Implicit selection
This is was the original use-case behind HHH-15133 (#4890). Specifically a query like
Historically, Hibernate would return an array with an element for the root (Order) plus any joins (Customer, Country).
6.0 made the switch to instead return just the root (Order).
The work for HHH-15133 allows seamless handling of this simply by specifying the result type. E.g.
Discussion
The main point to discuss here in my opinion is the uniquing of results.
Do we just say, as a blanket statement, that we only implicitly unique the results when there is only a single entity selection and it is requested as the result type? Does the same hold true for implicit selections of a single entity? I am fine with this as it can, at least, be consistently applied and explained/documented. To be clear, both of these would be uniqued:
Agreed?
But why just unique single entity selections? Why not unique any single selection? E.g., why not unique this result as well?
Overall the thing that bothers me is that these queries can all return different number of results which feels odd -
Beta Was this translation helpful? Give feedback.
All reactions