mysql identity 注解,注解@Id和@GeneratedValue(strategy = GenerationType.IDENTITY)有什么用?为什么生成类型是身份?...

@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

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值