@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
为什么我们要使用这个注解?我需要知道这是否会自动递增我的表标识值。(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抽象类吗?有什么用?
发布于 2013-12-16 15:20:09
首先,使用注释作为我们的配置方法是一种方便的方法,而不是复制无穷无尽的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")
在Oracle中,sequenceName
是序列的真实名称,而在Java中,name
则是您想要的名称。如果与name
不同,则需要指定sequenceName
,否则直接使用name
。为了节省时间,我通常忽略sequenceName
。
3.在Java中使用序列
最后,是时候在Java中使用这个序列了。只需添加@GeneratedValue
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
generator
字段表示要使用的序列生成器。请注意,它不是DB中的真实序列名称,而是您在SequenceGenerator
的name
字段中指定的名称。
4.完成
所以完整的版本应该是这样的:
public class MyTable
{
@Id
@SequenceGenerator(name="seq",sequenceName="oracle_seq")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
private Integer pid;
}
现在开始使用这些注释来简化您的JavaWeb开发。
发布于 2013-12-16 12:51:57
在对象关系映射上下文中,每个对象都需要具有唯一的标识符。您可以使用@Id
注释来指定实体的主键。
@GeneratedValue
注释用于指定应该如何生成主键。在您的示例中,您使用的是Identity
策略,该策略
指示持久性提供程序必须使用数据库标识列为实体分配主键。
还有其他策略,你可以看到更多的here。
发布于 2018-11-23 20:27:04
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
https://stackoverflow.com/questions/20603638
复制相似问题