@GeneratedValueソース解析
10124 ワード
JPAでは、エンティティごとにプライマリ・キーが1つしか存在しない必要があります.一方、@GeneratedValueでは、@GeneratedValue注釈が存在する意味で、プライマリ・キーの生成ポリシーが提供されます.本稿では,@GeneratedValueのソースコードについて述べる.
@GeneratedValueのソースコードは次のとおりです.
@GeneratedValueのソースコードは次のとおりです.
/*
* Copyright (c) 2008, 2009, 2011 Oracle, Inc. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
* which accompanies this distribution. The Eclipse Public License is available
* at http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution License
* is available at http://www.eclipse.org/org/documents/edl-v10.php.
*/
package javax.persistence;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
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.
*
* The GeneratedValue
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 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:
*
* @Id
* @GeneratedValue(strategy=SEQUENCE, generator="CUST_SEQ")
* @Column(name="CUST_ID")
* public Long getId() { return id; }
*
* Example 2:
*
* @Id
* @GeneratedValue(strategy=TABLE, generator="CUST_GEN")
* @Column(name="CUST_ID")
* Long id;
*
*
* @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.
* Defaults to the id generator supplied by persistence provider.
*/
String generator() default "";
}
@Target({METHOD,FIELD})は、この がメソッド およびドメイン に できることを す.
@Retention(RUNTIME)は、この がVMの にも され、 によって の を み ることができることを している.
よくわからない は、@Controllerと@Restcontrollerのソースコード を して、メタ について します.
@GeneratedValue には、strategyとgeneratorの2つのプロパティがあります.generatorプロパティの は で、デフォルトは「」で、プライマリ・キー・ジェネレータの が されます.
GenerationTypeのソースコードを すると、TABLE、SEQUENCE、IDENTITY、AUTOの4つのプライマリ・キー ポリシーが されていることがわかります./*
* Copyright (c) 2008, 2009, 2011 Oracle, Inc. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
* which accompanies this distribution. The Eclipse Public License is available
* at http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution License
* is available at http://www.eclipse.org/org/documents/edl-v10.php.
*/
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
* 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.
*/
AUTO
}
TABLE
データベース・テーブルを してプライマリ・キーを します.このポリシーは に、@Table Generatorを の とともに し、@Table Generator はプライマリ・キーを するテーブルを し(エンティティ・クラスで してもよいし、プライマリ・キー・フィールドまたは で してもよい)、JPAは の に づいてシーケンス・リストとして にテーブルを します(または のシーケンス・テーブルを します).シーケンステーブルを しないと、デフォルトのシーケンステーブルが され、テーブル のカラム も され、データベースにsequenceというテーブル(SEQ_NAME,SEQ_COUNT)が されます.シーケンス・テーブルには、 に2つのフィールドしか まれません.1つ のフィールドは ポリシーの で、2つ のフィールドはリレーショナル・テーブルの シーケンス で、データの に って に されます.このポリシーの は、 とデータベースの な に せず、 が であることです.ただし、データベースの を に できないため、 に することはありません. @Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "roleSeq")
@TableGenerator(name = "roleSeq", allocationSize = 1, table = "seq_table", pkColumnName = "seq_id", valueColumnName = "seq_count")
private Long id;
SEQUENCE
シーケンスを してプライマリ・キーを します.このポリシーの はTABLEとは であり、 のデータベース(Oracle,PostgreSQL,DB 2)のみがシーケンスオブジェクトをサポートしているため、このポリシーは に のデータベースでは されません.MySQLでは、このプライマリ・キー ポリシーはサポートされていません.このポリシーは、 に、 の とともに@SequenceGeneratorを する、@SequenceGenerator は、プライマリ・キーを するシーケンスを する.その 、JPAは の に づいてシーケンスを します(または のシーケンスを します).シーケンスを しないと、シーケンスSEQ_が に されます.GEN_SEQUENCE. @Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "menuSeq")
@SequenceGenerator(name = "menuSeq", initialValue = 1, allocationSize = 1, sequenceName = "MENU_SEQUENCE")
private Long id;
IDENTITY
データベースIDの を してプライマリ・キーを します.このポリシーは、ほとんどのデータベースでサポートされています( やキーワードが なる があります)が、 のデータベース(Oracle)ではサポートされていないため、 がやや っています. @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
AUTO
なプライマリ・キー ポリシーを に します.このポリシーは、@GeneratedValue(strategy=GenerationType.AUTO)が@GeneratedValueに するデフォルトオプションとしてよく されます. @Id
@GeneratedValue
private Long id;
:JPAの@GeneratedValue .この はとても しいので、お めします.