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
1 Answer
Reset to default -1You 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.
本文标签:
版权声明:本文标题:java - Should I always use SERIAL or INTEGER with manually set sequence with Hibernate GenerationType.IDENTITY strategy? - Stack 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741837498a2400305.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论