【问题标题】:Mapping a ManyToMany across join/link table using 3 ID Fields使用 3 个 ID 字段跨连接/链接表映射多对多
【发布时间】:2009-08-03 10:28:10
【问题描述】:

是否可以在使用 3 个 PK 列的连接中使用 @ManyToMany 映射?

我相当肯定这是不可能的,解决方案是使用我过去做过的@OneToMany 公开实体,但是因为这个字段只是一个 PK,我认为可能有一些聪明的映射可以拯救我工作。

问候一个有希望的人 - JamesC

响应 ChssPly 的更新

这是我的“加入”实体的 sn-p。

@SuppressWarnings("serial")
    @Embeddable
    public static class Id implements Serializable {

        @Column(name = StringPool.Column.APPLICATION_ID)
        private Long applicationId; 

        @Column(name = StringPool.Column.ARTICLECATEGORY_ID)
        private Long articleCategoryId;

        @Column(name = StringPool.Column.USER_ID)
        private Long userId;

        public Id() {}

        public Id(Long applicationId, Long articleCategoryId, Long userId) {
            this.applicationId = applicationId;
            this.articleCategoryId = articleCategoryId;
            this.userId = userId;
        }

        public boolean equals(Object o) {
            if (o instanceof Id) {
                Id that = (Id)o;
                return this.applicationId.equals(that.applicationId) &&
                       this.articleCategoryId.equals(that.articleCategoryId) &&
                       this.userId.equals(that.userId);
            } else {
                return false;
            }
        }

        public int hashCode() {
            return applicationId.hashCode() + articleCategoryId.hashCode() + userId.hashCode();
        }
    }

【问题讨论】:

    标签: hibernate jpa


    【解决方案1】:

    这取决于您所说的“3 PK 列”是什么意思。您要链接的实体之一是否具有复合键?您当然可以指定多个连接列:

    @Entity
    public class MyEntity {
    @ManyToMany(targetEntity=MyOtherEntity.class)
    @JoinTable(name="MY_JOIN_TABLE",
        joinColumns=@JoinColumn(name="ENTITY_ID"),
        inverseJoinColumns={@JoinColumn(name="OTHER_ID1"), @JoinColumn(name="OTHER_ID2")})
      public Collection getOthers() {
          return employees;
      }
    }
    

    如果这不是你的意思,请澄清你的问题。

    【讨论】:

    • 打算提出基本相同的建议
    • 谢谢,这正是我想要的。遗憾的是,我现在已将其重构为 OneToMany 设置,但下次我会知道的。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 2010-09-28
    • 1970-01-01
    • 2022-08-24
    相关资源
    最近更新 更多