问题
I've got a problem creating a simple ManyToMany mapping with hibernate anotations.
Table PAGE has a composite primary key.
+==============+ +================+ +==============+
| PAGE | | PAGE_USER | | USER |
+--------------+ 1 0..* +----------------+ 0..* 1 +--------------+
| pageid (pk) |-----------| pageid (pk) |----------| userid (pk) |
| entityid (pk)| | entityid (pk) | | ... |
| ... | | userid (pk) | | |
+==============+ +================+ +==============+
I solved the composite primary in the "page" with @EmbeddedId. But i've got no idea how to create the mapping between these tables.
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;
}
}
User.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;
}
}
But when i try to add a new page to an existing user like this
user.getPages().add(new Page(new PageKey(pageId, entityId)));
userDAO.save(user);
i get the following exception
java.sql.SQLIntegrityConstraintViolationException: ORA-02291: integrity constraint (WEB.PAGE_TO_PAGE_FK1) violated - parent key not found
PAGE_TO_PAGE_FK1 is the foreign key constraint set on "PAGE_USER" to "PAGE"
Thanks in advance!
回答1:
There is a problem managing the relationship. Save first the child, next the parent.
Page page = new Page();
page.set(new PageKey(pageId, entityId));
user.getPages().add(page);
pageDAO.save(page);
userDAO.save(user);
来源:https://stackoverflow.com/questions/18214030/hibernate-manytomany-with-one-composite-primary-key