School:Degree is one:many.
The result of this operation is a list of Schools where each school appears in the list once:
Code:
HibernateUtil.getSession()
.createCriteria(School.class)
.addOrder(Order.asc("name"))
.list()
However, if I set a JOIN fetch mode on the association (to avoid the n + 1 SQL execution problem downstream),
then each School appears appears in the list multiple times (the number of occurrences = the number of degrees associated with the school):
Code:
HibernateUtil.getSession()
.createCriteria(School.class)
.setFetchMode("degrees", FetchMode.JOIN)
.addOrder(Order.asc("name"))
.list()
I understand why this is happening, and it is relatively simple to create a Set from the List,
but I figured - incorrectly - that Hibernate would automatically deal with the denormalized SQL result set and return a unique list by default. Perhaps something like a
criteria.uniqueList method, similar to
uniqueResult would, at least in some cases, make changing fetch modes a more brainless operation.