Java 注释@Id和@GeneratedValue(strategy=GenerationType.IDENTITY)的用途是什么?为什么世代类型是身份?

Java 注释@Id和@GeneratedValue(strategy=GenerationType.IDENTITY)的用途是什么?为什么世代类型是身份?,java,sql,hibernate,java-annotations,Java,Sql,Hibernate,Java Annotations,我们为什么使用这种注释? 我需要知道这是否会自动增加我的表id值。 (GenerationType.IDENTITY)当我们使用此注释时,是否存在其他类型的实际情况 @Id @GeneratedValue(strategy = GenerationType.IDENTITY) 公共类作者扩展域 { @身份证 @GeneratedValue(策略=GenerationType.IDENTITY) @基本(可选=假) @列(name=“id”) 私有整数id; @基本(可选=假) @列(name

我们为什么使用这种注释? 我需要知道这是否会自动增加我的表id值。 (GenerationType.IDENTITY)当我们使用此注释时,是否存在其他类型的实际情况

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY)
公共类作者扩展域
{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@基本(可选=假)
@列(name=“id”)
私有整数id;
@基本(可选=假)
@列(name=“name”)
私有字符串名称;
@列(name=“address”)
私有字符串地址;
@OneToMany(cascade=CascadeType.ALL,mappedBy=“authorId”)
私人名单
书目;
公共作者()
{ 
setServiceClassName(“wawo.tutorial.service.admin.authorsService”);
}
}

*有必要扩展域抽象类吗?有什么用?

在对象关系映射上下文中,每个对象都需要有唯一的标识符。可以使用注释指定实体的主键

注释用于指定如何生成主键。在您的示例中,您使用的策略


指示持久性提供程序必须为其分配主键 使用数据库标识列的实体


还有其他策略,您可以看到更多。

首先,使用注释作为我们的配置方法只是一种方便的方法,而不是处理无休止的XML配置文件

@Id
注释继承自
javax.persistence.Id
,表示下面的成员字段是当前实体的主键。因此,您的Hibernate和spring框架以及您可以基于此注释进行一些
reflect
工作。详情请查收

@GeneratedValue
注释用于配置指定列(字段)的增量方式。例如,当使用
Mysql
时,您可以在表的定义中指定
auto_increment
,使其自增量,然后使用

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");
    }
}
在Java代码中,表示您也承认使用此数据库服务器端策略。此外,您可以更改此注释中的值以适应不同的要求

1.在数据库中定义序列 例如,Oracle必须使用
sequence
作为增量方法,比如我们在Oracle中创建一个序列:

@GeneratedValue(strategy = GenerationType.IDENTITY)
2.参考数据库序列 现在我们在数据库中有了序列,但我们需要使用以下方法建立Java和DB之间的关系:

sequenceName
是Oracle中序列的真实名称,
name
是Java中要称之为序列的名称。如果与
name
不同,则需要指定
sequenceName
,否则只需使用
name
。我通常忽略
sequenceName
以节省时间

3.在Java中使用序列 最后,是在Java中使用此序列的时候了。只需添加:

generator
字段表示要使用的序列生成器。请注意,它不是DB中的真实序列名,而是您在
SequenceGenerator
name
字段中指定的名称

4.完成 所以完整的版本应该是这样的:

@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
现在开始使用这些注释来简化JavaWeb开发

public class MyTable
{
    @Id
    @SequenceGenerator(name="seq",sequenceName="oracle_seq")        
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")               
    private Integer pid;
}

Reference:-

“表示持久性提供程序必须使用数据库标识列为实体分配主键。”您能解释一下吗this@404数据库用于生成主键的一种策略是保留一个带有列(YMMV)的表,其中只存储分配的ID。当必须输入新行时,将生成并使用一个新的id,而该id最初不在表中。那么id是否自动递增?@404我认为这取决于数据库。MySQL看起来就是这样,但可能与其他数据库不同。Oracle 11.2的注释SequenceGenerator的正确语法是@SequenceGenerator(name=“seq”,sequenceName=“Oracle_seq”,allocationSize=1)。否则,如果没有allocationSize参数,它会生成非常奇怪的结果(在我的例子中是负数)。@hariprasad在我的例子中,如果我不设置它,增量是10。但这仍然是一个可选参数。我的一个朋友说@Id代表唯一标识fy。若您将遵循代码优先的方法,那个么它将是主要的区别。你能解释一下上面的句子吗?@PSatishPatro正确,Id当然是唯一的记录。但是,即使对于NoSQL,在表定义中也总是会有这样的内容
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
public class MyTable
{
    @Id
    @SequenceGenerator(name="seq",sequenceName="oracle_seq")        
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")               
    private Integer pid;
}
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.