Java 注释@Id 和@GeneratedValue(strategy = GenerationType.IDENTITY) 的用途是什么?为什么生成类型是身份?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20603638/
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
what is the use of annotations @Id and @GeneratedValue(strategy = GenerationType.IDENTITY)? Why the generationtype is identity?
提问by Lijo
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Why we are using this annotations? i need to know if this autoincrement my table id values. (GenerationType.IDENTITY) is there any other types whats actually happening when we use this annotation
为什么我们要使用这个注解?我需要知道这是否会自动增加我的表 ID 值。(GenerationType.IDENTITY) 当我们使用这个注解时,是否还有其他类型实际发生了什么
public class Author extends Domain
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@Column(name = "name")
private String name;
@Column(name = "address")
private String address;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "authorId")
private List<Book>
bookList;
public Author()
{
setServiceClassName("wawo.tutorial.service.admin.AuthorService");
}
}
*Is it necessary to extend Domain abstract class?What is the use?
*是否需要扩展Domain抽象类?有什么用?
采纳答案by Rugal
Let me answer this question:
First of all, using annotations as our configure method is just a convenient method instead of coping the endless XML configuration file.
让我来回答这个问题:
首先,使用注解作为我们的配置方法只是一种方便的方法,而不是应付无穷无尽的 XML 配置文件。
The @Id
annotation is inherited from javax.persistence.Id
, indicating the member field below is the primary key of current entity. Hence your Hibernate and spring framework as well as you can do some reflect
works based on this annotation. for details please check javadoc for Id
的@Id
注释是继承自javax.persistence.Id
,指示构件字段下面是当前实体的主键。因此你的 Hibernate 和 spring 框架以及你可以reflect
基于这个注释做一些工作。有关详细信息,请查看javadoc 以获取 ID
The @GeneratedValue
annotation is to configure the way of increment of the specified column(field). For example when using Mysql
, you may specify auto_increment
in the definition of table to make it self-incremental, and then use
的@GeneratedValue
注释是配置指定列(字段)的增量的方式。例如使用时Mysql
,可以auto_increment
在表的定义中指定使其自增,然后使用
@GeneratedValue(strategy = GenerationType.IDENTITY)
in the Java code to denote that you also acknowledged to use this database server side strategy. Also, you may change the value in this annotation to fit different requirements.
在 Java 代码中表示您也承认使用此数据库服务器端策略。此外,您可以更改此注释中的值以适应不同的要求。
1. Define Sequence in database
1.在数据库中定义序列
For instance, Oracle has to use sequence
as increment method, say we create a sequence in Oracle:
例如,Oracle 必须使用sequence
增量方法,假设我们在 Oracle 中创建一个序列:
create sequence oracle_seq;
2. Refer the database sequence
2.参考数据库序列
Now that we have the sequence in database, but we need to establish the relation between Java and DB, by using @SequenceGenerator
:
现在我们在数据库中有了序列,但是我们需要建立 Java 和 DB 之间的关系,通过使用@SequenceGenerator
:
@SequenceGenerator(name="seq",sequenceName="oracle_seq")
sequenceName
is the real name of a sequence in Oracle, name
is what you want to call it in Java. You need to specify sequenceName
if it is different from name
, otherwise just use name
. I usually ignore sequenceName
to save my time.
sequenceName
在 Oracle 中name
是序列的真实名称,在 Java 中就是您想称呼它的名称。您需要指定sequenceName
它是否不同于name
,否则只需使用name
. 我通常会忽略sequenceName
以节省时间。
3. Use sequence in Java
3.在Java中使用序列
Finally, it is time to make use this sequence in Java. Just add @GeneratedValue
:
最后,是时候在 Java 中使用这个序列了。只需添加@GeneratedValue
:
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
The generator
field refers to which sequence generator you want to use. Notice it is not the real sequence name in DB, but the name you specified in name
field of SequenceGenerator
.
该generator
字段是指您要使用的序列生成器。请注意,它不是 DB 中的真实序列名称,而是您在 的name
字段中指定的名称SequenceGenerator
。
4. Complete
4. 完成
So the complete version should be like this:
所以完整版应该是这样的:
public class MyTable
{
@Id
@SequenceGenerator(name="seq",sequenceName="oracle_seq")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
private Integer pid;
}
Now start using these annotations to make your JavaWeb development easier.
现在开始使用这些注释来使您的 JavaWeb 开发更容易。
回答by Sotirios Delimanolis
In a Object Relational Mapping context, every object needs to have a unique identifier. You use the @Id
annotation to specify the primary key of an entity.
在对象关系映射上下文中,每个对象都需要有一个唯一的标识符。您可以使用@Id
注释来指定实体的主键。
The @GeneratedValue
annotation is used to specify how the primary key should be generated. In your example you are using an Identity
strategy which
该@GeneratedValue
注释用于指定主键应该如何生成的。在您的示例中,您使用的Identity
策略是
Indicates that the persistence provider must assign primary keys for the entity using a database identity column.
指示持久性提供程序必须使用数据库标识列为实体分配主键。
There are other strategies, you can see more here.
还有其他策略,你可以在这里看到更多。
回答by SumiSujith
Simply, @Id: This annotation specifies the primary key of the entity.
@GeneratedValue: This annotation is used to specify the primary key generation strategy to use. i.e Instructs database to generate a value for this field automatically. If the strategy is not specified by default AUTO will be used.
GenerationType enum defines four strategies:
1. Generation Type . TABLE,
2. Generation Type. SEQUENCE,
3. Generation Type. IDENTITY
4. Generation Type. AUTO
GenerationType.SEQUENCE
With this strategy, underlying persistence provider must use a database sequence to get the next unique primary key for the entities.
GenerationType.TABLE
With this strategy, underlying persistence provider must use a database table to generate/keep the next unique primary key for the entities.
GenerationType.IDENTITY
This GenerationType indicates that the persistence provider must assign primary keys for the entity using a database identity column. IDENTITY column is typically used in SQL Server. This special type column is populated internally by the table itself without using a separate sequence. If underlying database doesn't support IDENTITY column or some similar variant then the persistence provider can choose an alternative appropriate strategy. In this examples we are using H2 database which doesn't support IDENTITY column.
GenerationType.AUTO
This GenerationType indicates that the persistence provider should automatically pick an appropriate strategy for the particular database. This is the default GenerationType, i.e. if we just use @GeneratedValue annotation then this value of GenerationType will be used.
Reference:- https://www.logicbig.com/tutorials/java-ee-tutorial/jpa/jpa-primary-key.html
参考:- https://www.logicbig.com/tutorials/java-ee-tutorial/jpa/jpa-primary-key.html