Java SQL JPA - 多列作为主键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41143913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:10:57  来源:igfitidea点击:

SQL JPA - Multiple columns as primary key

javasqljpaspring-bootentity

提问by Anders Pedersen

If i want severeal Column to make up an ID.

如果我想要严重的列来组成一个 ID。

SQL example :

SQL 示例:

CONSTRAINT [PK_NAME] PRIMARY KEY ([Column1],[Column2],[Column3])

How can i do that with a Jpa Entity class ? through columndefinition ?

我怎么能用 Jpa 实体类做到这一点?通过列定义?

just setting the id field as:

只需将 id 字段设置为:

value = Column1 + Column2 + Column3 // aint working.

采纳答案by Raul Cuth

You need to have a class for your composite key:

您需要为复合键创建一个类:

public class CompositeKey implements Serializable {
    private int column1;
    private int column2;
    private int column3;
}

and then in your entity class use the @IdClassannotation:

然后在您的实体类中使用@IdClass注释:

@Entity
@IdClass(CompositeKey.class)
public class EntityExample {
    @Id
    private int column1;
    @Id
    private int column2;
    @Id
    private int column3;
    ...
    ...
}

I think this should work. Hope it helps, cheers!

我认为这应该有效。希望能帮到你,加油!

Yea and there is the other solution, the one that @jklee mentioned, both work, it's a matter of preference.

是的,还有另一种解决方案,@jklee 提到的那个,两者都有效,这是一个偏好问题。

回答by Penguin

  1. Using @IdClassannotation on the @Entityclass followed by @Idannotation on individual fields that are part of composite primary key.
  2. Alternatively can make use of @Embeddableclass which can consist of individual fields of the composite primary key and then a reference of this class can be used as an attribute with @Embeddedannotation in @Entityclass. Hope this helps.
  1. @IdClass@Entity类上使用注释,然后@Id在作为复合主键一部分的各个字段上使用注释。
  2. 或者,可以使用@Embeddable由复合主键的各个字段组成的类,然后可以将此类的引用用作类中带有@Embedded注释的属性@Entity。希望这可以帮助。

回答by jklee

Use @Embeddableand @EmbeddedId.

使用@Embeddable@EmbeddedId

Example:

例子:

@Entity
public class Project {
    @EmbeddedId ProjectId id;
     :
}

@Embeddable
Class ProjectId {
    int departmentId;
    long projectId;
}

More information here http://www.objectdb.com/java/jpa/entity/id#Embedded_Primary_Key_

更多信息在这里http://www.objectdb.com/java/jpa/entity/id#Embedded_Primary_Key_

回答by heroin

If all fields in the class are part of primary key, then solution would be pretty simple (extending solution provided by @raul-cuth):

如果类中的所有字段都是主键的一部分,那么解决方案将非常简单(扩展解决方案由@raul-cuth 提供):

@Entity
@IdClass(EntityExample.class)
public class EntityExample implements Serializable {

    @Id
    private int column1;

    @Id
    private int column2;

    @Id
    private int column3;
}

回答by user577736

Be aware that the hibernate Entity-Class-to-SQL-DDL-Script generator will sort all the fields, and irrespective of the order in which it appears in the definitions, will create the table definition and the index / constraint definitions in this sorted order of the fields.

请注意,hibernate Entity-Class-to-SQL-DDL-Script 生成器将对所有字段进行排序,并且无论它在定义中出现的顺序如何,都将在此排序中创建表定义和索引/约束定义字段的顺序。

While the order of appearance of the fields in the table definition may not matter much, the order of fields in a composite index definitely do. So your key-fields must be named so that when sorted by their names they are in the order you desire for the index).

虽然表定义中字段的出现顺序可能无关紧要,但复合索引中字段的顺序确实很重要。因此,您的关键字段必须命名,以便按名称排序时,它们会按您希望的索引顺序排列)。