JPA表有2个主键字段 [英] JPA table with 2 primary key fields

查看:535
本文介绍了JPA表有2个主键字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只包含2个字段的表。该表具有由这两个字段组成的复合PK。

I have a table which contains only 2 fields. The table has a composite PK formed by these two fields.

当使用Netbeans从数据库创建实体bean时,实体bean不会像其他具有更多数据的表一样自动创建超过2个字段。

When using Netbeans for creating entity beans from database, the entity bean is not created automatically like other tables that have more than 2 fields.

所以我想我需要自己创建实体bean。创建此实体bean的最佳做法是什么?它是否必须包含 COMPOSITE KEY 对象?

So I guess I need to create the entity bean myself. What is the best practice for creating this entity bean? Does it have to contain COMPOSITE KEY object or not?

推荐答案

我不要使用NetBeans,所以我无法真正说出它的映射工具。

I don't use NetBeans so I can't really say anything about its mapping tools.

对于映射复合键,有几个选项。你可以

For mapping a composite key, there are a couple of options. You can

  • Define a separate @Embeddable object with the PK fields and use it as @EmbeddedId in your @Entity class

@Embeddable
public class MyCompositePK { 
    @Column
    private String fieldA;
    @Column
    private String fieldB;
}
@Entity 
public class MyBean { 
    @EmbeddedId
    private MyCompositePK id;
    @Column
    private String fieldC;
}


  • 使用PK字段定义未映射的POJO并使用它如 @IdClass @Entity 中的

    @Entity
    @IdClass(value=ClassAB.ClassABId.class)
    public class ClassAB implements Serializable {
        private String idA;
        private String idB;
    
        @Id
        @Column(name="ID_A")
        public String getIdA(){ return idA; }
        public void setIdA(String idA){ this.idA = idA; }
    
        @Id
        @Column(name="ID_B")
        public String getIdB(){ return idB; }
        public void setIdB(String idB){ this.idB = idB; }
    
        static class ClassABId implements Serializable {
            private String idA;
            private String idB;
    
            public String getIdA(){ return idA; }
            public void setIdA(String idA){ this.idA = idA; }
    
            public String getIdB(){ return idB; }
            public void setIdB(String idB){ this.idB = idB; }
    
            // implement equals(), hashcode()
        }
    }
    

    在此示例中, ClassABId 是一个静态内部类,只是为了方便起见。

    In this example ClassABId is a static inner class just for convenience.

    Pascal Thivent对这个问题的出色回答也解释了这些选项:如何使用Hibernate映射复合密钥?

    These options are also explained in Pascal Thivent's excellent answer to this question: How to map a composite key with Hibernate?.

    这个相关问题讨论了这些方法之间的差异:< a href =https://stackoverflow.com/q/212350/851811>我应该使用哪种anotation:@IdClass或@EmbeddedId 。请注意,字段的声明与 @IdClass 方法重复。

    This related question discusses differences between these approaches: Which anotation should I use: @IdClass or @EmbeddedId. Notice the fields' declaration gets duplicated with the @IdClass approach.

    无论如何,我认为没有其他方法可以创建两个类。这就是我提出这个问题的原因:映射一个只包含没有@IdClass或@EmbeddedId的复合PK的类。似乎有一个特定于hibernate的功能。

    Anyhow, I don't think there's an alternative to creating two classes. That's why I asked this question : Mapping a class that consists only of a composite PK without @IdClass or @EmbeddedId. It seems there's an hibernate-specific feature for this.

    作为旁注,如果你能控制数据库结构,你也可以考虑避免使用复合键。 有一些理由这样做

    As a side note, if you've got control over the DB structure, you might also consider avoiding composite keys. There are some reasons to do so.

    这篇关于JPA表有2个主键字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆