Publicado por Unknown en 1:44 p. m.





hibernate performance tip. Do you remember the model of previous hibernate Let's introduce anotherperformance tip. Do you remember the model of previous post ? We had a starship and officer related with a one to many association.









Now we have next requirement:

We shall get all officers assigned to a starship by alphabetical order.

To solve this requirement we can: implementing an HQL query with order by clause. using sort approach. using order approach.

The first solution is good in terms of performance, but implies more work as a developers because we should write a query finding all officers of given starship ordered by name and then create a finder method in DAO layer (in case you are using DAO pattern).





Let's explore the second solution, we could use SortedSet class as association, and make Officer implements Comparable, so Officer has natural order. This solution implies less work than the first one, but requires using @Sort hibernate annotation on association definition. So let's going to modify previous model to meet our new requirement. Note that there is no equivalent annotation in JPA specification.





First we are going to implement Comparable interface in Officer class.

We are ordering officer by name by simply comparing name field. Next step is annotating association with @Sort.

Notice that now officers association is implemented using

instead of a

. Furthermore we are adding

annotation to relationship, stating that officers should be natural ordered. Before finishing this post we will insist more in

topic, but for now it is sufficient.





And finally a method that gets all officers of given starship ordered by name, printing them in log file.









All officers are sorted by their names, but let's examine which queries are sent to RDBMS.

First query is resulting of calling find method on EntityManager instance finding starship.





Because one to many relationships are lazy by default when we call getOfficers method and we access first time to SortedSet, second query is executed to retrieve all officers. See that no order by clause is present on query, but looking carefully on output, officers are retrieved in alphabetical order.









So who is sorting officer entities? The explanation is on @Sort annotation. In hibernate a sorted collection is sorted in memory being Java the responsible of sorting data using compareTo method.

clause and using annotation instead of writing a query.

@OrderBy annotation, available as hibernate annotation and JPA annotation, let us specifies how to order a collection by adding “order by" clause to generated SQL.



Keep in mind that using javax.persistence.OrderBy allows us to specify the order of the collection via object properties, meanwhile org.hibernate.annotations.OrderBy order a collection appending directly the fragment of SQL (not HQL) to order by clause.

method nor a

. We only need to annotate officers field with

annotation. Since in this case we are ordering by simple attribute,

annotation is used to maintain fully compatibility to other “

”

engines. By default ascendent order is assumed.

Both queries are still executed but note that now select query contains order by clause too.





With this solution you are saving process time allowing RDBMS sorting data in a fast-way, rather than ordering data in Java once received.



Furthermore OrderBy annotation does not force you to use SortedSet or SortedMap collection. You can use any collection like HashMap, HashSet, or even a Bag, because hibernate will use internally a LinkedHashMap, LinkedHashSet or ArrayList respectively.

In this example we have seen the importance of choosing correctly an order strategy. Whenever possible you should try to take advantage of capabilities of RDBMS, so your first option should be using OrderBy annotaion (hibernate or JPA), instead of Sort. But sometimes OrderBy clause will not be enough. In this case, I recommend you using Sort annotation with custom type (using java.util.Comparator class), instead of relaying on natural order to avoid touching model classes.

Obviously this method is not the best performance-way to sort a collection of elements. It is likely that we'll need a hybrid solution between usingAnd this leads us to explain the third possibility, using ordering approach.Now Officer class should not be touched, we don't need to implementAnd if we rerun get all officers method, next queries are sent:I wish this post helped you to understand differences betweenandinKeep learning.Music: http://www.youtube.com/watch?v=VHoT4N43jK8&ob=av3n