简体   繁体   中英

hibernate @GeneratedValue correct?

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM