@Factoryが必要な理由

bookingサンプルではホテルの検索結果を@Factoryでコンテキストに一度だけバインドしてViewではその値を表示しています。これを修正して@Factoryを使わずにgetterメソッド(getHotels)で直接DB検索をするように修正すると、次のように1回のレンダリングの途中で何度もgetHotels()の呼び出しが発生しています。これを見ると@Factoryがいかに有効であるかがわかります。

View (main.xhtml)


コンソールログ

22:12:53,628 INFO  [STDOUT] ***************** getHotels called
22:12:53,634 INFO  [STDOUT] Hibernate: select top ? hotel0_.id as id13_, hotel0_
.address as address13_, hotel0_.city as city13_, hotel0_.country as country13_,
hotel0_.name as name13_, hotel0_.price as price13_, hotel0_.state as state13_, h
otel0_.zip as zip13_ from Hotel hotel0_ where lower(hotel0_.name) like ? or lowe
r(hotel0_.city) like ? or lower(hotel0_.zip) like ? or lower(hotel0_.address) li
ke ?
22:12:53,727 INFO  [STDOUT] ***************** getHotels called
22:12:53,734 INFO  [STDOUT] Hibernate: select top ? hotel0_.id as id13_, hotel0_
.address as address13_, hotel0_.city as city13_, hotel0_.country as country13_,
hotel0_.name as name13_, hotel0_.price as price13_, hotel0_.state as state13_, h
otel0_.zip as zip13_ from Hotel hotel0_ where lower(hotel0_.name) like ? or lowe
r(hotel0_.city) like ? or lower(hotel0_.zip) like ? or lower(hotel0_.address) li
ke ?
22:12:53,806 INFO  [STDOUT] ***************** getHotels called
22:12:53,809 INFO  [STDOUT] Hibernate: select top ? hotel0_.id as id13_, hotel0_
.address as address13_, hotel0_.city as city13_, hotel0_.country as country13_,
hotel0_.name as name13_, hotel0_.price as price13_, hotel0_.state as state13_, h
otel0_.zip as zip13_ from Hotel hotel0_ where lower(hotel0_.name) like ? or lowe
r(hotel0_.city) like ? or lower(hotel0_.zip) like ? or lower(hotel0_.address) li
ke ?
22:12:53,829 INFO  [STDOUT] ***************** getHotels called
22:12:53,834 INFO  [STDOUT] Hibernate: select top ? hotel0_.id as id13_, hotel0_
.address as address13_, hotel0_.city as city13_, hotel0_.country as country13_,
hotel0_.name as name13_, hotel0_.price as price13_, hotel0_.state as state13_, h
otel0_.zip as zip13_ from Hotel hotel0_ where lower(hotel0_.name) like ? or lowe
r(hotel0_.city) like ? or lower(hotel0_.zip) like ? or lower(hotel0_.address) li
ke ?
22:12:53,851 INFO  [STDOUT] ***************** getHotels called
22:12:53,856 INFO  [STDOUT] Hibernate: select top ? hotel0_.id as id13_, hotel0_
.address as address13_, hotel0_.city as city13_, hotel0_.country as country13_,
hotel0_.name as name13_, hotel0_.price as price13_, hotel0_.state as state13_, h
otel0_.zip as zip13_ from Hotel hotel0_ where lower(hotel0_.name) like ? or lowe
r(hotel0_.city) like ? or lower(hotel0_.zip) like ? or lower(hotel0_.address) li
ke ?
22:12:53,874 INFO  [STDOUT] ***************** getHotels called
22:12:53,878 INFO  [STDOUT] Hibernate: select top ? hotel0_.id as id13_, hotel0_
.address as address13_, hotel0_.city as city13_, hotel0_.country as country13_,
hotel0_.name as name13_, hotel0_.price as price13_, hotel0_.state as state13_, h
otel0_.zip as zip13_ from Hotel hotel0_ where lower(hotel0_.name) like ? or lowe
r(hotel0_.city) like ? or lower(hotel0_.zip) like ? or lower(hotel0_.address) li
ke ?
22:12:53,894 INFO  [STDOUT] ***************** getHotels called
22:12:53,899 INFO  [STDOUT] Hibernate: select top ? hotel0_.id as id13_, hotel0_
.address as address13_, hotel0_.city as city13_, hotel0_.country as country13_,
hotel0_.name as name13_, hotel0_.price as price13_, hotel0_.state as state13_, h
otel0_.zip as zip13_ from Hotel hotel0_ where lower(hotel0_.name) like ? or lowe
r(hotel0_.city) like ? or lower(hotel0_.zip) like ? or lower(hotel0_.address) li
ke ?
22:12:53,915 INFO  [STDOUT] ***************** getHotels called
22:12:53,920 INFO  [STDOUT] Hibernate: select top ? hotel0_.id as id13_, hotel0_
.address as address13_, hotel0_.city as city13_, hotel0_.country as country13_,
hotel0_.name as name13_, hotel0_.price as price13_, hotel0_.state as state13_, h
otel0_.zip as zip13_ from Hotel hotel0_ where lower(hotel0_.name) like ? or lowe
r(hotel0_.city) like ? or lower(hotel0_.zip) like ? or lower(hotel0_.address) li
ke ?
22:12:53,935 INFO  [STDOUT] ***************** getHotels called
22:12:53,939 INFO  [STDOUT] Hibernate: select top ? hotel0_.id as id13_, hotel0_
.address as address13_, hotel0_.city as city13_, hotel0_.country as country13_,
hotel0_.name as name13_, hotel0_.price as price13_, hotel0_.state as state13_, h
otel0_.zip as zip13_ from Hotel hotel0_ where lower(hotel0_.name) like ? or lowe
r(hotel0_.city) like ? or lower(hotel0_.zip) like ? or lower(hotel0_.address) li
ke ?
22:12:53,956 INFO  [STDOUT] ***************** getHotels called
22:12:53,964 INFO  [STDOUT] Hibernate: select top ? hotel0_.id as id13_, hotel0_
.address as address13_, hotel0_.city as city13_, hotel0_.country as country13_,
hotel0_.name as name13_, hotel0_.price as price13_, hotel0_.state as state13_, h
otel0_.zip as zip13_ from Hotel hotel0_ where lower(hotel0_.name) like ? or lowe
r(hotel0_.city) like ? or lower(hotel0_.zip) like ? or lower(hotel0_.address) li
ke ?
22:12:53,985 INFO  [STDOUT] ***************** getHotels called
22:12:53,990 INFO  [STDOUT] Hibernate: select top ? hotel0_.id as id13_, hotel0_
.address as address13_, hotel0_.city as city13_, hotel0_.country as country13_,
hotel0_.name as name13_, hotel0_.price as price13_, hotel0_.state as state13_, h
otel0_.zip as zip13_ from Hotel hotel0_ where lower(hotel0_.name) like ? or lowe
r(hotel0_.city) like ? or lower(hotel0_.zip) like ? or lower(hotel0_.address) li
ke ?
22:12:54,009 INFO  [STDOUT] ***************** getHotels called
22:12:54,016 INFO  [STDOUT] Hibernate: select top ? hotel0_.id as id13_, hotel0_
.address as address13_, hotel0_.city as city13_, hotel0_.country as country13_,
hotel0_.name as name13_, hotel0_.price as price13_, hotel0_.state as state13_, h
otel0_.zip as zip13_ from Hotel hotel0_ where lower(hotel0_.name) like ? or lowe
r(hotel0_.city) like ? or lower(hotel0_.zip) like ? or lower(hotel0_.address) li
ke ?
22:12:54,061 INFO  [STDOUT] ***************** getHotels called
22:12:54,065 INFO  [STDOUT] Hibernate: select top ? hotel0_.id as id13_, hotel0_
.address as address13_, hotel0_.city as city13_, hotel0_.country as country13_,
hotel0_.name as name13_, hotel0_.price as price13_, hotel0_.state as state13_, h
otel0_.zip as zip13_ from Hotel hotel0_ where lower(hotel0_.name) like ? or lowe
r(hotel0_.city) like ? or lower(hotel0_.zip) like ? or lower(hotel0_.address) li
ke ?
22:12:54,084 INFO  [STDOUT] ***************** getHotels called
22:12:54,117 INFO  [STDOUT] Hibernate: select top ? hotel0_.id as id13_, hotel0_
.address as address13_, hotel0_.city as city13_, hotel0_.country as country13_,
hotel0_.name as name13_, hotel0_.price as price13_, hotel0_.state as state13_, h
otel0_.zip as zip13_ from Hotel hotel0_ where lower(hotel0_.name) like ? or lowe
r(hotel0_.city) like ? or lower(hotel0_.zip) like ? or lower(hotel0_.address) li
ke ?

参考:
Avoid this common JSF mistake