休止状態の注釈を使用して単純な ManyToMany マッピングを作成する際に問題が発生しました。
テーブル PAGE には複合主キーがあります。
+==============+ +================+ +==============+
| PAGE | | PAGE_USER | | USER |
+--------------+ 1 0..* +----------------+ 0..* 1 +--------------+
| pageid (pk) |-----------| pageid (pk) |----------| userid (pk) |
| entityid (pk)| | entityid (pk) | | ... |
| ... | | userid (pk) | | |
+==============+ +================+ +==============+
@EmbeddedId を使用して、「ページ」の複合プライマリを解決しました。しかし、これらのテーブル間のマッピングを作成する方法がわかりません。
Page.java
@Entity
@Table(name = "PAGE")
public class Page {
@EmbeddedId
private PageKey pageKey;
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "pages")
private Set<User> users = new HashSet<User>();
public Page() {
}
public Page(final PageKey pageKey, final Date lastChanged) {
this.pageKey = pageKey;
this.lastChanged = lastChanged;
}
public PageKey getPageKey() {
return this.pageKey;
}
public void setPageKey(final PageKey pageKey) {
this.pageKey = pageKey;
}
public Set<User> getUsers() {
return this.users;
}
public void setUsers(final Set<User> users) {
this.users = users;
}
}
PageKey.java
@Embeddable
public class PageKey implements Serializable {
private static final long serialVersionUID = 2671213284295386474L;
@Column(name = "pageid")
private Long id;
@Column(name = "entityid")
private Long entityId;
public PageKey() {
}
public PageKey(final Long id, final Long entityId) {
this.id = id;
this.entityId = entityId;
}
public Long getId() {
return this.id;
}
public void setId(final Long id) {
this.id = id;
}
public Long getEntityId() {
return this.entityId;
}
public void setEntityId(final Long entityId) {
this.entityId = entityId;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = (prime * result) + ((this.entityId == null) ? 0 : this.entityId.hashCode());
result = (prime * result) + ((this.id == null) ? 0 : this.id.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
final PageKey other = (PageKey) obj;
if (this.entityId == null) {
if (other.entityId != null) {
return false;
}
} else if (!this.entityId.equals(other.entityId)) {
return false;
}
if (this.id == null) {
if (other.id != null) {
return false;
}
} else if (!this.id.equals(other.id)) {
return false;
}
return true;
}
}
ユーザー.java
@Entity
@Table(name = "USER")
public class User {
@Id
@Column(name = "ID")
private Long id;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "PAGE_USER", joinColumns = { @JoinColumn(name = "userid", nullable = false) }, inverseJoinColumns = { @JoinColumn(name = "pageid", nullable = false), @JoinColumn(name = "entityid", nullable = false) })
private Set<Page> pages = new HashSet<Page>();
public User() {
}
public Long getId() {
return this.id;
}
public Set<Page> getPages() {
return this.pages;
}
public void setPages(final Set<Page> pages) {
this.pages = pages;
}
}
しかし、このように既存のユーザーに新しいページを追加しようとすると
user.getPages().add(new Page(new PageKey(pageId, entityId)));
userDAO.save(user);
次の例外が発生します
java.sql.SQLIntegrityConstraintViolationException: ORA-02291: integrity constraint (WEB.PAGE_TO_PAGE_FK1) violated - parent key not found
PAGE_TO_PAGE_FK1 は、「PAGE_USER」から「PAGE」に設定された外部キー制約です。
前もって感謝します!