加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

@GeneratedValue源码解析

发布时间:2020-12-14 06:32:31 所属栏目:Java 来源:网络整理
导读:JPA要求每一个实体必须有且只有一个主键,而@GeneratedValue提供了主键的生成策略,这就是@GeneratedValue注解存在的意义。本文将浅析@GeneratedValue的源码。 @GeneratedValue的源码如下: span style="color: #0000ff;"import span style="color: #000000;

JPA要求每一个实体必须有且只有一个主键,而@GeneratedValue提供了主键的生成策略,这就是@GeneratedValue注解存在的意义。本文将浅析@GeneratedValue的源码。

@GeneratedValue的源码如下:

<span style="color: #0000ff;">import<span style="color: #000000;"> java.lang.annotation.Retention;
<span style="color: #0000ff;">import
<span style="color: #000000;"> java.lang.annotation.Target;

<span style="color: #0000ff;">import <span style="color: #0000ff;">static<span style="color: #000000;"> java.lang.annotation.ElementType.FIELD;
<span style="color: #0000ff;">import <span style="color: #0000ff;">static<span style="color: #000000;"> java.lang.annotation.ElementType.METHOD;
<span style="color: #0000ff;">import <span style="color: #0000ff;">static<span style="color: #000000;"> java.lang.annotation.RetentionPolicy.RUNTIME;
<span style="color: #0000ff;">import <span style="color: #0000ff;">static<span style="color: #000000;"> javax.persistence.GenerationType.AUTO;

<span style="color: #008000;">/**<span style="color: #008000;">

  • Provides for the specification of generation strategies for the
  • values of primary keys.
  • The GeneratedValue annotation

  • may be applied to a primary key property or field of an entity or
  • mapped superclass in conjunction with the {<span style="color: #808080;">@link<span style="color: #008000;"> Id} annotation.
  • The use of the GeneratedValue annotation is only
  • required to be supported for simple primary keys. Use of the
  • GeneratedValue annotation is not supported for derived
  • primary keys.
  • Example 1:
  • &#064;Id
  • &#064;GeneratedValue(strategy=SEQUENCE,generator="CUST_SEQ")
  • &#064;Column(name="CUST_ID")
  • public Long getId() { return id; }
  • Example 2:
  • &#064;Id
  • &#064;GeneratedValue(strategy=TABLE,generator="CUST_GEN")
  • &#064;Column(name="CUST_ID")
  • Long id;
  • <span style="color: #808080;">@see<span style="color: #008000;"> Id
  • <span style="color: #808080;">@see<span style="color: #008000;"> TableGenerator
  • <span style="color: #808080;">@see<span style="color: #008000;"> SequenceGenerator
  • <span style="color: #808080;">@since<span style="color: #008000;"> Java Persistence 1.0
    <span style="color: #008000;">*/<span style="color: #000000;">
    @Target({METHOD,FIELD})
    @Retention(RUNTIME)

<span style="color: #0000ff;">public @<span style="color: #0000ff;">interface<span style="color: #000000;"> GeneratedValue {

</span><span style="color: #008000;"&gt;/**</span><span style="color: #008000;"&gt;
 * (Optional) The primary key generation strategy
 * that the persistence provider must use to
 * generate the annotated entity primary key.
 </span><span style="color: #008000;"&gt;*/</span><span style="color: #000000;"&gt;
GenerationType strategy() </span><span style="color: #0000ff;"&gt;default</span><span style="color: #000000;"&gt; AUTO;

</span><span style="color: #008000;"&gt;/**</span><span style="color: #008000;"&gt;
 * (Optional) The name of the primary key generator
 * to use as specified in the {</span><span style="color: #808080;"&gt;@link</span><span style="color: #008000;"&gt; SequenceGenerator}
 * or {</span><span style="color: #808080;"&gt;@link</span><span style="color: #008000;"&gt; TableGenerator} annotation.
 * <p> Defaults to the id generator supplied by persistence provider.
 </span><span style="color: #008000;"&gt;*/</span><span style="color: #000000;"&gt;
String generator() </span><span style="color: #0000ff;"&gt;default</span> ""<span style="color: #000000;"&gt;;

}

@Target({METHOD,FIELD})说明该注解可以用于方法声明和域声明。

@Retention(RUNTIME)说明该注解在VM运行期间也可以保留,可以通过反射机制读取注解的信息。

如果不是很清楚,可参考中有对元注解的介绍。

@GeneratedValue注解有两个属性,分别是strategy和generator,其中generator属性的值是一个字符串,默认为"",其声明了主键生成器的名称。

查看GenerationType的源码,可以看到定义了四种主键生成策略:TABLE,SEQUENCE,IDENTITY,AUTO。

<span style="color: #008000;">/**<span style="color: #008000;">

  • Defines the types of primary key generation strategies.

  • <span style="color: #808080;">@see<span style="color: #008000;"> GeneratedValue

  • <span style="color: #808080;">@since<span style="color: #008000;"> Java Persistence 1.0
    <span style="color: #008000;">*/
    <span style="color: #0000ff;">public <span style="color: #0000ff;">enum<span style="color: #000000;"> GenerationType {

    <span style="color: #008000;">/**<span style="color: #008000;">

    • Indicates that the persistence provider must assign
    • primary keys for the entity using an underlying
    • database table to ensure uniqueness.
      <span style="color: #008000;">*/<span style="color: #000000;">
      TABLE,<span style="color: #008000;">/**<span style="color: #008000;">
    • Indicates that the persistence provider must assign
    • primary keys for the entity using a database sequence.
      <span style="color: #008000;">*/<span style="color: #000000;">
      SEQUENCE,<span style="color: #008000;">/**<span style="color: #008000;">
    • Indicates that the persistence provider must assign
    • primary keys for the entity using a database identity column.
      <span style="color: #008000;">*/<span style="color: #000000;">
      IDENTITY,<span style="color: #008000;">/**<span style="color: #008000;">
    • Indicates that the persistence provider should pick an
    • appropriate strategy for the particular database. The
    • AUTO generation strategy may expect a database
    • resource to exist,or it may attempt to create one. A vendor
    • may provide documentation on how to create such resources
    • in the event that it does not support schema generation
    • or cannot create the schema resource at runtime.
      <span style="color: #008000;">*/<span style="color: #000000;">
      AUTO
      }

TABLE

使用数据库表来生成主键。该策略一般与另外一个注解一起使用@TableGenerator,@TableGenerator注解指定了生成主键的表(可以在实体类上指定也可以在主键字段或属性上指定),然后JPA将会根据注解内容自动生成一张表作为序列表(或使用现有的序列表)。如果不指定序列表,则会生成一张默认的序列表,表中的列名也是自动生成,数据库上会生成一张名为sequence的表(SEQ_NAME,SEQ_COUNT)。序列表一般只包含两个字段:第一个字段是该生成策略的名称,第二个字段是该关系表的最大序号,它会随着数据的插入逐渐累加。该策略的好处就是不依赖于外部环境和数据库的具体实现,便于移植;但由于其不能充分利用数据库的特性,所以不会优先使用。

= GenerationType.TABLE,generator = "roleSeq"= "roleSeq",allocationSize = 1,table = "seq_table",pkColumnName = "seq_id",valueColumnName = "seq_count" Long id;

SEQUENCE

使用序列来生成主键。 该策略的不足之处正好与TABLE相反,由于只有部分数据库(Oracle,PostgreSQL,DB2)支持序列对象,所以该策略一般不应用于其他数据库。需要注意,MySQL不支持这种主键生成策略。该策略一般与另外一个注解一起使用@SequenceGenerator,@SequenceGenerator注解指定了生成主键的序列.然后JPA会根据注解内容创建一个序列(或使用一个现有的序列)。如果不指定序列,则会自动生成一个序列SEQ_GEN_SEQUENCE。

= GenerationType.SEQUENCE,generator = "menuSeq"= "menuSeq",initialValue = 1,sequenceName = "MENU_SEQUENCE" Long id;

IDENTITY

使用数据库ID自增长的方式来生成主键。 该策略在大部分数据库中都提供了支持(指定方法或关键字可能不同),但还是有少数数据库(Oracle)不支持,所以可移植性略差。

= Long id;

AUTO

自动选择合适的主键生成策略。该策略比较常用,也是默认选项,@GeneratedValue(strategy = GenerationType.AUTO)相当于@GeneratedValue。

Long id;

参考:。这篇介绍的挺详细的,推荐一下。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读