使用一个复合主键休眠ManyToMany [英] Hibernate ManyToMany with one composite primary key

查看:91
本文介绍了使用一个复合主键休眠ManyToMany的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用休眠注释创建简单的ManyToMany映射时遇到问题.

I've got a problem creating a simple ManyToMany mapping with hibernate anotations.

表PAGE具有复合主键.

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)    |          |              |
+==============+           +================+          +==============+

我使用@EmbeddedId解决了页面"中的复合主数据库.但是我不知道如何在这些表之间创建映射.

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

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

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

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

我收到以下异常

java.sql.SQLIntegrityConstraintViolationException: ORA-02291: integrity constraint (WEB.PAGE_TO_PAGE_FK1) violated - parent key not found

PAGE_TO_PAGE_FK1是在"PAGE_USER"上设置为"PAGE"的外键约束

PAGE_TO_PAGE_FK1 is the foreign key constraint set on "PAGE_USER" to "PAGE"

提前谢谢!

推荐答案

管理关系时出现问题.首先保存 child ,然后保存 parent .

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

这篇关于使用一个复合主键休眠ManyToMany的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆