java - 使用 JPA 连接不区分大小写的 PK 和 FK 表

标签 java hibernate jpa join

我想在 JPA 中加入 PK 和 FK 上的 2 个表,但 PK 是大写字母,FK 小写字母。

如何映射 Person -> GroupAssociationEntity 之间不区分大小写的关联?

我当前的映射不起作用。

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

    ......
}

最佳答案

我将您的映射切换为:

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

}

并在 SQL Server 和 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());
} );

而且效果很好。查看GitHub .

关于java - 使用 JPA 连接不区分大小写的 PK 和 FK 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40918597/

相关文章:

java - 在 Java 中检索原始 MySql 输出

java - 是否可以使用 hbm2ddl 为某个数据库列生成默认值

java - 无法 Autowiring 项目中的实体 Bean

java - hibernate - SQLGrammarException : could not extract ResultSet on Persist

java - 如何用JPA映射实体?

java - 如何在其他测试中重用(包含)Junit 测试 block

java - 不在 JPanel 中显示添加到另一个 JPanel 的图形

java - 括号订单追踪止损java交互式经纪商

java - hibernate : Not able to create table

java - Guice 合成方法警告