Example of @OrderBy in Hibernate
May 22, 2013
@OrderBy orders the column values and put into the list as ordered list data. By Default @OrderBy orders the element in ascending order. We need to define property name on the basis of which values will be ordered. In our example, a country has many states. Country and States are associated by @OneToMany. We are ordering states on the basis of state name. Syntax is:
@OrderBy("property name")
Country.java
package com.concretepage.persistence; import java.io.Serializable; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.OrderBy; @Entity public class Country implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name="id") private int id; @Column(name="name") private String name; @OneToMany(cascade=CascadeType.ALL) @JoinColumn(name="country_id") @OrderBy("name") private List<State> states; public Country(int id,String name,List<State> states){ this.id=id; this.name=name; this.states=states; } public Country(){ } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<State> getStates() { return states; } public void setStates(List<State> states) { this.states = states; } }
package com.concretepage.persistence; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "state") public class State { @Id @Column(name = "id") private int id; @Column(name = "name") private String name; public State(int id,int countryId,String name){ this.id=id; this.name=name; } public State(){} public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
package com.concretepage.util; import java.util.List; import java.util.Properties; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import com.concretepage.persistence.Country; import com.concretepage.persistence.State; public class HibernateUtil { private static final SessionFactory concreteSessionFactory; static { try { Properties prop= new Properties(); prop.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernate"); prop.setProperty("hibernate.connection.username", "root"); prop.setProperty("hibernate.connection.password", ""); prop.setProperty("hibernate.hbm2ddl.auto", "update"); prop.setProperty("dialect", "org.hibernate.dialect.MySQLDialect"); concreteSessionFactory = new AnnotationConfiguration() .addPackage("com.concretepage.persistence") .addProperties(prop) .addAnnotatedClass(Country.class) .addAnnotatedClass(State.class) .buildSessionFactory(); } catch (Throwable ex) { throw new ExceptionInInitializerError(ex); } } public static Session getSession() throws HibernateException { return concreteSessionFactory.openSession(); } public static void main(String... args){ Session session=getSession(); session.beginTransaction(); Country c=(Country)session.get(Country.class, new Integer(1)); List<State> states = c.getStates(); for(State s: states){ System.out.println(s.getName()); } session.close(); } }
and output as follows
DELHI HP MP UP