没有联接表的多对多关联 [英] Many to many association without join table

查看:109
本文介绍了没有联接表的多对多关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在以下(简化的)数据库中应用JPA:

I'd like to apply JPA in the following (simplified) database:

NODE                         AUTHORITY
-----                        ----------
idNode int                   idAuthorities int
nameNode varchar(50)         person varchar(255)
idAuthorities int            rank int
PRIMARY KEY (idNode)         PRIMARY KEY (idAuthorities, rank)
FOREIGN KEY (idAuthorites)

因此,一个节点可以具有多个权限,并且一个权限可以被多个节点引用.

So one node can have multiple authorities and one authority can be referenced by multiple nodes.

我希望我的班级看起来像这样:

And I wanted my classes to look like:

@Entity
@Table(name="NODE")
public class Node {

    private Integer id;
    private String nameNode;
    private Set<Authority> authorities;

    // ... getter and setter normaly annoted for "id" and "nameNode"

    @ManyToMany
    public Set<Authority> getAuthorities(){
        return authorities;
    }
    // ... setter ...

}

@Entity
@Table(name="AUTHORITY")
public class Authority {

    private AuthorityPK pk;
    private String person;
    privat Set<Node> nodes;

    // ... getter and setter normaly annoted for "person"

    @Id
    public AuthorityPK getPk(){
        return this.pk
    }
    // ... setter ...

    @ManyToMany
    public Set<Node> getNodes(){
        return nodes;
    }
    // ... setter ...

}

@Embeddable
public class AuthorityPK implements Serializable {
    private Integer idAuthorities;
    private Integer rankAuthorities;

    // override of equals and hashCode
}

但是注释"@ManyToMany"似乎仅可用于"@JoinTable",在这种情况下,该注释不可用(据我所知). 有人知道修改数据库之外是否还有其他方法吗?

But the annotation "@ManyToMany" seems to be usable only with "@JoinTable", which isn't usable (as far as I understand) in that case. Does anyone know if there is a way arroud beside modifying the database?

推荐答案

JPA不允许这样做,因为出于身份目的,它需要外键引用完整的主键,并且可能不适用于缓存.如果可以的话,我建议使用使用实际主键的关系表切换到更传统的模型.

JPA does not allow this as it requires foreign keys to reference the full primary key for identity purposes, and it might not work well with caching. If you can, I would recommend switching to a more traditional model using a relation table that uses the actual primary keys.

如果您的提供程序允许映射部分pk(我相信Hibernate可以这样做),您要做的就是使用JoinColumn而不是JoinTable进行两个1:M映射,但是在Node-> Authority上将其标记为insertable = false, updatable = false

If your provider allows mapping partial pks (I believe Hibernate does), what you would do is make two 1:M mappings each using a JoinColumn instead of JoinTable, but mark the one on Node->Authority as insertable=false, updatable=false

例如,类似:

public class Node {
    @Id
    private Integer id;
    private String nameNode;
    @OneToMany
    @JoinColumn(name = "idAuthorites", referencedColumnName = "idAuthorites", insertable=false, updatable=false)
    private Set<Authority> authorities;
    ...

public class Authority {
    @Id
    private AuthorityPK pk;
    private String person;
    @OneToMany
    @JoinColumn(name = "idAuthorites", referencedColumnName = "idAuthorites")
    private Set<Node> nodes;
    ...

这篇关于没有联接表的多对多关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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