使用3个ID字段在连接/链接表中映射ManyToMany

时间:2009-08-03 10:28:10

标签: hibernate jpa

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

我很确定这是不可能的,解决方案是使用我以前做过的@OneToMany公开实体,但是因为这个字段只是一个PK,我想可能有一些聪明的映射来救我这个工作

关心希望 - 詹姆斯克

响应ChssPly更新

以下是我的“加入”实体的片段。

@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();
        }
    }

1 个答案:

答案 0 :(得分:3)

这取决于“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;
  }
}

如果那不是您的意思,请澄清您的问题。

相关问题