使用一个复合主键来Hibernate ManyToMany

时间:2013-08-13 16:08:40

标签: hibernate annotations jpa-2.0

我在使用hibernate anotations创建一个简单的ManyToMany映射时遇到了问题。

表格PAGE有一个复合主键。

+==============+           +================+          +==============+
| PAGE         |           | PAGE_USER      |          | USER         |
+--------------+ 1    0..* +----------------+ 0..*   1 +--------------+
| pageid (pk)  |-----------| pageid (pk)    |----------| userid (pk)  |
| entityid (pk)|           | entityid (pk)  |          | ...          |
| ...          |           | userid (pk)    |          |              |
+==============+           +================+          +==============+

我用@EmbeddedId解决了“page”中的复合主要问题。但我不知道如何在这些表之间创建映射。

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;
    }
}

但是当我尝试向这样的现有用户添加新页面时

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”上设置的外键约束

提前致谢!

1 个答案:

答案 0 :(得分:1)

管理这种关系存在问题。首先保存孩子,然后是

Page page = new Page();
page.set(new PageKey(pageId, entityId));
user.getPages().add(page);

pageDAO.save(page);
userDAO.save(user);