Table policy (Table strategy)
In this strategy, the persistence engine (persistence engine) uses a table in a relational database to generate the primary key. This strategy portability is better because all relational databases support this strategy. Different Java EE application servers use different persistence engines.
Here is an example to illustrate the use of this table generation strategy:
Listing 1.Table Generation Policy
@Entity public
class Primarykey_table {
@TableGenerator (name = "Pk_seq",
Table = "Sequence_table"
, Pkcolumnname = "Sequence_name",
valuecolumnname = "Sequence_count")
@Id
@GeneratedValue ( Strategy =generationtype.table,generator= "PK_SEQ")
private Long ID;
Getters and setters
//For convenience, there is no other column in the class except for a required primary key column, which is similar later
}
First, @javax. Persistence.tablegenerator is used in Listing 1 to specify a table to generate a primary key, which can be used on an entity class or as an example in a primary key field.
In this case, the name attribute "Pk_seq" indicates the generator, which means that the name of the generator is pk_seq. This table property indicates which table is used to store the generated primary key, in this case, "sequence_table" is used for storing the primary key, and there is a corresponding sequence_table table in the database. Where the Pkcolumnname property is used to specify the primary key of the table in the generator, which is the name of the primary key of the sequence_table table. Property valuecolumnname Specifies that the column is the value used to store the last generated primary key.
You can also use the default Table provided by the persistence engine, for example:
Listing 2. Use the table builder of the exact province
public class PK implements Serializable {
private static final long serialversionuid = 1L;
@Id
@GeneratedValue (strategy = generationtype.table)
private Long Id;
Getters and Setters
}
Different persistence engines have different defaults, and in glass fish, the default value for the Table property is SEQUENCE, and the Pkcolumnname property default is the Seq_name,,valuecolumnname property's default value is Seq_count
Sequence strategy
Some databases, such as Oralce, have a built-in mechanism called "sequence" (sequence) to generate primary keys. To invoke this sequence, you need to use @javax. Persistence.sequencegenerator this annotation.
For example
Listing 3.sequence policy generates primary key
Public
class Pk_sequence implements Serializable {
private static final long serialversionuid = 1L;
@SequenceGenerator (name= "Pk_seq_tbl", Sequencename= "Pk_seq_name")
@Id
@GeneratedValue (strategy = Generationtype.sequence,generator= "Pk_seq_tbl")
private Long ID;
Getters and Setters
}
The @javax. Persistence.sequencegenerator is defined as follows:
Listing 4. Definition of @SequenceGenerator annotations
@Target (value = {elementtype.type, Elementtype.method, Elementtype.field})
@Retention (value = retentionpolicy.runtime) Public
@interface sequencegenerator {public
String name ();
Public String sequencename () Default "";
Public String catalog () default "";
Public String schema () default "";
public int InitialValue () default 1;
public int allocationsize () default
It can be seen from the definition that this annotation can be used on a class or On methods and fields where the Name property specifies the generator to use; sequencename Specifies the sequence in the database; InitialValue specifies the initial value of the sequence, and the @ Tablegenerator differs from its default value 1;allocationsize specifies the cache size when the persistence engine (persistence engine) reads a value from the sequence (sequence), and its default value is 50.