2016-12-01 2 views
1

J'ai 2 tables que je veux joindre sur PK et FK en JPA, mais le PK est en majuscule et le minuscule FK. Comment faire correspondre l'association insensible à la casse entre Person -> GroupAssociationEntity?Joindre la table sur la case insensible PK et FK avec JPA

Mon mappage actuel ne fonctionne pas.

@Entity 
public class Person { 

    @Id 
    @Column(name = "id", columnDefinition = "nvarchar") 
    private String id; 


    @OneToMany(mappedBy = "person") 
    private List<GroupAssociationEntity> groups; 
} 

@Entity 
@IdClass(GroupAssociationKey.class) 
public class GroupAssociationEntity { 

    @Id 
    private String id; 

    @Id 
    private String memberOf; 

    @ManyToOne 
    @JoinColumn(name = "id", updatable = false, insertable = false, referencedColumnName = "id") 
    private Group group; 

    @ManyToOne 
    @JoinColumn(name = "memberOf", updatable = false, insertable = false, referencedColumnName = "id") 
    private Person person; 

    .... 
} 

@Entity 
public class Group { 

    @Id 
    @Column(name = "id") 
    private String id; 

    @OneToMany(mappedBy = "group") 
    private List<GroupAssociationEntity> persons; 

    ...... 
} 
+0

Il n'y a pas de champ 'memberOf' dans la classe Person. Lorsque vous utilisez '@ JoinColumn' sur une classe, ils doivent en avoir un champ. – msagala25

+0

Il ne doit pas être. 'memberOf' est juste une colonne dans la table des liens. –

Répondre

1

Je commutées votre mappage:

@Entity(name = "Person") 
public static class Person { 

    @Id 
    @Column(name = "id") 
    private String id; 


    @OneToMany(mappedBy = "person") 
    private List<GroupAssociationEntity> groups; 
} 

@Entity(name = "GroupAssociationEntity") 
public static class GroupAssociationEntity { 

    @EmbeddedId 
    private GroupAssociationKey id; 

    @ManyToOne 
    @MapsId("id") 
    private Group group; 

    @ManyToOne 
    @MapsId("memberOf") 
    private Person person; 
} 

@Embeddable 
public static class GroupAssociationKey implements Serializable{ 

    private String id; 

    private String memberOf; 

    public GroupAssociationKey() { 
    } 

    public GroupAssociationKey(String id, String memberOf) { 
     this.id = id; 
     this.memberOf = memberOf; 
    } 

    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public String getMemberOf() { 
     return memberOf; 
    } 

    public void setMemberOf(String memberOf) { 
     this.memberOf = memberOf; 
    } 

    @Override 
    public boolean equals(Object o) { 
     if (this == o) return true; 
     if (!(o instanceof GroupAssociationKey)) return false; 
     GroupAssociationKey that = (GroupAssociationKey) o; 
     return Objects.equals(getId(), that.getId()) && 
       Objects.equals(getMemberOf(), that.getMemberOf()); 
    } 

    @Override 
    public int hashCode() { 
     return Objects.hash(getId(), getMemberOf()); 
    } 
} 

@Entity(name = "Group") 
@Table(name = "groups") 
public static class Group { 

    @Id 
    @Column(name = "id") 
    private String id; 

    @OneToMany(mappedBy = "group") 
    private List<GroupAssociationEntity> persons; 

} 

et exécuter ce test sur les deux SQL Server et MySQL:

doInJPA(entityManager -> { 

    Person person1 = new Person(); 
    person1.id = "abc1"; 
    entityManager.persist(person1); 

    Person person2 = new Person(); 
    person2.id = "abc2"; 
    entityManager.persist(person2); 

    Group group = new Group(); 
    group.id = "g1"; 
    entityManager.persist(group); 

    GroupAssociationEntity p1g1 = new GroupAssociationEntity(); 
    p1g1.id = new GroupAssociationKey("G1", "ABC1"); 
    p1g1.group = group; 
    p1g1.person = person1; 
    entityManager.persist(p1g1); 

    GroupAssociationEntity p2g1 = new GroupAssociationEntity(); 
    p2g1.id = new GroupAssociationKey("G1", "ABC2"); 
    p2g1.group = group; 
    p2g1.person = person2; 
    entityManager.persist(p2g1); 
}); 

doInJPA(entityManager -> { 
    Group group = entityManager.find(Group.class, "g1"); 
    assertEquals(2, group.persons.size()); 
}); 

doInJPA(entityManager -> { 
    Person person = entityManager.find(Person.class, "abc1"); 
    assertEquals(1, person.groups.size()); 
}); 

Et ça marche très bien. Découvrez-le sur GitHub.

0
@Entity 
public class Person { 

    @Id 
    @Column(name = "id", columnDefinition = "nvarchar") 
    private String id; 

} 

@Entity 
@IdClass(GroupAssociationKey.class) 
public class GroupAssociationEntity { 

    @Id 
    private String id; 

    @Id 
    private String memberOf; 

    @ManyToOne 
    @JoinColumn(name = "id", updatable = false, insertable = false, referencedColumnName = "id") 
    private Group group; 

    @ManyToOne 
    @JoinColumn(name = "id", updatable = false, insertable = false, referencedColumnName = "id") 
    private Person person; 

    .... 
} 

@Entity 
public class Group { 

    @Id 
    @Column(name = "id") 
    private String id; 

} 

essayer.