JPA和Hibernate中的@Id批注 [英] @Id annotation in JPA and Hibernate

查看:64
本文介绍了JPA和Hibernate中的@Id批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我不想自动生成主键,那么我想提供ID,并以表的第一列作为主键.

If I don't want to automatically generate the primary Key, instead, I want to supply the ID with the first column of the table as primary key.

2 A
4 B
7 D
13 E

...

我希望第一列2,4、7、13为表的主键.我应该只使用@Id做注释吗?

I want the first column 2,4, 7, 13 to be the primary key of the table. Should I just use @Id to do the annotation?

@Entity
public class Code {
   @Id
   @Column(unique=true)
   private int id;

   ...
}

或者,如果使用@Id,在这种情况下,主键将始终自动生成,而不是使用第一列吗?

Or, if @Id is used, the primary key will be always automatically generated, instead of using the first column, in this case?

推荐答案

@Id仅声明主键.它不会插入生成的值.如果使用@GeneratedValue,它将生成该字段的值.

@Id will only declare the primary key. it will not insert generated value. if you use @GeneratedValue then it will generate the value of the field.

这篇关于JPA和Hibernate中的@Id批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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