Close

JPA - Maintaining the persistent order of a list with @OrderColumn

[Updated: Jun 8, 2018, Created: Jun 8, 2018]

@OrderColumn annotation specifies a column that should maintain the persistent order of a list.
The persistence provider maintains the order and also updates the ordering on insertion, deletion, or reordering of the list.

This annotation can be used on @OneToMany or @ManyToMany relationship or on @ElementCollection.

Example

Entity

@Entity
public class Employee {
  @Id
  @GeneratedValue
  private long id;
  private String name;
  private double salary;
  private String dept;
  @ElementCollection
  @OrderColumn
  private List<String> phoneNumbers;
    .............
}

Mapped tables

public class ExampleMain {
  private static EntityManagerFactory entityManagerFactory =
          Persistence.createEntityManagerFactory("example-unit");

  public static void main(String[] args) {
      try {
          nativeQuery("SHOW TABLES");
          nativeQuery("Show Columns from Employee");
          nativeQuery("Show Columns from Employee_PhoneNumbers");

      } finally {
          entityManagerFactory.close();
      }
  }

  private static void nativeQuery(String s) {
      EntityManager em = entityManagerFactory.createEntityManager();
      System.out.printf("'%s'%n", s);
      Query query = em.createNativeQuery(s);
      List list = query.getResultList();
      for (Object o : list) {
          if (o instanceof Object[]) {
              System.out.println(Arrays.toString((Object[]) o));
          } else {
              System.out.println(o);
          }
      }
      em.close();
  }
}
'SHOW TABLES'
[EMPLOYEE, PUBLIC]
[EMPLOYEE_PHONENUMBERS, PUBLIC]
'Show Columns from Employee'
[ID, BIGINT(19), NO, PRI, NULL]
[DEPT, VARCHAR(255), YES, , NULL]
[NAME, VARCHAR(255), YES, , NULL]
[SALARY, DOUBLE(17), NO, , NULL]
'Show Columns from Employee_PhoneNumbers'
[EMPLOYEE_ID, BIGINT(19), NO, PRI, NULL]
[PHONENUMBERS, VARCHAR(255), YES, , NULL]
[PHONENUMBERS_ORDER, INTEGER(10), NO, PRI, NULL]

Persisting data

public class ExampleMain2 {
  private static EntityManagerFactory entityManagerFactory =
          Persistence.createEntityManagerFactory("example-unit");

  public static void main(String[] args) {
      try {
          persistEmployees();
          nativeQuery("Select * FROM Employee");
          nativeQuery("Select * FROM Employee_PhoneNumbers");
      } finally {
          entityManagerFactory.close();
      }
  }

  public static void persistEmployees() {
      Employee employee1 = Employee.create("Diana", 2000, "IT",
               "111-111,111", "222-222,222");
      Employee employee2 = Employee.create("Denise", 2500, "Admin",
              "333-333-333", "444-444-444", "666-666-666");
      Employee employee3 = Employee.create("Linda", 4500, "Sales",
              "555-555-555");
      EntityManager em = entityManagerFactory.createEntityManager();
      em.getTransaction().begin();
      em.persist(employee1);
      em.persist(employee2);
      em.persist(employee3);
      em.getTransaction().commit();
  }

  public static void nativeQuery(String s) {
      EntityManager em = entityManagerFactory.createEntityManager();
      System.out.printf("'%s'%n", s);
      Query query = em.createNativeQuery(s);
      List list = query.getResultList();
      for (Object o : list) {
          if (o instanceof Object[]) {
              System.out.println(Arrays.toString((Object[]) o));
          } else {
              System.out.println(o);
          }
      }
      em.close();
  }
}
'Select * FROM Employee'
[1, IT, Diana, 2000.0]
[2, Admin, Denise, 2500.0]
[3, Sales, Linda, 4500.0]
'Select * FROM Employee_PhoneNumbers'
[1, 111-111,111, 0]
[1, 222-222,222, 1]
[2, 333-333-333, 0]
[2, 444-444-444, 1]
[2, 666-666-666, 2]
[3, 555-555-555, 0]

Example Project

Dependencies and Technologies Used:

  • h2 1.4.197: H2 Database Engine.
  • hibernate-core 5.2.13.Final: The core O/RM functionality as provided by Hibernate.
  • JDK 1.8
  • Maven 3.3.9

@OrderColumn Example Select All Download
  • jpa-order-column-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
          • META-INF

See Also