Example of @OrderColumn in Hibernate
April 09, 2019
@OrderColumn
is used to store list index. As list is ordered collection, so to maintain the list index there can be a column in database and while inserting the data, list index will be stored in that column. For this the property in entity should be annotated with
@OrderColumn (name="column-name")
@OrderColumn
.
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.OrderColumn; @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") @OrderColumn(name="list_index") 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.ArrayList; 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(); State s1= new State(1,1,"UP"); State s2= new State(2,1,"MP"); State s3= new State(3,1,"HP"); State s4= new State(4,1,"DELHI"); List<State> states= new ArrayList<State>(); states.add(s1); states.add(s2); states.add(s3); states.add(s4); Country country= new Country(1,"India",states); session.persist(country); session.getTransaction().commit(); session.close(); } }