Order multiple one-to-many relationships

I have a search screen using JSF, JBoss Seam and Hibernate at the bottom. There are columns for A, Band C, where the relationships are:

A (1< --; >*) B (1< --; >*) C

Suppose y Ais List< B >and y Bis List< C >(both are one-to-many relationships).

The user interface table supports sorting by any column (ASC or DESC), so I want to sort the query results. For this reason, I used lists in the model.

However, I got an exception that Hibernate cannot willingly receive multiple packages (it considers both lists to be packages). There is an interesting blog here , and they highlight the following solutions:

  1. Use the @IndexColumn 'annotation (it’s not in my database, and what else, I want the position of the results to be determined in order and not in the index column)
  2. Get lazy (for performance reasons, I need an impatient receive)
  3. Change installation list

I changed the list to Set, which, by the way, is more correct according to the model.

  • First, if you do not use @OrderBy, the PersistentSetreturn of Hibernate wraps HashSetthat is out of order. Therefore, when I iterate over it in the user interface, the order is randomly selected, regardless of what the order in the database did.
  • -, @OrderBy, PersistentSet LinkedHashSet, , , . OrderBy , , Collections () HQL (). , , , .

Sets SortedSet ( , TreeSet), :

  1. , , , TreeSet ( Comparator, TreeSet ).

  2. , Hibernate @Sort, SortOrder.UNSORTED Comparator. , , , .

.

Maven Google Code. .

+5
2

, ? , , . , .

, , Hibernate bok:

(, , TreeBag), ; .

+3

, Hibernate , , , , .

@Entity
public class Aa {

     private List<Bb> bbList - new ArrayList<Bb>();

     @OneToMany
     public List<Bb> getBbList() {
         return bbList;
     }

     @Transient
     public List<Bb> getBbListSortedBySomeProperty() {
         Collections.sort(bbList, new Comparator<Bb>() {
             public int compare(Bb o1, Bb o2) {
                 return o1.getSomeProperty().compareTo(o2.getSomeProperty());
             }
         });

         return bbList;

     }

}

, someProperty Comparable

...

@Entity
public class Bb {

     private List<Cc> ccList - new ArrayList<Cc>();

     @OneToMany
     public List<Cc> getCcList() {
         return ccList;
     }

}
0

All Articles