Reputation: 34424
I can see many generator classes at http://www.roseindia.net/hibernate/hibernateidgeneratorelement.shtml for generating identifier. (Official list is here: http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/mapping.html#d0e5294)
But if I look at increment
and sequence
, they sound the same, I am not sure what the difference is and which is better?
Upvotes: 3
Views: 20939
Reputation: 158
You can try this lines
@GenericGenerator(name = "increment", strategy = "increment")
@GeneratedValue(generator = "increment")
Upvotes: -1
Reputation: 1815
I'd use the auto generated value. Hibernate will then produce the best strategy for the DB you're using.
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
Also means you can test against a simple in memory DB like HsqlDB without changing your entities.
Upvotes: 2
Reputation: 9868
Sequence
uses the database sequence (eg in Oracle) to generate the id whereas increment
keeps track of id in jvm and increments it within process. Sequence is safer if any other program/person is going to insert into the database table.
To use sequence :
CREATE SEQUENCE mySquence;
)Increment
is less safer solution (but portable, to those db that don't support Sequence) and good for testing and\or getting started. There are better ways (eg native/identity etc) than using Increment
for use in production.
Upvotes: 15