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

 焦鹏666_479 发布于 2023-02-10 11:40

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

@Id注释是继承自javax.persistence.Id,表示下面的构件字段是当前实体的主键.因此,你的Hibernate和spring框架以及你可以reflect根据这个注释做一些工作.有关详细信息,请检查Id的javadoc

@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调用它的.您需要指定sequenceName它是否不同name,否则只需使用name.我经常忽略sequenceName节省时间.

3.在Java中使用序列

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

@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")

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

4.完成

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

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

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

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

    @Id注释是继承自javax.persistence.Id,表示下面的构件字段是当前实体的主键.因此,你的Hibernate和spring框架以及你可以reflect根据这个注释做一些工作.有关详细信息,请检查Id的javadoc

    @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调用它的.您需要指定sequenceName它是否不同name,否则只需使用name.我经常忽略sequenceName节省时间.

    3.在Java中使用序列

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

    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
    

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

    4.完成

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

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

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

    2023-02-10 11:43 回答
  • 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

    2023-02-10 11:43 回答
  • 在对象关系映射上下文中,每个对象都需要具有唯一标识符.您可以使用@Id批注指定实体的主键.

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

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

    还有其他策略,你可以在这里看到更多.

    2023-02-10 11:43 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有