zoukankan      html  css  js  c++  java
  • JPA通用策略生成器(@GeneratedValue 四种标准用法为TABLE, SEQUENCE, IDENTITY, AUTO)

    JPA通用策略生成器

    查看JPA的源码可知:

    package javax.persistence;
    
    /** 
     * Defines the types of primary key generation strategies. 
     *
     * @see GeneratedValue
     *
     * @since Java Persistence 1.0
     */
    public enum GenerationType { 
    
        /**
         * Indicates that the persistence provider must assign 
         * primary keys for the entity using an underlying 
         * database table to ensure uniqueness.
         */
        TABLE, 
    
        /**
         * Indicates that the persistence provider must assign 
         * primary keys for the entity using a database sequence.
         */
        SEQUENCE, 
    
        /**
         * Indicates that the persistence provider must assign 
         * primary keys for the entity using a database identity column.
         */
        IDENTITY, 
    
        /**
         * Indicates that the persistence provider should pick an 
         * appropriate strategy for the particular database. The 
         * <code>AUTO</code> 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.
         */
        AUTO
    }

    查看源码知道默认类型为AUTO类型,这是由程序控制的

    package javax.persistence;
    
    import java.lang.annotation.Target;
    import java.lang.annotation.Retention;
    import static java.lang.annotation.ElementType.FIELD;
    import static java.lang.annotation.ElementType.METHOD;
    import static java.lang.annotation.RetentionPolicy.RUNTIME;
    import static javax.persistence.GenerationType.AUTO;
    
    /**
     * Provides for the specification of generation strategies for the
     * values of primary keys. 
     *
     * <p> The <code>GeneratedValue</code> annotation
     * may be applied to a primary key property or field of an entity or
     * mapped superclass in conjunction with the {@link Id} annotation.
     * The use of the <code>GeneratedValue</code> annotation is only
     * required to be supported for simple primary keys.  Use of the
     * <code>GeneratedValue</code> annotation is not supported for derived
     * primary keys.
     *
     * @see Id
     * @see TableGenerator
     * @see SequenceGenerator
     *
     * @since Java Persistence 1.0
     */
    @Target({METHOD, FIELD})
    @Retention(RUNTIME)
    
    public @interface GeneratedValue {
    
        /**
         * (Optional) The primary key generation strategy
         * that the persistence provider must use to
         * generate the annotated entity primary key.
         */
        GenerationType strategy() default AUTO;
    
        /**
         * (Optional) The name of the primary key generator
         * to use as specified in the {@link SequenceGenerator} 
         * or {@link TableGenerator} annotation.
         * <p> Defaults to the id generator supplied by persistence provider.
         */
        String generator() default "";
    }

    JPA提供的四种标准用法为TABLE, SEQUENCE, IDENTITY, AUTO.。
    TABLE:使用一个特定的数据库表格来保存主键。 
    SEQUENCE:根据底层数据库的序列来生成主键,条件是数据库支持序列。 
    IDENTITY:主键由数据库自动生成(主要是自动增长型) 。
    AUTO:主键由程序控制。 

    详情请查看另外一篇,感觉写的很全,如有兴趣的话,可以看看:https://blog.csdn.net/tree_java/article/details/71158122

  • 相关阅读:
    UVa 11988
    UVa 442
    .MySQL数据库技术
    Mysql数据库技术
    JDBC技术
    JDBC技术
    JavaSE编程基础
    JavaSE编程基础
    JavaSE编程基础
    web安全性测试
  • 原文地址:https://www.cnblogs.com/no8g/p/13415609.html
Copyright © 2011-2022 走看看