質問を小さくしようとしましたが、小さくできませんでした:(
私には2つのエンティティがあり、これがそれらの関係です。
Collection (1) ----> (n) Item
Item (0 or 1) -----> (n) Item
つまり、それぞれItem
がに関連していCollection
ます。さらに、それぞれItem
に0個以上の子を含めることができますItem
。子が存在する場合Item
、子Item
はnull以外になりますparent_item_id
。
テーブル:
コレクション
collection_id raw
アイテム
item_id raw
collection_id raw
parent_item_id raw
マッピング:
class Collections
{
@Id @Column("collection_id")
String id;
@OneToMany(fetch=FetchType.EAGER, cascadeType.ALL, mappedBy="collection")
List<Items> items;
}
class Items
{
@Id @Column("item_id")
String id;
@ManyToOne(optional=false)
@JoinColumn(name="collection_id")
Collections collection;
@ManyToOne(optional=true)
@JoinColumn(name="parent_item_id")
Items parentItem;
}
次のようなオブジェクトを作成したとします。
//Constructor parameters are the Ids.
Collections collection1 = new Collections("1234");
Items i1 = new Items("111");
Items i2 = new Items("222");
item1.parentItem = item2;
item1.collection = collection1;
item2.collection = collection1;
List<Items> listItems = new ArrayList<Items>(1);
listItems.add(item2);
collection1.items = listItems;
Hibernateを使用したJPAのラッパーであるデータアクセス層(DA)があります。次に、2つの操作を実行します。
Collections collection1 = DA.merge(collection1);
Collections collection2 = DA.find(collection1.id);
collection2.items.size()
私は2を返すことを期待しますが、1を返します(PS:実際にはコレクションに関連する2つのアイテムがあります1)。
説明できますか?それは予期されているのですか、それともDAのバグですか?JPAはコレクションをキャッシュしますか?
後で、別のトランザクションでフェッチしようとすると、Collections collectionNew = DA.find("1234").items.size()
2(予想)が返されます。