@Id和@GeneratedValue(strategy = GenerationType.IDENTITY)注释的用途是什么?为什么generationtype是身份?

时间:2013-12-16 04:46:46

标签: java sql hibernate

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY)

为什么我们使用此注释? 我需要知道这个自动增量我的表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");
    }
}

*是否有必要扩展Domain抽象类?有什么用?

3 个答案:

答案 0 :(得分:91)

让我回答这个问题:
首先,使用注释作为我们的配置方法只是一种方便的方法,而不是应对无尽的XML配置文件。

@Id注释继承自javax.persistence.Id,表明下面的成员字段是当前实体的主键。因此,你的Hibernate和spring框架以及你可以根据这个注释做一些reflect工作。有关详情,请查看javadoc for Id

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

@GeneratedValue(strategy = GenerationType.IDENTITY)

在Java代码中表示您也承认使用此数据库服务器端策略。此外,您可以更改此注释中的值以满足不同的要求。

1。在数据库中定义序列

例如,Oracle必须使用sequence作为增量方法,比如我们在Oracle中创建一个序列:

create sequence oracle_seq;

2。请参阅数据库序列

既然我们在数据库中有序列,但我们需要使用@SequenceGenerator建立Java和DB之间的关系:

@SequenceGenerator(name="seq",sequenceName="oracle_seq")

sequenceName是Oracle中序列的真实名称,name是您想用Java调用的。如果sequenceNamename不同,则需要指定name,否则只需使用sequenceName。我经常忽略@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq") 以节省我的时间。

3。在Java中使用序列

最后,是时候在Java中使用这个序列了。只需添加@GeneratedValue

generator

name字段指的是您要使用的序列生成器。请注意,它不是数据库中的真实序列名称,而是您在SequenceGenerator的{​​{1}}字段中指定的名称。

4。完整

所以完整版应该是这样的:

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

现在开始使用这些注释来简化JavaWeb开发。

答案 1 :(得分:20)

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

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

  

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

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

答案 2 :(得分:5)

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. 

参考:-https://www.logicbig.com/tutorials/java-ee-tutorial/jpa/jpa-primary-key.html