java - 使用@GeneratedValue(strategy=GenerationType.TABLE),sequence_next_hi_value 是一个 int(11) 但我的表有一个 id 列 bigint(20)

标签 java mysql hibernate jpa

我将@Id 与@GeneratedValue(strategy=GenerationType.TABLE) 一起使用,只是检查了hibernate 是否创建了一个表hibernate_sequences,但是sequence_next_hi_value 的列类型是一个int(11)。我有一些具有 bigint(20) 类型的 id 字段的实体(我的意思是表),这行得通吗?当我的表达到我期望的行数时?

谢谢

最佳答案

(...) hibernate has created a table hibernate_sequences, but the column type for the sequence_next_hi_value is an int(11). I have some entities (I mean tables) that have an id field of type bigint(20), will that works ?

是的,这会起作用。

and when my table reach the number of rows I am expecting?

我不确定我是否理解了那部分内容。但如果问题是关于数字用完的问题,这里有一些来自 JPA 维基书中的数字:

Running Out of Numbers

One paranoid delusional fear that programmers frequently have is running out of sequence numbers. Since most sequence strategies just keep incrementing a number it is unavoidable that you will eventually run out. However as long a large enough numeric precision is used to store the sequence id this is not an issue. For example if you stored your id in a NUMBER(5) column, this would allow 99,999 different ids, which on most systems would eventually run out. However if you store your id in a NUMBER(10) column, which is more typical, this would store 9,999,999,999 ids, or one id each second for about 300 years (longer than most databases exist). But perhaps your system will process a lot of data, and (hopefully) be around a very long time. If you store your id in a NUMBER(20) this would be 99,999,999,999,999,999,999 ids, or one id each millisecond for about 3,000,000,000 years, which is pretty safe.

But you also need to store this id in Java. If you store the id in a Java int, this would be a 32 bit number , which is 4,294,967,296 different ids, or one id each second for about 200 years. If you instead use a long, this would be a 64 bit number, which is 18,446,744,073,709,551,616 different ids, or one id each millisecond for about 600,000,000 years, which is pretty safe.

IMO,默认分配大小为 50 的 int(11) 会给您一些时间。

关于java - 使用@GeneratedValue(strategy=GenerationType.TABLE),sequence_next_hi_value 是一个 int(11) 但我的表有一个 id 列 bigint(20),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4019625/

相关文章:

java - 将 @Mock 注入(inject)另一个 @Mock 或重构?

java - 如何将事务插入到 mysql 中的表中,使其对每个用户都是唯一的

java - Hibernate:如何强制 Hibernate 设置集合类型?

java - Junit测试导致Mysql元数据锁

java - 如何将 CA 证书添加到 cacerts 存储以便它按预期工作?

java - Java 应用程序的多个实例

java - 使用 TableView.getItems() 观察 ObservableList?

mysql - 追踪基本的 1064 错误

php - 使用 count 从两个表中选择总行数

hibernate - 如何在Grails/GORM/Hibernate中执行特定的SQL