注解@Id和@GeneratedValue(strategy = GenerationType.IDENTITY)有什么用?为什么生成类型是身份? [英] what is the use of annotations @Id and @GeneratedValue(strategy = GenerationType.IDENTITY)? Why the generationtype is identity?

查看:12907
本文介绍了注解@Id和@GeneratedValue(strategy = GenerationType.IDENTITY)有什么用?为什么生成类型是身份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @Id 
@GeneratedValue(strategy = GenerationType.IDENTITY)

为什么我们使用这个注释?
我需要知道这是否自动增加我的表ID值。
(GenerationType.IDENTITY)是否还有其他类型实际发生的事情当我们使用这个注解时

  public class Author extends Domain 
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(可选= false)
@Column(name =id)
私人整数ID;

@Basic(可选= false)
@Column(name =name)
私有字符串名称;

@Column(name =address)
私有字符串地址;

@OneToMany(cascade = CascadeType.ALL,mappedBy =authorId)
private List< Book>
bookList;
$ b $ public Author()
{
setServiceClassName(wawo.tutorial.service.admin.AuthorService);


$ / code $ / pre

是否需要扩展Domain抽象类?是否有用?

解决方案

让我回答这个问题:

首先,使用注释作为我们的配置方法只是一种方便的方法,而不是应付无尽的XML配置文件。
$ b

@Id 注释是从 javax.persistence.Id ,表示下面的成员字段是当前实体的主键。因此,您的Hibernate和Spring框架以及您可以根据此批注执行一些 reflect 作品。详情请查阅 javadoc for Id



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

  @GeneratedValue(strategy = GenerationType.IDENTITY)


1。在数据库中定义序列



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

  create sequence oracle_seq; 



2。引用数据库序列



既然我们有数据库中的序列,但是我们需要通过使用 @SequenceGenerator

  @SequenceGenerator(name =seq,sequenceName =oracle_seq)

sequenceName 是Oracle中序列的真实名称, name 是什么你想用Java调用它。如果它不同于 name ,则需要指定 sequenceName ,否则只需使用 name 。我通常会忽略 sequenceName 来节省时间。

3。在Java中使用序列



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

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

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

4。完成



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

  public class MyTable 
{
@Id
@SequenceGenerator(name =seq,sequenceName =oracle_seq)
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator =seq )
私人整数pid;
}

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

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY)

Why we are using this annotations? i need to know if this autoincrement my table id values. (GenerationType.IDENTITY) is there any other types whats actually happening when we use this annotation

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");
    }
}

*Is it necessary to extend Domain abstract class?What is the use?

解决方案

Let me answer this question:
First of all, using annotations as our configure method is just a convenient method instead of coping the endless XML configuration file.

The @Idannotation is inherited from javax.persistence.Id, indicating the member field below is the primary key of current entity. Hence your Hibernate and spring framework as well as you can do some reflect works based on this annotation. for details please check javadoc for Id

The @GeneratedValue annotation is to configure the way of increment of the specified column(field). For example when using Mysql, you may specify auto_increment in the definition of table to make it self-incremental, and then use

@GeneratedValue(strategy = GenerationType.IDENTITY)

in the Java code to denote that you also acknowledged to use this database server side strategy. Also, you may change the value in this annotation to fit different requirements.

1. Define Sequence in database

For instance, Oracle has to use sequence as increment method, say we create a sequence in Oracle:

create sequence oracle_seq;

2. Refer the database sequence

Now that we have the sequence in database, but we need to establish the relation between Java and DB, by using @SequenceGenerator:

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

sequenceName is the real name of a sequence in Oracle, name is what you want to call it in Java. You need to specify sequenceName if it is different from name, otherwise just use name. I usually ignore sequenceName to save my time.

3. Use sequence in Java

Finally, it is time to make use this sequence in Java. Just add @GeneratedValue:

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

The generator field refers to which sequence generator you want to use. Notice it is not the real sequence name in DB, but the name you specified in name field of SequenceGenerator.

4. Complete

So the complete version should be like this:

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

Now start using these annotations to make your JavaWeb development easier.

这篇关于注解@Id和@GeneratedValue(strategy = GenerationType.IDENTITY)有什么用?为什么生成类型是身份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆