java - Difference between @GeneratedValue and @GenericGenerator -


sometimes find them together, alone... other times seem same.

what's difference?

here 3 examples. of different? why can't use @generatedvalue of them?

example 1

@id @generatedvalue(generator="increment") @genericgenerator(name="increment", strategy = "increment")  long id; 

example 2

@id @generatedvalue(strategy=generationtype.sequence) private int userid; 

example 3

@elementcollection @jointable(name="address",    joincolumns=@joincolumn(name="user_id") ) @genericgenerator(name="hilo-gen", strategy="hilo") @collectionid(columns = @column(name="address_id"), generator = "hilo-gen", type = @type(type="long")) collection<addr> listofaddresses = new arraylist<addr>(); 

when using orm necessary generate primary key value.

the @generatedvalue annotation denotes value column, must annotated @id generated. elements strategy , generator on annotation describe how generated value obtained.

there 4 possible values strategy element on @generatedvalue annotation: identity, auto, table , sequence. see more.

so answer part 2 of question, code snippet indicating value of userid obtained through sequence in database.

the generator element of @generatedvalue annotation denotes name of primary key generator. in part1 of question, code snippet indicates generator named increment used obtain primary key value. increment defined in next annotation @genericgenerator. @genericgenerator hibernate annotation used denote custom generator, can class or shortcut generator supplied hibernate. increment shortcut hibernate generator that:

generates identifiers of type long, short or int unique when no other process inserting data same table. not use in cluster.

in third part of question, code uses hilo hibernate generator that:

uses hi/lo algorithm efficiently generate identifiers of type long, short or int, given table , column (by default hibernate_unique_key , next_hi respectively) source of hi values. hi/lo algorithm generates identifiers unique particular database.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -