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


Arquillian用ArchiveへクラスとMaven Dependencyを楽に追加する


Posted on Tuesday Jan 28, 2014 at 09:48PM in Technology


環境

  • shrinkwrap-resolver 2.1.0-alpha-1
  • wildfly-arquillian-container-remote 8.0.0.CR1
  • Arquillian 1.1.2.Final
  • WildFly 8.0.0.CR1
  • Eclipse Kepler SR1
  • Oracle JDK7u51

サブパッケージを再帰的に探索させてクラスを追加する

例えばこういう状況で

テストするのに必要なクラス群が複数のパッケージに分散している

クラスの配置

Hige.java

package org.example.hige;

import javax.inject.Named;

@Named
public class Hige {

    public String hige() {
        return "hige()";
    }
}

Hoge.java

package org.example.hoge;

import javax.inject.Inject;
import javax.inject.Named;

import org.example.hige.Hige;

@Named
public class Hoge {

    @Inject
    Hige hige;

    public String hoge() {
        return "hoge()" + hige.hige();
    }
}

こうするとよい

Archive作成時にaddPackages()メソッドをこういう感じで呼んでやればよい。これでいちいち全パッケージに対してaddPackage()呼んでやらずにすむ。第一引数がtrueだと、第二引数で与えたパッケージのサブパッケージを再帰的に探索してくれる。

テストクラス

package org.example.hoge;

import javax.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class HogeTest {

    @Deployment
    public static Archive<?> createDeployment() {
        WebArchive a =
                ShrinkWrap.create(WebArchive.class, "test.war")
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
                .addPackages(true, "org.example");
        return a;
    }

    @Inject
    Hoge hoge;

    @Test
    public void test() throws Exception {
        Assert.assertEquals("hoge()hige()", hoge.hoge());
    }
}

Maven Dependencyを楽に追加する

例えばこういう状況で

guavaをpom.xmlに設定して使っている。Archiveを作る際に、いちいち依存するjarファイルをaddLibraryしていくのが面倒くさい。

Hoge.java

package org.example.hoge;

import javax.inject.Named;

import com.google.common.collect.Lists;

@Named
public class Hoge {

    public void hoge() {
        Lists.newArrayList();
    }
}

こうするとよい

pom.xml

shrinkwrap-resolverへの依存を追加する(groupIdがorg.jboss.shrinkwrap.resolverの要素)。例えばこんな感じ

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>arquillian-prac</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.jboss.shrinkwrap.resolver</groupId>
                <artifactId>shrinkwrap-resolver-bom</artifactId>
                <version>2.1.0-alpha-1</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
            <dependency>
                <groupId>org.jboss.arquillian</groupId>
                <artifactId>arquillian-bom</artifactId>
                <version>1.1.2.Final</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12</version>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-integration</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>net.avh4.util</groupId>
            <artifactId>imagecomparison</artifactId>
            <version>0.0.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.junit</groupId>
            <artifactId>arquillian-junit-container</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.wildfly</groupId>
            <artifactId>wildfly-arquillian-container-remote</artifactId>
            <version>8.0.0.CR1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.extension</groupId>
            <artifactId>arquillian-persistence-impl</artifactId>
            <version>1.0.0.Alpha6</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.shrinkwrap.resolver</groupId>
            <artifactId>shrinkwrap-resolver-impl-maven</artifactId>
            <version>2.1.0-alpha-1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.shrinkwrap.resolver</groupId>
            <artifactId>shrinkwrap-resolver-api-maven</artifactId>
            <version>2.1.0-alpha-1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>16.0</version>
        </dependency>
    </dependencies>
</project>

テストクラス

package org.example.hoge;

import javax.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class HogeTest {

    @Deployment
    public static Archive<?> createDeployment() {
        WebArchive a =
                ShrinkWrap.create(WebArchive.class, "test.war")
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
                .addPackages(true, "org.example");

        a.addAsLibraries(
                Maven.resolver()
                .loadPomFromFile("pom.xml")
                .importRuntimeDependencies()
                .resolve()
                .withTransitivity()
                .asFile());

        return a;
    }

    @Inject
    Hoge hoge;

    @Test
    public void test() throws Exception {
        hoge.hoge();
        Assert.assertTrue(true);
    }
}

importRuntimeDependencies()の他にもいろいろあるので必要に応じて使い分ける。ただ多くなりすぎるとデプロイが遅くなる。

  • importRuntimeAndCompileDependencies()
  • importDependencies(ScopeType…)
  • importRuntimeAndTestDependencies()
  • importRuntimeDependencies()
  • importTestDependencies()

こういう感じにすると依存性を個別に追加できる[3]

        File[] files = Maven.resolver()
                .loadPomFromFile("pom.xml")
                .resolve("org.apache.commons:commons-lang3:3.2.1")
                .withTransitivity()
                .asFile();

        WebArchive w = ShrinkWrap.create(WebArchive.class, "hoge.war").addClass(HogeBean.class)
                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml").addAsLibraries(files);

バージョンの省略も可。省略するとpom.xmlに書いてあるバージョンを取ってきてくれる

        File[] files = Maven.resolver()
                .loadPomFromFile("pom.xml")
                .resolve("org.apache.commons:commons-lang3")
                .withTransitivity()
                .asFile();

参考文献

  1. Add all classes in src/main/java | Community
  2. java - Adding all Maven dependencies to Arquillian - Stack Overflow
  3. shrinkwrap/resolver · GitHub
  4. Using the ShrinkWrap Maven Resolver for Arquillian Tests | Develop In Java
  5. arquillian 1.1.0 final and shrinkwrap | Community
  6. Add directory recursively to arquillian using S… | Community
  7. Add src/main/java to webarchive | Community


Arquillian Persistence ExtensionでExcelデータを使ってみる


Posted on Tuesday Jan 28, 2014 at 04:21PM in Technology


Excelで作ったデータを投入したり検証に使ったりしてみます

環境

  • Arquillian Persistence Extension 1.0.0.Alpha6
  • wildfly-arquillian-container-remote 8.0.0.CR1
  • Arquillian 1.1.2.Final
  • Hibernate 4.3.0.Final
  • WildFly 8.0.0.CR1
  • Eclipse Kepler SR1
  • Oracle JDK7u51
  • postgresql-9.3-1100.jdbc41.jar
  • PostgreSQL 9.2.4
  • Excel 2011

前提条件

準備

資源の配置図

下図で選択した資源を作成または編集します

VariousTypes.java

package org.arquillian.example;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
public class VariousTypes implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long bigint1;
    @Column
    private String varchar1;
    @Column
    @Temporal(TemporalType.DATE)
    private Date date1;
    @Column
    @Temporal(TemporalType.TIME)
    private Date time1;
    @Column
    @Temporal(TemporalType.TIMESTAMP)
    private Date timestamp1;
    @Column(precision = 7, scale = 4)
    private BigDecimal numeric1;
    @Column
    private Double double1;

    public Long getBigint1() {
        return bigint1;
    }

    public void setBigint1(Long bigint1) {
        this.bigint1 = bigint1;
    }

    public String getVarchar1() {
        return varchar1;
    }

    public void setVarchar1(String varchar1) {
        this.varchar1 = varchar1;
    }

    public Date getDate1() {
        return date1;
    }

    public void setDate1(Date date1) {
        this.date1 = date1;
    }

    public Date getTime1() {
        return time1;
    }

    public void setTime1(Date time1) {
        this.time1 = time1;
    }

    public Date getTimestamp1() {
        return timestamp1;
    }

    public void setTimestamp1(Date timestamp1) {
        this.timestamp1 = timestamp1;
    }

    public BigDecimal getNumeric1() {
        return numeric1;
    }

    public void setNumeric1(BigDecimal numeric1) {
        this.numeric1 = numeric1;
    }

    public Double getDouble1() {
        return double1;
    }

    public void setDouble1(Double double1) {
        this.double1 = double1;
    }

    @Override
    public String toString() {
        return "VariousTypes [bigint1=" + bigint1 + ", varchar1=" + varchar1 + ", date1=" + date1 + ", time1=" + time1
                + ", timestamp1=" + timestamp1 + ", numeric1=" + numeric1 + ", double1=" + double1 + "]";
    }

}

orm.xml

以下のようなクエリを追加します

    <named-query name="findVariousTypes">
        <query><![CDATA[
            SELECT
                v
            FROM
                VariousTypes v
        ]]></query>
    </named-query>

XlsDataSetTest.java

package org.arquillian.example;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.persistence.ShouldMatchDataSet;
import org.jboss.arquillian.persistence.UsingDataSet;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;


@RunWith(Arquillian.class)
public class XlsDataSetTest {
   @Deployment
   public static Archive<?> createDeployment() {
       Archive<?> a = ShrinkWrap.create(WebArchive.class, "test.war")
           .addPackage(VariousTypes.class.getPackage())
           .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
           .addAsResource("META-INF/orm.xml", "META-INF/orm.xml")
           .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
           .addAsWebInfResource("jbossas-ds.xml");
       return a;
   }

   @PersistenceContext
   EntityManager em;

   @Test
   @Transactional
   @UsingDataSet("datasets/xlsDataSet/variousTypes.xls")
   @ShouldMatchDataSet(value = "datasets/xlsDataSet/variousTypesExpected.xls", orderBy = "bigint1")
   public void select() throws Exception {
       dumpEntityList(em.createNamedQuery("findVariousTypes", VariousTypes.class).getResultList());

       Calendar cal = Calendar.getInstance();
       cal.set(2014, 0, 2, 12, 13, 14);
       cal.set(Calendar.MILLISECOND, 0);
       Date newDate = cal.getTime();

       VariousTypes toModify = em.find(VariousTypes.class, 1l);
       toModify.setVarchar1("HogeHoge");
       toModify.setDate1(newDate);
       toModify.setTime1(newDate);
       toModify.setTimestamp1(newDate);
       toModify.setNumeric1(new BigDecimal("222.2222"));
       toModify.setDouble1(1.0);

       dumpEntityList(em.createNamedQuery("findVariousTypes", VariousTypes.class).getResultList());
   }

   protected void dumpEntityList(List<?> list){
       for(Object o : list){
           System.out.println(o + ", contains=" + em.contains(o));
       }
   }
}

variousTypes.xls

今回はMacのExcel2011で作りましたが、.xls形式(.xlsx形式でなく)で出力していれば、LibreOfficeやOpenOfficeで作ってもおそらく問題ありません。日付や時刻は文字列型で入れます。普通に入力するとシリアル値になって正常に認識してくれなくなります。こんな感じ。

blobは少し調べた範囲ではExcel形式では対応してなさそうなのでスルーします。XML形式なら対応しているそうですが試してません[2]

variousTypesExpected.xls

こんな感じ。IDが1のデータをテストクラス内で更新してその結果を検証してみます

テスト実行

投入データの確認

デバッガで45行目で止めて投入データを見てみます

テーブル

jpaprac=# \d varioustypes
             Table "public.varioustypes"
   Column   |            Type             | Modifiers 
------------+-----------------------------+-----------
 bigint1    | bigint                      | not null
 date1      | date                        | 
 double1    | double precision            | 
 numeric1   | numeric(7,4)                | 
 time1      | time without time zone      | 
 timestamp1 | timestamp without time zone | 
 varchar1   | character varying(255)      | 
Indexes:
    "varioustypes_pkey" PRIMARY KEY, btree (bigint1)

jpaprac=# 

データ

jpaprac=# select bigint1, varchar1, date1, time1, timestamp1, numeric1, double1 from varioustypes ;
 bigint1 | varchar1 |   date1    |  time1   |     timestamp1      | numeric1 | double1 
---------+----------+------------+----------+---------------------+----------+---------
       1 | Hoge     | 1980-01-01 | 12:00:00 | 1980-01-01 12:00:00 | 100.0001 |     0.5
       2 | Hige     | 1980-01-02 | 12:00:01 | 1980-01-02 12:00:01 | 100.0020 |    0.25
       3 | Fuge     | 1980-01-03 | 12:00:02 | 1980-01-03 12:00:02 | 100.0300 |   0.125
(3 rows)

jpaprac=# 

ちゃんと入ってますね

実行結果

JUnit窓

コンソール出力

18:21:16,667 INFO  [stdout] (pool-2-thread-20) Hibernate: select varioustyp0_.bigint1 as bigint1_2_, varioustyp0_.date1 as date2_2_, varioustyp0_.double1 as double3_2_, varioustyp0_.numeric1 as numeric4_2_, varioustyp0_.time1 as time5_2_, varioustyp0_.timestamp1 as timestam6_2_, varioustyp0_.varchar1 as varchar7_2_ from VariousTypes varioustyp0_
18:21:16,670 INFO  [stdout] (pool-2-thread-20) VariousTypes [bigint1=1, varchar1=Hoge, date1=1980-01-01, time1=12:00:00, timestamp1=1980-01-01 12:00:00.0, numeric1=100.0001, double1=0.5], contains=true
18:21:16,670 INFO  [stdout] (pool-2-thread-20) VariousTypes [bigint1=2, varchar1=Hige, date1=1980-01-02, time1=12:00:01, timestamp1=1980-01-02 12:00:01.0, numeric1=100.0020, double1=0.25], contains=true
18:21:16,671 INFO  [stdout] (pool-2-thread-20) VariousTypes [bigint1=3, varchar1=Fuge, date1=1980-01-03, time1=12:00:02, timestamp1=1980-01-03 12:00:02.0, numeric1=100.0300, double1=0.125], contains=true
18:21:16,676 INFO  [stdout] (pool-2-thread-20) Hibernate: update VariousTypes set date1=?, double1=?, numeric1=?, time1=?, timestamp1=?, varchar1=? where bigint1=?
18:21:16,677 INFO  [stdout] (pool-2-thread-20) Hibernate: select varioustyp0_.bigint1 as bigint1_2_, varioustyp0_.date1 as date2_2_, varioustyp0_.double1 as double3_2_, varioustyp0_.numeric1 as numeric4_2_, varioustyp0_.time1 as time5_2_, varioustyp0_.timestamp1 as timestam6_2_, varioustyp0_.varchar1 as varchar7_2_ from VariousTypes varioustyp0_
18:21:16,678 INFO  [stdout] (pool-2-thread-20) VariousTypes [bigint1=2, varchar1=Hige, date1=1980-01-02, time1=12:00:01, timestamp1=1980-01-02 12:00:01.0, numeric1=100.0020, double1=0.25], contains=true
18:21:16,678 INFO  [stdout] (pool-2-thread-20) VariousTypes [bigint1=3, varchar1=Fuge, date1=1980-01-03, time1=12:00:02, timestamp1=1980-01-03 12:00:02.0, numeric1=100.0300, double1=0.125], contains=true
18:21:16,681 INFO  [stdout] (pool-2-thread-20) VariousTypes [bigint1=1, varchar1=HogeHoge, date1=Thu Jan 02 12:13:14 JST 2014, time1=Thu Jan 02 12:13:14 JST 2014, timestamp1=Thu Jan 02 12:13:14 JST 2014, numeric1=222.2222, double1=1.0], contains=true

更新後のDBも覗いてみたいところですが、コンソールにJPQLから覗いた結果が出ているのでそれでよしとします

備考

  • 浮動小数点の検証が気になる。丸め誤差とか。最悪テスト実行後に走らせるSQL文にUPDATEかまして端数を切り捨てるとかすれば出来そうだけど気持ち悪いなあ
  • xlsのテストデータ修正後はEclipseでRefreshかけないとなぜか更新後のデータを読んでくれない

参考文献

  1. Java/DBUnit/ExcelシートでImport,Exportする - きのさいと
  2. 6. 便利な機能 | TECHSCORE(テックスコア)


ローカルで一人用Gitを使ってみる


Posted on Monday Jan 27, 2014 at 09:45AM in Technology


Eclipseでローカルで一人用Gitを使う方法。初期の操作と日常的な操作。

環境

  • Eclipse Kelper SR1
  • OS X 10.9.1

JBoss Toolsも入れたせいかもしれないが、どうやら最初からGitプラグイン的なものが入っているようだ。

初回の操作

プロジェクトをGitリポジトリに登録する

たぶん微妙な表現だと思うけど。すでに登録したいプロジェクトが存在するものとする。

  1. プロジェクトを右クリックしてTeam→Share Project

  2. Gitを選んでNext

  3. Createを押す

  4. 適当な場所と名前を入れてFinish

  5. 入力内容を確認してFinish

  6. 結果プロジェクトのアイコンがこんな表示になる

初回コミット

  1. プロジェクトを右クリックしてTeam→Commit

  2. 名前とメールアドレスを入れてOK。Githubとか使うと公開されるのかなこれ。とりあえず迷惑メール来ても良さそうなのを入れとく

  3. コミットメッセージと対象資源を選択してCommit。右上のチェックボックスのボタンをクリックすると全ての資源にチェックが入る。何か除外した方が良さそうなものとかも混じってそうな気もするがとりあえずこれで。

  4. 結果プロジェクトのアイコンがこんな表示になる。何も資源が入ってないフォルダは?のままだけどこれでいいのかな。

日常的な操作

編集〜差分確認〜コミット

  1. 資源を編集して保存するとアイコンがこうなる。「>」 が付く

  2. 差分を確認してみる。プロジェクトを右クリックしてCompare With→HEAD Revisionを選択

  3. Git Tree Compare窓に編集された資源が表示されるのでHigeServlet.javaをダブルクリック

  4. 差分が表示される

  5. 差分を確認したら初回と同じ要領でコミットすると、「>」が消える

ローカルの編集内容を破棄してHEADに戻す

  1. 編集した資源を右クリックしてReplace With→HEAD Revisionを選択

  2. OK

履歴を見る

  1. プロジェクトを右クリックしてTeam→Show in Historyを選択

  2. こんな感じに表示される。詳細な情報はこの画面から辿れるようだ

その他

  • Macの場合は.DS_Storeをgitに無視させる設定を入れておくとよい[3]。
  • Eclipseの設定ファイル群を外したい場合は[4].

参考文献

  1. Eclipse + maven + bitbucket でJava開発 - Level.TRACE
  2. Git - Book
  3. グローバルな.gitignore を設定して、すべてのgitリポジトリで無視するファイルを設定する|WEBデザイン Tips
  4. Eclipse で Android アプリを作る時の .gitignore | SLUMBERS


Entity Graphを使ってみる


Posted on Sunday Jan 26, 2014 at 04:05PM in Technology


JPA2.1の新機能Entity Graphを使ってみます。SELECT文を実行するときにどこまで階層を掘り下げて拾ってくるかを指定することができるようになり、むだにManaged状態のときにsize()を呼び出したりしなくてよくなる仕組みのようです。

環境

  • Hibernate 4.3.0.Final
  • WildFly8.0.0.CR1
  • Oracle JDK7u51
  • postgresql-9.3-1100.jdbc41.jar
  • PostgreSQL 9.2.4

使い方

  1. エンティティの階層を表すEntity Graphを作ります。JPQLのような名前付きの静的なものとして作っておく他に、動的にコードから作ることも可能。Canonical MetaModelも使えるようです。
  2. Query#setHint()メソッドを使ってEntity Graphを渡してやってからクエリを実行します。ヒント名には2つあり、どちらを使うかで以下のように動作が変わります
    • javax.persistence.fetchgraph: Entity Graphに含まれるフィールドのみロードされる。含まれないフィールドはロードされない。Entity Graphに含まれないフィールドは全てLAZY扱いになる
    • javax.persistence.loadgraph: Entity Graphに含まれるフィールドはLAZYであってもロードされる。含まれないフィールドはそれぞれのフィールドの設定による

前提条件

NativeQueryで関連持ちエンティティをSELECTしてみるで使った資源を使います。エンティティはこれです

試してみる

javax.persistence.fetchgraph

Entity Graphを作る

Employeeクラスにこんなアノテーションを付けます

@NamedEntityGraph(
        name = "onlyFirstNameAndLastName",
        attributeNodes = {
                @NamedAttributeNode("firstName"),
                @NamedAttributeNode("lastName")})

orm.xmlにクエリを定義

    <named-query name="findEmployees">
        <query><![CDATA[
            SELECT
                e
            FROM
                Employee e
        ]]></query>
    </named-query>

テストメソッド

    @Test
    @Transactional
    @UsingDataSet({"datasets/relativeSelect/dept.yml", "datasets/relativeSelect/employees.yml"})
    public void select() throws Exception {
        EntityGraph<?> entityGraph = em.getEntityGraph("onlyFirstNameAndLastName");
        dumpEmployeeList(em.createNamedQuery("findEmployees", Employee.class)
            .setHint("javax.persistence.fetchgraph", entityGraph)
            .getResultList());
    }

    protected void dumpEmployeeList(List<Employee> employees){
        for(Employee emp : employees){
            System.out.printf("        emp: id=%d, firstName=%s, lastName=%s\n", emp.getId(), emp.getFirstName(), emp.getLastName());               
        }
    }

テストメソッドを実行

javax.persistence.fetchgraphを指定して、Entity GraphではfirstNameとlastNameのみ指定しているので、deptはロードされないと思っていたら…

17:27:12,991 INFO  [stdout] (pool-2-thread-34) Hibernate: select employee0_.id as id1_1_, employee0_.dept_id as dept_id4_1_, employee0_.firstName as firstNam2_1_, employee0_.lastName as lastName3_1_ from Employee employee0_
17:27:12,993 INFO  [stdout] (pool-2-thread-34) Hibernate: select dept0_.id as id1_0_0_, dept0_.deptName as deptName2_0_0_ from Dept dept0_ where dept0_.id=?
17:27:12,994 INFO  [stdout] (pool-2-thread-34) Hibernate: select dept0_.id as id1_0_0_, dept0_.deptName as deptName2_0_0_ from Dept dept0_ where dept0_.id=?
17:27:12,995 INFO  [stdout] (pool-2-thread-34)         emp: id=-1, firstName=Taro, lastName=Yamada
17:27:12,996 INFO  [stdout] (pool-2-thread-34)         emp: id=-2, firstName=Jiro, lastName=Suzuki
17:27:12,996 INFO  [stdout] (pool-2-thread-34)         emp: id=-3, firstName=Saburo, lastName=Tanaka

何故か普通に取ってきてますね。どうも無視されているような気が…。[2]にはエンティティグラフについても言及されているようなのだが未実装という事だろうか。あるいは、この機能はあくまでヒントであって実際にどうなるかはJPAプロバイダの実装によるとかそういう事なのか。

[1]を書いた人のサンプルを見るとEclipseLinkを使っているようなのでEclipseLinkなら期待通りの動作をするかもしれない。

[4]に起票されているが、どうもJPA仕様では強制されてないからバグではないと認識されているようだ。ううむ。

javax.persistence.loadgraph

Entity Graphを作る

Deptクラスにこんなアノテーションを付けます

@NamedEntityGraph(
name = "loadEmployees",
attributeNodes = {
        @NamedAttributeNode("employees")}
)

orm.xmlにクエリを定義

    <named-query name="findDeptsDistinct">
        <query><![CDATA[
            SELECT DISTINCT
                d
            FROM
                Dept AS d
        ]]></query>
    </named-query>

テストメソッド

    @Test
    @Transactional
    @UsingDataSet({"datasets/relativeSelect/dept.yml", "datasets/relativeSelect/employees.yml"})
    public void select() throws Exception {
        EntityGraph<?> entityGraph = em.getEntityGraph("loadEmployees");
        dumpDeptList(em.createNamedQuery("findDeptsDistinct", Dept.class)
            .setHint("javax.persistence.loadgraph", entityGraph)
            .getResultList());
    }

    protected void dumpEmployeeList(List<Employee> employees){
        for(Employee emp : employees){
            System.out.printf("        emp: id=%d, firstName=%s, lastName=%s\n", emp.getId(), emp.getFirstName(), emp.getLastName());               
        }
    }

    protected void dumpDeptList(List<Dept> list){
        for(Dept dept : list){
            System.out.printf("dept: id=%d, deptName=%s\n", dept.getId(), dept.getDeptName());
            dumpEmployeeList(dept.getEmployees());
        }
    }

テストメソッドを実行

21:03:35,540 INFO  [stdout] (pool-2-thread-4) Hibernate: select distinct dept0_.id as id1_0_0_, employees1_.id as id1_1_1_, dept0_.deptName as deptName2_0_0_, employees1_.dept_id as dept_id4_1_1_, employees1_.firstName as firstNam2_1_1_, employees1_.lastName as lastName3_1_1_, employees1_.dept_id as dept_id4_0_0__, employees1_.id as id1_1_0__ from Dept dept0_ left outer join Employee employees1_ on dept0_.id=employees1_.dept_id
21:03:35,544 INFO  [stdout] (pool-2-thread-4) dept: id=-1, deptName=Sales
21:03:35,545 INFO  [stdout] (pool-2-thread-4)         emp: id=-2, firstName=Jiro, lastName=Suzuki
21:03:35,545 INFO  [stdout] (pool-2-thread-4)         emp: id=-1, firstName=Taro, lastName=Yamada
21:03:35,545 INFO  [stdout] (pool-2-thread-4) dept: id=-2, deptName=Legal
21:03:35,545 INFO  [stdout] (pool-2-thread-4)         emp: id=-3, firstName=Saburo, lastName=Tanaka

めでたくSELECT文が一回になりました。こっちは大丈夫そう。

その他

Entity Graphをorm.xmlで定義しようとすると例外が起きてしまい動作しません。「named-attribute-node.value is mandatory in XML overriding.」と言われます。書き方が悪いのか、あるいは他の原因があるのかは不明。またそのうち調べる。

参考文献

  1. Forward Everyday: JPA 2.1: Entity Graph
  2. ORMツール Hibernare 4.3がリリース,JPA 2.1仕様を実装
  3. FETCH JOIN is still a JOIN « Piotr Nowicki's Homepage
  4. [HHH-8776] Ability for JPA entity-graphs to handle non-lazy attributes as lazy - Hibernate JIRA