RamValli
RamValli

Reputation: 4475

Hibernate ID generator "increment" by using annotation

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. Like hilo, increment, and so on. In documentation this is done by using XML configurations.Is there any way to use it with Annotations?

Upvotes: 2

Views: 15520

Answers (3)

Mohamed ElAlami
Mohamed ElAlami

Reputation: 75

This worked for me:

@Id
@GeneratedValue(generator = "increment")
private int id;

Upvotes: 1

Gaurav
Gaurav

Reputation: 1567

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

Upvotes: 1

José Mendes
José Mendes

Reputation: 990

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.

Upvotes: 2

Related Questions