views:

389

answers:

2

I set my entity property

@GeneratedValue
Long id;

and I able to generate id for the entity in database. My question is why all the entities are sharing the same incremental number? aren't each table should start counting from zero?

+2  A: 

This is database-dependent. JPA implementations use different ID generators depending on which database system they're using. For example, with Oracle, a single sequence will be created, and that sequence will be used to generate IDs for all entity types. By default, it will not create a sequence for each entity, since there's usually no reason to. The same logic applies to other database systems that use sequences rather than auto-increment columns.

I'm not 100% sure if the JPA API lets you change this behaviour, but I know that Hibernate annotations do. However, you haven't told us which database you're using or which JPA implementation you're using, so I can't give you much more advice than that.

skaffman
i'm using oracle db and using spring-hibenateTemplate
cometta
+1  A: 

It depends on the underlying database. GenerationType is AUTO by default, and Hibernate chooses one of the three variants depending on the database. If you want to use one in particular, set it as attribute of @GeneratedValue

Bozho
i already set @GeneratedValue , so all entity now sharing same sequence. i want each entity has it own sequence
cometta
Try GenerationType.TABLE
Bozho
i get error: could not get or update next value;nested exception is org.hibernate.exception.SQLGrammerException:could not get or update next value
cometta
problem sovled, i added the line <prop key="hibernate.hbm2ddl.auto">update</prop>
cometta