java 实体的 id 字段的 Long 与 Integer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17398192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:52:50  来源:igfitidea点击:

Long versus Integer for the id field of java entities

javajpapersistence

提问by osh

Jpa entity generator assigns Integer type to the id fields of my entities. The corresponding attribute/column in my DB is of type serial ( yep postgres). I would also assign the integer type to my entities id fields. But i have seen usage of Long getId()on this page. I have also seen this type of type assigning on the geomajas examples. Is there any gotcha when it comes to using Integer? I mean, yeah you have to be careful with integer that the id is not below 0 but at the same time you also have to make sure that your Long Id is not bigger than 2,147,483,647. So what's deal here?

Jpa 实体生成器将 Integer 类型分配给我的实体的 id 字段。我的数据库中相应的属性/列的类型是 serial ( yep postgres)。我还将整数类型分配给我的实体 id 字段。但是我Long getId()这个页面上看到了 的用法。我还在 geomajas 示例中看到了这种类型的分配。使用时有什么问题Integer吗?我的意思是,是的,您必须注意 id 不低于 0 的整数,但同时您还必须确保您的 Long Id 不大于 2,147,483,647。那么这里有什么关系呢?

EDIT: I was making a confusion between Longand unsigned integer so i guess what i was thinking was "unsigned integer versus Integerfor the id field of java entities" which is nonsense now that my confusion between long and unsigned integer is gone. My bad. Thank you for your answers and comments. I guess if i would have used bigserial jpa entity generator would have used Long too.

编辑:我在Long无符号整数和无符号整数之间产生了混淆,所以我想我在想的是“无符号整数与Integerjava实体的id字段”,现在我在长整数和无符号整数之间的混淆消失了,这是无稽之谈。我的错。感谢您的回答和评论。我想如果我使用 bigserial jpa 实体生成器也会使用 Long 。

回答by usmansamie

Longhas a much bigger capacity than Integerdata type, so if you are not sure what length your data is going to be, its better to be using Longtype data...

Long具有比Integer数据类型大得多的容量,因此如果您不确定数据的长度,最好使用Long类型数据...

On the other hand, since Longhas a bigger capacity it uses extra space and if you are sure that your data will not be bigger than the Integerdata type then using Longjust makes your code inefficient

另一方面,由于Long容量更大,它会使用额外的空间,如果您确定您的数据不会大于Integer数据类型,那么使用Long只会使您的代码效率低下

回答by Glen Best

Jpa entity generator assigns Integer type to the id fields of my entities

Jpa 实体生成器将整数类型分配给我的实体的 id 字段

You set the type of the id field, the JPA generator fills it with unique values. You are free to use Integer or Long.

您设置 id 字段的类型,JPA 生成器用唯一值填充它。您可以自由使用 Integer 或 Long。

Is there any gotcha when it comes to using Integer?

在使用 Integer 时有什么问题吗?

Not really, the only difference between Integer & Long is the number of bits (64 v 32) and permissable range:

不是真的,Integer 和 Long 之间的唯一区别是位数 (64 v 32) 和允许的范围:

Integer:             -2,147,483,648  to  2,147,483,647
Long:    -9,223,372,036,854,775,808  to  9,223,372,036,854,775,807

The generator will assign values to the Id field. You can control the initial generated value: for @SequenceGenerator and @TableGenerator, set the initialValue attribute; for Identity generator, modify database DDL definition for the Identity column.

生成器将为 Id 字段分配值。您可以控制初始生成值:对于@SequenceGenerator 和@TableGenerator,设置initialValue 属性;对于身份生成器,修改身份列的数据库 DDL 定义。

Simply determine (maximum number of Ids generated per week by your app) x (maximum number of weeks your app can be "live"). If the result is less than, say, 1,500,000,000 (giving a safety margin), feel free to use Integer; otherwise use Long.

只需确定(您的应用程序每周生成的最大 ID 数)x(您的应用程序可以“上线”的最大周数)。如果结果小于,比如说,1,500,000,000(给出一个安全余量),请随意使用 Integer;否则使用 Long。

回答by Steven Spungin

The biggest 'gotcha' is that when you query for an entity, you need to use the EXACT TYPE, or the implementation with throw an exception. And this is a RUNTIME exception.

最大的“问题”是,当您查询实体时,您需要使用 EXACT TYPE,或者使用抛出异常的实现。这是一个 RUNTIME 异常。

entityManager.find(MyClass.class, intVal); // fails if type is long
entityManager.find(MyClass.class, longVal); // fails if type is integer

I make the case that for maintainability, consistency among your entities is as important as any memory considerations.

我认为为了可维护性,实体之间的一致性与任何内存考虑一样重要。

To further this argument, consider that the 'size' does not effect the packets to/from the database.

为了进一步讨论这个论点,请考虑“大小”不会影响进出数据库的数据包。