0

I am pretty new in Hibernate and I have the following doubt.

Into a class I have this field that retrieve a list of Twp1007Progetto objects:

@OneToMany(fetch=FetchType.EAGER)
@JoinColumns({
    @JoinColumn(name="COD_MEC_ATT", referencedColumnName="COD_SCU_UT"),
    @JoinColumn(name="DAT_ANN_SCO_ATT", referencedColumnName="DAT_ANN_SCO_RIL")
    })
private List<Twp1007Progetto> twp1007Progettos;

This works fine but now I have to add an order behavior.

So the Twp1007Progetto object have this String field:

@Column(name="FLG_TIP_PRG")
private String flgTipPrg;

which can have only the following values (these values are defined into the DB): W or L or C or L or S).

So now I want use Hibernet to order the previous twp1007Progettos list according to the flgTipPrg value.

So for example if the list contains two W element, three C element and one S element, the retrieved list have to be something like: [C, C, S, W] (alphabetically ordered).

How can I do something like this using Hibernate?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

1 Answers1

0

Are you using hibernate criteria's or HQL query to fetch the results?

when using criteria you can use

.addOrder( Order.asc("name") )
.addOrder( Order.desc("age") )

refer the link How do I sort list with criteria in hibernate

if you are using HQL query, you can use the order by in that query directly.

if the list is obtained, you can use the Java collection package to sort (comparator interface will help).

Community
  • 1
  • 1
Tim
  • 1,321
  • 1
  • 22
  • 47