让我回答这个问题:
首先,使用注释作为我们的配置方法只是一种方便的方法,而不是应对无穷无尽的XML配置文件.
的@Id
注释是继承自javax.persistence.Id
,表示下面的构件字段是当前实体的主键.因此,你的Hibernate和spring框架以及你可以reflect
根据这个注释做一些工作.有关详细信息,请检查Id的javadoc
的@GeneratedValue
注释是配置指定列(字段)的增量的方式.例如,在使用时Mysql
,您可以auto_increment
在表的定义中指定使其自增量,然后使用
@GeneratedValue(strategy = GenerationType.IDENTITY)
在Java代码中表示您也承认使用此数据库服务器端策略.此外,您可以更改此注释中的值以满足不同的要求.
例如,Oracle必须使用sequence
增量方法,比如我们在Oracle中创建一个序列:
create sequence oracle_seq;
既然我们在数据库中有序列,但我们需要通过使用@SequenceGenerator
以下方式建立Java和DB之间的关系:
@SequenceGenerator(name="seq",sequenceName="oracle_seq")
sequenceName
是Oracle中序列的真实名称,name
是您希望用Java调用它的.您需要指定sequenceName
它是否不同name
,否则只需使用name
.我经常忽略sequenceName
节省时间.
最后,是时候在Java中使用这个序列了.只需添加@GeneratedValue
:
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
该generator
字段指的是您要使用的序列生成器.请注意,它不是DB中的实际序列名称,而是您在name
字段中指定的名称SequenceGenerator
.
所以完整版应该是这样的:
public class MyTable { @Id @SequenceGenerator(name="seq",sequenceName="oracle_seq") @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq") private Integer pid; }
现在开始使用这些注释来简化JavaWeb开发.
让我回答这个问题:
首先,使用注释作为我们的配置方法只是一种方便的方法,而不是应对无穷无尽的XML配置文件.
的@Id
注释是继承自javax.persistence.Id
,表示下面的构件字段是当前实体的主键.因此,你的Hibernate和spring框架以及你可以reflect
根据这个注释做一些工作.有关详细信息,请检查Id的javadoc
的@GeneratedValue
注释是配置指定列(字段)的增量的方式.例如,在使用时Mysql
,您可以auto_increment
在表的定义中指定使其自增量,然后使用
@GeneratedValue(strategy = GenerationType.IDENTITY)
在Java代码中表示您也承认使用此数据库服务器端策略.此外,您可以更改此注释中的值以满足不同的要求.
例如,Oracle必须使用sequence
增量方法,比如我们在Oracle中创建一个序列:
create sequence oracle_seq;
既然我们在数据库中有序列,但我们需要通过使用@SequenceGenerator
以下方式建立Java和DB之间的关系:
@SequenceGenerator(name="seq",sequenceName="oracle_seq")
sequenceName
是Oracle中序列的真实名称,name
是您希望用Java调用它的.您需要指定sequenceName
它是否不同name
,否则只需使用name
.我经常忽略sequenceName
节省时间.
最后,是时候在Java中使用这个序列了.只需添加@GeneratedValue
:
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
该generator
字段指的是您要使用的序列生成器.请注意,它不是DB中的实际序列名称,而是您在name
字段中指定的名称SequenceGenerator
.
所以完整版应该是这样的:
public class MyTable { @Id @SequenceGenerator(name="seq",sequenceName="oracle_seq") @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq") private Integer pid; }
现在开始使用这些注释来简化JavaWeb开发.
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
在对象关系映射上下文中,每个对象都需要具有唯一标识符.您可以使用@Id
批注指定实体的主键.
该@GeneratedValue
注释用于指定主键应该如何生成的.在您的示例中,您使用的是Identity
策略
指示持久性提供程序必须使用数据库标识列为实体分配主键.
还有其他策略,你可以在这里看到更多.