私のシナリオは users テーブルと pictures テーブルです。各ユーザーは複数の写真をアップロードできます。各ユーザーは、自分の写真の 1 つをお気に入りの写真に設定できますが、その写真がそのユーザーの写真コレクション内にあることには影響しません。
これを休止状態にマッピングするのは難しいことがわかっています。私のアプリケーションには、ユーザーのすべての写真のリストを表示し、どの写真がお気に入りかを示す画面があります。ただし、アプリケーションが写真を読み込むと、お気に入りの写真が 1 つだけ読み込まれません。Eclipse でデバッグすると、リスト内の特定のオブジェクト (おそらくプロキシ オブジェクト) についてさまざまな興味深いことがわかります。マッピングを示すエンティティ コードは次のとおりです。
@Entity
class Account {
@Id
public String username;
/* Originally a Set<Picture>, but wanted ordering */
@OneToMany(cascade=CascadeType.ALL, mappedBy="account")
@OrderBy("uploadtime DESC")
public List<Picture> pictures;
@ManyToOne(fetch = FetchType.LAZY, optional=true)
@LazyToOne(LazyToOneOption.PROXY)
@JoinColumn(name="favourite_picture_id")
public Picture favourite_picture;
/* Sometimes it is useful to get just the id, if possible without loading the entire entity */
@OneToOne(fetch = FetchType.LAZY, optional=true)
@Column(insertable=false, updatable=false)
public String favourite_picture_id;
}
@Entity
public class Picture {
@Id
@GeneratedValue(generator="uuid")
@GenericGenerator(name="uuid", strategy="uuid2")
public String id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="username")
public Account account;
/* This is never used, but I thought this might be required for the mapping? */
@OneToOne(mappedBy="favourite_picture")
public Account account_favourite_picture;
public String mimetype;
public Date uploadtime = new Date();
@Column(length=1024 * 1024 * 4)
public byte[] data;
}
お気に入りの写真が写真のリストに読み込まれない理由がわかりません。どんな提案でも大歓迎です:D