hibernate annotation (annoation) uses id primary key generation strategy

Refer to the original text: http://blog.csdn.net/shendeguang/article/details/47976117

The hibernate annotation (annoation) uses the id primary key generation to mainly use the following annotations: @Id, @GeneratedValue, @GenericGenerator. The role of @GeneratedValue is the default implementation of JPA to customize the primary key generation strategy, @GenericGenerator is hibernate in JPA enhanced on the basis.

Custom primary key generation strategy, implemented by @GenericGenerator. Hibernate has been extended on the basis of JPA, and the unique primary key generation strategy of hibernate can be introduced in the following way, which is added through @GenericGenerator.

For example, JPA standard usage 

@Id
@GeneratedValue(GenerationType.AUTO)

 You can use the following usage specific to hibernate to achieve

@GeneratedValue(strategy = GenerationType.AUTO)
@GenericGenerator(name = "paymentableGenerator", strategy = "native")

 Use @GeneratedValue

1, for mysql, oracle, sqlserver database can be used at the same time

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@GenericGenerator(name = "paymentableGenerator", strategy = "native")

 The name attribute specifies the generator name.

The strategy attribute specifies the class name of the concrete generator.

parameters Get the parameters used by the specific generator specified by strategy.

2. Use with @GenericGenerator not alone

The relationship between these hibernate primary key generation strategies and their respective specific generators is specified in org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory: 

//Constructs a new DefaultIdentifierGeneratorFactory.
public  DefaultIdentifierGeneratorFactory() {
        register( "uuid2", UUIDGenerator.class);
        register( "guid", GUIDGenerator.class);       
        register( "uuid", UUIDHexGenerator.class);        
        register( "uuid.hex", UUIDHexGenerator.class);    
        register( "hilo", TableHiLoGenerator.class);
        register( "assigned", Assigned.class);
        register( "identity", IdentityGenerator.class);
        register( "select", SelectGenerator.class);
        register( "sequence", SequenceGenerator.class);
        register( "seqhilo", SequenceHiLoGenerator.class);
        register( "increment", IncrementGenerator.class);
        register( "foreign", ForeignGenerator.class);
        register( "sequence-identity", SequenceIdentityGenerator.class);
        register( "enhanced-sequence", SequenceStyleGenerator.class);
        register( "enhanced-table", TableGenerator.class);
 }

If you want different tables to use the same primary key generator, you can set the name attribute of his generator to the same, for example: 

@GeneratedValue(name="id1")
@GenericGenerator(name="id1",strategy="identity")

The above twelve strategies, plus native, hibernate supports a total of thirteen generation strategies by default

1、native 

@GeneratedValue(generator = "paymentableGenerator")
@GenericGenerator(name = "paymentableGenerator", strategy = "native")

2、uuid

@GeneratedValue(generator = "paymentableGenerator")
@GenericGenerator(name = "paymentableGenerator", strategy = "uuid")

3、hilo

@GeneratedValue(generator = "paymentableGenerator")
@GenericGenerator(name = "paymentableGenerator", strategy = "hilo")

4、assigned

@GeneratedValue(generator = "paymentableGenerator")
@GenericGenerator(name = "paymentableGenerator", strategy = "assigned")

5、identity

@GeneratedValue(generator = "paymentableGenerator")
@GenericGenerator(name = "paymentableGenerator", strategy = "identity")

6、select 

@GeneratedValue(generator = "paymentableGenerator")
@GenericGenerator(name="select", strategy="select", parameters = { @Parameter(name = "key", value = "idstoerung") })

7、sequence 

@GeneratedValue(generator = "paymentableGenerator")
@GenericGenerator(name = "paymentableGenerator", strategy = "sequence", parameters = { @Parameter(name = "sequence", value = "seq_payablemoney") })

8 、 seqhilo

@GeneratedValue(generator = "paymentableGenerator")
@GenericGenerator(name = "paymentableGenerator", strategy = "seqhilo", parameters = { @Parameter(name = "max_lo", value = "5") })

9、increment

@GeneratedValue(generator = "paymentableGenerator")
@GenericGenerator(name = "paymentableGenerator", strategy = "increment")

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326400198&siteId=291194637