admin管理员组

文章数量:1308109

If I have @GeneratedValue(strategy = GenerationType.IDENTITY) above my id in an Hibernate entity, should I use only SERIAL or INTEGER with manually set up sequence for it? Is it possible to use default INTEGER or BIGINT? If it's not possible, then why? I thought strategy = GenerationType.IDENTITY means that Hibernate will care about generating id in case of its nullability, not database.

I'm using PostgreSQL.

If I have @GeneratedValue(strategy = GenerationType.IDENTITY) above my id in an Hibernate entity, should I use only SERIAL or INTEGER with manually set up sequence for it? Is it possible to use default INTEGER or BIGINT? If it's not possible, then why? I thought strategy = GenerationType.IDENTITY means that Hibernate will care about generating id in case of its nullability, not database.

I'm using PostgreSQL.

Share Improve this question edited Feb 3 at 9:03 Mark Rotteveel 109k229 gold badges156 silver badges220 bronze badges asked Feb 2 at 19:04 lowkkidlowkkid 131 silver badge4 bronze badges 1
  • Isn't postgreSQL SERIAL just a integer linked to a sequence? danieleteti.it/post/postgresql-identities-vs-serials I'm not sure what you are looking for, but it doesn't seem to be a concrete problem and more opinion based. Does either method give you issues? GenerationType.IDENTITY tells JPA that the database is responsible for ID generation, and that it is done after the insert - so it needs to understand how the DB can return the generated ID which is sometimes non-standard. Not all databases are supported by all JPA providers. Sequence allows preallocation – Chris Commented Feb 3 at 17:49
Add a comment  | 

1 Answer 1

Reset to default -1

You can use either of them. However, using SERIAL is always a better option for columns like ID because it can automatically increment the IDs which is really helpful when the ID column is left blank or null or during database insertions. INTEGER OR BIGINT(without the attached sequence) cannot handle/support this.

本文标签: