私は次の戦略でうまくいくことができました。
この戦略は、コレクション全体をフェッチしたくない場合、またはいくつかの追加基準を使用してフェッチしたくない場合に非常にうまく機能します。名前付きクエリを使用してコレクション(コレクションリレーション)を取得できます。多対多の関係のJOINテーブルでのCRUD操作には個別のDAOを使用します。たとえば、ユーザーは多くのアカウントを持つことができ、アカウントは多くのユーザーによって共有できます。3つのテーブルすべてに対してドメインモデル/DAOを作成し、取得だけにリレーションマッピングを使用し、DDLには個々のプロパティを使用します。
@Entity
@Table(name="account" )
public class Account {
@Id (name="accountid")
private Long accountId;
@Column
private String type;
// removed @OneToMany as it causes issue while serializing to xml
@Transient
private Collection accountUsers;
//rest of the properties n geter setter goes here
}
@Entity
@Table(name="user")
public class User {
@Id(name="userid")
private Long userId;
@Column
private String name;
// by making transient jpa / hibernate does not initialize it with proxy.. so it remains null
/* you can populate this property using named query whenever required .*/
@Transient
private Collection userAccounts;
// rest of the properties n getter setter goes here
}
@Entity
@Table(name="AccountUser")
public class AccountUser {
// whatever your strategy to implement primary key here ,
@Id (name="accountuserid")
private Long accountUserId;
/* please note this annotation , made insertable/updatable false , relation defined just for fetching relation
*/
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "accountid", referencedColumnName = "accountid", insertable = false, updatable = false)
private Account account;
// look at insertable / updatable properties its turned off
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "userid", referencedColumnName = "userid", insertable = false, updatable = false)
private User user;
//
@Column ( name = "userid" )
private Long userId;
@Column ( name = "accountid" )
private Long accountId;
@Column ( name="opendate")
private Date opendate;
}
/* use separate dao to save above beans */
// somthing like this
public class AccountDAOImpl extends GenericDAOImpl implements AccountDAO {
}
public class UserDAOImpl extends GenericDAOImpl implements UserDAO {
}
public class AccountUserDAOImpl extends GenericDAOImpl implements AccountUserDAO {
}
何か説明が必要な場合は、親切に元に戻して説明しようとしました。ありがとう