Kohei Nozaki's blog 

Just try to use OrderColumn


Posted on Thursday Jan 30, 2014 at 02:09PM in Technology


It sounds convenience so I just tried it.

Environment

  • WildFly 8.0.0.Final
  • Hibernate 4.3.1
  • PostgreSQL 9.2.4
  • postgresql-9.3-1100.jdbc41.jar

Does Hibernate require intersection entity for @OrderColumn?

According to [2], it said that @OrderColumn doesn't work with relations like that:

Parent side

    @OneToMany(mappedBy = "dept")
    @OrderColumn(name = "pos")
    private List<Employee> employees;

Child side

    @ManyToOne
    @JoinColumn(nullable = false)
    private Dept dept;

I found [1], and it said something like that it working now with recent versions.

And, like said at [2], when I delete mappedBy, it makes intersection entity (dept_employee).

Make example project

Whole project resources are available in GitHub.

Codes that I will mention later are easy to find in these files:

Entities in the project

Test data

Available in GitHub.

Data entry to empty table

log

19:51:55,703 DEBUG [org.hibernate.SQL] (default task-2) select nextval ('hibernate_sequence')
19:51:55,704 DEBUG [org.hibernate.SQL] (default task-2) select nextval ('hibernate_sequence')
19:51:55,705 DEBUG [org.hibernate.SQL] (default task-2) select nextval ('hibernate_sequence')
19:51:55,706 DEBUG [org.hibernate.SQL] (default task-2) select nextval ('hibernate_sequence')
19:51:55,712 DEBUG [org.hibernate.SQL] (default task-2) insert into Dept (deptName, id) values (?, ?)
19:51:55,714 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [1] as [VARCHAR] - [Finance]
19:51:55,714 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [2] as [BIGINT] - [1]
19:51:55,716 DEBUG [org.hibernate.SQL] (default task-2) insert into Employee (dept_id, firstName, lastName, pos, id) values (?, ?, ?, ?, ?)
19:51:55,716 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [1] as [BIGINT] - [1]
19:51:55,716 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [2] as [VARCHAR] - [Taro]
19:51:55,717 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [3] as [VARCHAR] - [Yamada]
19:51:55,717 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [4] as [INTEGER] - [null]
19:51:55,717 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [5] as [BIGINT] - [2]
19:51:55,719 DEBUG [org.hibernate.SQL] (default task-2) insert into Employee (dept_id, firstName, lastName, pos, id) values (?, ?, ?, ?, ?)
19:51:55,719 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [1] as [BIGINT] - [1]
19:51:55,719 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [2] as [VARCHAR] - [Jiro]
19:51:55,719 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [3] as [VARCHAR] - [Suzuki]
19:51:55,719 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [4] as [INTEGER] - [null]
19:51:55,719 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [5] as [BIGINT] - [3]
19:51:55,720 DEBUG [org.hibernate.SQL] (default task-2) insert into Employee (dept_id, firstName, lastName, pos, id) values (?, ?, ?, ?, ?)
19:51:55,720 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [1] as [BIGINT] - [1]
19:51:55,720 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [2] as [VARCHAR] - [Saburo]
19:51:55,720 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [3] as [VARCHAR] - [Tanaka]
19:51:55,721 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [4] as [INTEGER] - [null]
19:51:55,721 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [5] as [BIGINT] - [4]
19:51:55,721 DEBUG [org.hibernate.SQL] (default task-2) update Employee set pos=? where id=?
19:51:55,721 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [1] as [INTEGER] - [0]
19:51:55,722 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [2] as [BIGINT] - [2]
19:51:55,723 DEBUG [org.hibernate.SQL] (default task-2) update Employee set pos=? where id=?
19:51:55,723 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [1] as [INTEGER] - [1]
19:51:55,723 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [2] as [BIGINT] - [3]
19:51:55,724 DEBUG [org.hibernate.SQL] (default task-2) update Employee set pos=? where id=?
19:51:55,724 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [1] as [INTEGER] - [2]
19:51:55,724 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [2] as [BIGINT] - [4]

table

jpatest=# select * from dept ;
 id | deptname 
----+----------
  1 | Finance
(1 row)

jpatest=# select * from employee ;
 id | firstname | lastname | pos | dept_id 
----+-----------+----------+-----+---------
  2 | Taro      | Yamada   |   0 |       1
  3 | Jiro      | Suzuki   |   1 |       1
  4 | Saburo    | Tanaka   |   2 |       1
(3 rows)

jpatest=# 

Add an employee (didn't work correctly)

log

20:07:13,014 DEBUG [org.hibernate.SQL] (default task-8) select dept0_.id as id1_0_0_, dept0_.deptName as deptName2_0_0_ from Dept dept0_ where dept0_.id=?
20:07:13,015 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-8) binding parameter [1] as [BIGINT] - [1]
20:07:13,015 TRACE [org.hibernate.type.descriptor.sql.BasicExtractor] (default task-8) extracted value ([deptName2_0_0_] : [VARCHAR]) - [Finance]
20:07:13,016 TRACE [org.hibernate.type.CollectionType] (default task-8) Created collection wrapper: [org.nailedtothex.jpatest.ordercolumn.Dept.employees#1]
20:07:13,016 DEBUG [org.hibernate.SQL] (default task-8) select nextval ('hibernate_sequence')
20:07:13,022 DEBUG [org.hibernate.SQL] (default task-8) insert into Employee (dept_id, firstName, lastName, pos, id) values (?, ?, ?, ?, ?)
20:07:13,023 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-8) binding parameter [1] as [BIGINT] - [1]
20:07:13,023 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-8) binding parameter [2] as [VARCHAR] - [Jane]
20:07:13,023 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-8) binding parameter [3] as [VARCHAR] - [Doe]
20:07:13,023 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-8) binding parameter [4] as [INTEGER] - [null]
20:07:13,023 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-8) binding parameter [5] as [BIGINT] - [1]
20:07:13,024 DEBUG [org.hibernate.SQL] (default task-8) update Employee set pos=? where id=?
20:07:13,024 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-8) binding parameter [1] as [INTEGER] - [0]
20:07:13,024 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-8) binding parameter [2] as [BIGINT] - [1]

table

jpatest=# select * from dept;
 id  | deptname 
-----+----------
 101 | Finance
(1 row)

jpatest=# select * from employee ;
 id  | firstname | lastname | pos | dept_id 
-----+-----------+----------+-----+---------
 201 | Taro      | Yamada   |   0 |     101
 202 | Jiro      | Suzuki   |   1 |     101
 203 | Saburo    | Tanaka   |   2 |     101
   1 | Jane      | Doe      |   0 |     101
(4 rows)

jpatest=# 
  • Hibernate doesn't fetch max pos, consequently, pos of Jane Doe assigned pos 0 that duplicated.

Add an employee (revised)

  • To overcome the problem which above-mentioned, force Hibernate to fetch collections before addition of new entity through invoke size() method.

log

20:22:23,748 DEBUG [org.hibernate.SQL] (default task-6) select dept0_.id as id1_0_0_, dept0_.deptName as deptName2_0_0_ from Dept dept0_ where dept0_.id=?
20:22:23,748 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [1] as [BIGINT] - [101]
20:22:23,749 DEBUG [org.hibernate.SQL] (default task-6) select employees0_.dept_id as dept_id5_0_0_, employees0_.id as id1_1_0_, employees0_.pos as pos4_0_, employees0_.id as id1_1_1_, employees0_.dept_id as dept_id5_1_1_, employees0_.firstName as firstNam2_1_1_, employees0_.lastName as lastName3_1_1_, employees0_.pos as pos4_1_1_ from Employee employees0_ where employees0_.dept_id=?
20:22:23,750 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [1] as [BIGINT] - [101]
20:22:23,752 DEBUG [org.hibernate.SQL] (default task-6) select nextval ('hibernate_sequence')
20:22:23,760 DEBUG [org.hibernate.SQL] (default task-6) insert into Employee (dept_id, firstName, lastName, pos, id) values (?, ?, ?, ?, ?)
20:22:23,760 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [1] as [BIGINT] - [101]
20:22:23,760 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [2] as [VARCHAR] - [Jane]
20:22:23,760 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [3] as [VARCHAR] - [Doe]
20:22:23,761 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [4] as [INTEGER] - [null]
20:22:23,761 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [5] as [BIGINT] - [1]
20:22:23,762 DEBUG [org.hibernate.SQL] (default task-6) update Employee set pos=? where id=?
20:22:23,762 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [1] as [INTEGER] - [0]
20:22:23,762 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [2] as [BIGINT] - [201]
20:22:23,763 DEBUG [org.hibernate.SQL] (default task-6) update Employee set pos=? where id=?
20:22:23,763 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [1] as [INTEGER] - [1]
20:22:23,763 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [2] as [BIGINT] - [202]
20:22:23,763 DEBUG [org.hibernate.SQL] (default task-6) update Employee set pos=? where id=?
20:22:23,763 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [1] as [INTEGER] - [2]
20:22:23,763 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [2] as [BIGINT] - [203]
20:22:23,764 DEBUG [org.hibernate.SQL] (default task-6) update Employee set pos=? where id=?
20:22:23,764 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [1] as [INTEGER] - [3]
20:22:23,764 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [2] as [BIGINT] - [1]

table

jpatest=# select * from employee ;
 id  | firstname | lastname | pos | dept_id 
-----+-----------+----------+-----+---------
 201 | Taro      | Yamada   |   0 |     101
 202 | Jiro      | Suzuki   |   1 |     101
 203 | Saburo    | Tanaka   |   2 |     101
   1 | Jane      | Doe      |   3 |     101
(4 rows)

jpatest=# 
  • Hibernate recognized maximum pos and Jane Doe assigned correct pos.

Delete an employee

log

20:47:53,429 DEBUG [org.hibernate.SQL] (default task-14) select dept0_.id as id1_0_0_, dept0_.deptName as deptName2_0_0_ from Dept dept0_ where dept0_.id=?
20:47:53,429 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [1] as [BIGINT] - [101]
20:47:53,430 DEBUG [org.hibernate.SQL] (default task-14) select employees0_.dept_id as dept_id5_0_0_, employees0_.id as id1_1_0_, employees0_.pos as pos4_0_, employees0_.id as id1_1_1_, employees0_.dept_id as dept_id5_1_1_, employees0_.firstName as firstNam2_1_1_, employees0_.lastName as lastName3_1_1_, employees0_.pos as pos4_1_1_ from Employee employees0_ where employees0_.dept_id=?
20:47:53,430 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [1] as [BIGINT] - [101]
20:47:53,435 DEBUG [org.hibernate.SQL] (default task-14) update Employee set pos=? where id=?
20:47:53,436 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [1] as [INTEGER] - [0]
20:47:53,436 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [2] as [BIGINT] - [201]
20:47:53,436 DEBUG [org.hibernate.SQL] (default task-14) update Employee set pos=? where id=?
20:47:53,436 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [1] as [INTEGER] - [1]
20:47:53,436 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [2] as [BIGINT] - [203]
20:47:53,439 DEBUG [org.hibernate.SQL] (default task-14) delete from Employee where id=?
20:47:53,439 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [1] as [BIGINT] - [202]

table

jpatest=# select * from dept ;
 id  | deptname 
-----+----------
 101 | Finance
(1 row)

jpatest=# select * from employee ;
 id  | firstname | lastname | pos | dept_id 
-----+-----------+----------+-----+---------
 201 | Taro      | Yamada   |   0 |     101
 203 | Saburo    | Tanaka   |   1 |     101
(2 rows)

jpatest=#

Swap order

log

21:02:05,979 DEBUG [org.hibernate.SQL] (default task-1) select dept0_.id as id1_0_0_, dept0_.deptName as deptName2_0_0_ from Dept dept0_ where dept0_.id=?
21:02:05,980 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-1) binding parameter [1] as [BIGINT] - [101]
21:02:05,981 DEBUG [org.hibernate.SQL] (default task-1) select employees0_.dept_id as dept_id5_0_0_, employees0_.id as id1_1_0_, employees0_.pos as pos4_0_, employees0_.id as id1_1_1_, employees0_.dept_id as dept_id5_1_1_, employees0_.firstName as firstNam2_1_1_, employees0_.lastName as lastName3_1_1_, employees0_.pos as pos4_1_1_ from Employee employees0_ where employees0_.dept_id=?
21:02:05,981 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-1) binding parameter [1] as [BIGINT] - [101]
21:02:05,983 FINE  [org.nailedtothex.jpatest.ordercolumn.OrderColumnTestDataManipulator] (default task-1) [Employee [id=201, firstName=Taro, lastName=Yamada, pos=0, dept=101], Employee [id=202, firstName=Jiro, lastName=Suzuki, pos=1, dept=101], Employee [id=203, firstName=Saburo, lastName=Tanaka, pos=2, dept=101]]
21:02:05,983 FINE  [org.nailedtothex.jpatest.ordercolumn.OrderColumnTestDataManipulator] (default task-1) [Employee [id=201, firstName=Taro, lastName=Yamada, pos=0, dept=101], Employee [id=203, firstName=Saburo, lastName=Tanaka, pos=2, dept=101], Employee [id=203, firstName=Saburo, lastName=Tanaka, pos=2, dept=101]]
21:02:05,984 FINE  [org.nailedtothex.jpatest.ordercolumn.OrderColumnTestDataManipulator] (default task-1) [Employee [id=201, firstName=Taro, lastName=Yamada, pos=0, dept=101], Employee [id=203, firstName=Saburo, lastName=Tanaka, pos=2, dept=101], Employee [id=202, firstName=Jiro, lastName=Suzuki, pos=1, dept=101]]
21:02:05,987 DEBUG [org.hibernate.SQL] (default task-1) update Employee set pos=? where id=?
21:02:05,987 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-1) binding parameter [1] as [INTEGER] - [0]
21:02:05,987 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-1) binding parameter [2] as [BIGINT] - [201]
21:02:05,988 DEBUG [org.hibernate.SQL] (default task-1) update Employee set pos=? where id=?
21:02:05,988 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-1) binding parameter [1] as [INTEGER] - [1]
21:02:05,988 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-1) binding parameter [2] as [BIGINT] - [203]
21:02:05,989 DEBUG [org.hibernate.SQL] (default task-1) update Employee set pos=? where id=?
21:02:05,989 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-1) binding parameter [1] as [INTEGER] - [2]
21:02:05,989 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-1) binding parameter [2] as [BIGINT] - [202]

table

jpatest=# select * from dept ;
 id  | deptname 
-----+----------
 101 | Finance
(1 row)

jpatest=# select * from employee ;
 id  | firstname | lastname | pos | dept_id 
-----+-----------+----------+-----+---------
 201 | Taro      | Yamada   |   0 |     101
 203 | Saburo    | Tanaka   |   1 |     101
 202 | Jiro      | Suzuki   |   2 |     101
(3 rows)

jpatest=# 

Retrieve from unordered table

log

21:13:55,544 DEBUG [org.hibernate.SQL] (default task-4) select dept0_.id as id1_0_0_, dept0_.deptName as deptName2_0_0_ from Dept dept0_ where dept0_.id=?
21:13:55,545 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-4) binding parameter [1] as [BIGINT] - [101]
21:13:55,546 DEBUG [org.hibernate.SQL] (default task-4) select employees0_.dept_id as dept_id5_0_0_, employees0_.id as id1_1_0_, employees0_.pos as pos4_0_, employees0_.id as id1_1_1_, employees0_.dept_id as dept_id5_1_1_, employees0_.firstName as firstNam2_1_1_, employees0_.lastName as lastName3_1_1_, employees0_.pos as pos4_1_1_ from Employee employees0_ where employees0_.dept_id=?
21:13:55,546 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-4) binding parameter [1] as [BIGINT] - [101]
21:13:55,548 FINE  [org.nailedtothex.jpatest.ordercolumn.OrderColumnTestDataManipulator] (default task-4) [Employee [id=201, firstName=Taro, lastName=Yamada, pos=0, dept=101], Employee [id=202, firstName=Jiro, lastName=Suzuki, pos=1, dept=101], Employee [id=203, firstName=Saburo, lastName=Tanaka, pos=2, dept=101]]

table

jpatest=# select employees0_.dept_id as dept_id5_0_0_, employees0_.id as id1_1_0_, employees0_.pos as pos4_0_, employees0_.id as id1_1_1_, employees0_.dept_id as dept_id5_1_1_, employees0_.firstName as firstNam2_1_1_, employees0_.lastName as lastName3_1_1_, employees0_.pos as pos4_1_1_ from Employee employees0_ where employees0_.dept_id=101;
 dept_id5_0_0_ | id1_1_0_ | pos4_0_ | id1_1_1_ | dept_id5_1_1_ | firstnam2_1_1_ | lastname3_1_1_ | pos4_1_1_ 
---------------+----------+---------+----------+---------------+----------------+----------------+-----------
           101 |      203 |       2 |      203 |           101 | Saburo         | Tanaka         |         2
           101 |      201 |       0 |      201 |           101 | Taro           | Yamada         |         0
           101 |      202 |       1 |      202 |           101 | Jiro           | Suzuki         |         1
(3 rows)

jpatest=# 
  • Hibernate executed a query that not contains ORDER BY, and returned unordered result set, but I got correctly ordered list (did Java sorted it?).

Reverse the order

log

22:23:11,950 DEBUG [org.hibernate.SQL] (default task-9) select dept0_.id as id1_0_0_, dept0_.deptName as deptName2_0_0_ from Dept dept0_ where dept0_.id=?
22:23:11,950 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-9) binding parameter [1] as [BIGINT] - [101]
22:23:11,951 DEBUG [org.hibernate.SQL] (default task-9) select employees0_.dept_id as dept_id5_0_0_, employees0_.id as id1_1_0_, employees0_.pos as pos4_0_, employees0_.id as id1_1_1_, employees0_.dept_id as dept_id5_1_1_, employees0_.firstName as firstNam2_1_1_, employees0_.lastName as lastName3_1_1_, employees0_.pos as pos4_1_1_ from Employee employees0_ where employees0_.dept_id=?
22:23:11,951 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-9) binding parameter [1] as [BIGINT] - [101]
22:23:11,956 DEBUG [org.hibernate.SQL] (default task-9) update Employee set pos=? where id=?
22:23:11,957 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-9) binding parameter [1] as [INTEGER] - [0]
22:23:11,957 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-9) binding parameter [2] as [BIGINT] - [203]
22:23:11,957 DEBUG [org.hibernate.SQL] (default task-9) update Employee set pos=? where id=?
22:23:11,957 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-9) binding parameter [1] as [INTEGER] - [1]
22:23:11,957 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-9) binding parameter [2] as [BIGINT] - [202]
22:23:11,958 DEBUG [org.hibernate.SQL] (default task-9) update Employee set pos=? where id=?
22:23:11,958 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-9) binding parameter [1] as [INTEGER] - [2]
22:23:11,958 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-9) binding parameter [2] as [BIGINT] - [201]

table

jpatest=# select * from employee ;
 id  | firstname | lastname | pos | dept_id 
-----+-----------+----------+-----+---------
 203 | Saburo    | Tanaka   |   0 |     101
 202 | Jiro      | Suzuki   |   1 |     101
 201 | Taro      | Yamada   |   2 |     101
(3 rows)

jpatest=# 

Conclusion

Pros

  • It brings powerful mechanism that developer can reduce some annoying codes to maintain order of the list.

Cons

  • It executes bunch of inefficient queries.
  • Through considering above queries, maybe use of unique index of order column is impossible.

Remarks

  • Not bad for such as low-traffic internal systems.
  • To OrderColumn work correctly, we have to keep all elements fetched when modify the list.

References

  1. [HHH-5732] @OrderColumn not updated if @OneToMany has mappedby defined - Hibernate JIRA
  2. JPA 2.0 @OrderColumn annotation in Hibernate 3.5 - Stack Overflow
  3. JPA Best Practices