Hibernate ID generator "increment" by using annotation
Solution 1
Hibernate implements JPA and uses the JPA id generation strategy.
Check the documentation here for 4.3 : http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/ : Section5.1.2.2. Identifier generator
Hibernate also provides @GenericGenerator which can be used to configure Hibernate specific generator by passing in strategy attribute
Solution 2
For Hibernate 4.x
you can find 4 types of Generation Types
GeneratorType.AUTO - This is the default strategy and is portable across different databases. Hibernate chooses the appropriate ID based on the database.
GeneratorType.IDENTITY - This setting is based on the identity provided by some databases; it is the respon‐ sibility of the database to provide a unique identifier.
GeneratorType.SEQUENCE - Some databases provide a mechanism of sequenced numbers, so this setting will let Hibernate use the sequence number.
GeneratorType.TABLE - Sometimes the primary keys have been created from a unique column in another table. In this case, use the TABLE generator.
With the Annotations:
If the ID Generation Strategy is NOT SET
it means you are using the AUTO Strategy
.
To use the others, annotate like:
@Entity(name = "TBL_EMPLOYEE")
public class Employee {
@Id
@Column(name="ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int employeeId =0;
...
}
or
public class Employee {
@Id
@Column(name="EMPLOYEE_ID")
@GeneratedValue (strategy= GenerationType.SEQUENCE, generator="empSeqGen")
@SequenceGenerator(name = "empSeqGen", sequenceName = "EMP_SEQ_GEN")
private int employeeId =0;
...
}
or
public class Employee {
@Id
@Column(name="ID")
@GeneratedValue (strategy= GenerationType.TABLE, generator="empTableGen")
@TableGenerator(name = "empTableGen", table = "EMP_ID_TABLE")
private int empoyeeId =0;
...
}
You can also use Composite Identifiers
, in this case I recommend you go for the Book Just Hibernate
.
RamValli
Software Engineer who still trying to figure out whats that Software means ! :D #SOreadytohelp
Updated on June 04, 2022Comments
-
RamValli almost 2 years
As per the Hibernate Developer guide 3.3 here, Hibernate provides support in many ways for generating Identifiers. But this is by using XML based mapping. [1] How to do the same by using Annotations?
Especially I'm interested in 'increment' type. The closest thing that i found is using@GeneratedValue(strategy=GenerationType.AUTO)
. But this is a JPA based strategy.
How can I use Hibernate based using Annotations?
And Even this information is not present in the Hibernate Developer Guide of version 4.3! Any particular reason for this?UPDATE
I'm very well aware of the four strategies which is from JPA. I'm interested in the other types which Hibernate provides. Likehilo
,increment
, and so on. In documentation this is done by using XML configurations.Is there any way to use it with Annotations? -
RamValli over 8 yearsI'm quoting from the hibernate Dev Guide "These are the four standard JPA generators. Hibernate goes beyond that and provide additional generators or additional options as we will see below". And below it talks about Other types of generators which hibernate provides. here My question is about this!
-
Gaurav over 8 yearsCan you check if GenericGenerator annotation works for you. docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/annotations/…
-
RamValli over 8 yearsGenericGenerator annotation works like a charm! That is what i was looking for! Thanks mate! Kindly update your Answer.