tags:

views:

195

answers:

1

Is it possible to use a @ManyToMany mapping across a join using 3 PK columns?

I'm fairly certain this is not possible and the solution is to expose the entity using @OneToMany which I have done in the past, but because this field is simply a PK I thought there maybe some clever mapping to save me this work.

Regards a Hopeful - JamesC

Update in response to ChssPly

Here is a snippet of my 'Join' entity.

@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();
        }
    }
+3  A: 

That depends on what you mean by "3 PK columns". Does one of the entities you're linking have a composite key? You can certainly specify multiple join columns:

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

If that's not what you meant, please clarify your question.

ChssPly76
was going to suggest basically the same thing
instanceofTom
Thank You that's exactly what I wanted. Pity I've refactored this into a OneToMany setup now, but I'll know for next time. Thank You.
JamesC