JPA实体ID - 长或长 [英] JPA entity id - long or Long

查看:200
本文介绍了JPA实体ID - 长或长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您的实体的ID应该是 long (原始类型)还是 ??


  • ID是我表中的主键,在数据库中永远不会为'null'。
  • 我的同事建议使用Object Type Long。
  • Hibernate Reverse Engeneering工具默认生成一个长为id的基本类型。



要选择什么? long或long?

  @Entity 
@Table(name =COUNTRY)
public class CountryEntity实现java.io.Serializable {
private static final long serialVersionUID = 1L;

@Id
@Column(name =ID)
私人长ID;
@Column(name =NAME)
私人字符串名称;
@Column(name =CURRENCY)
私人字符串货币;
@Column(name =PEOPLE)
私人长者;
@Column(name =SIZE)
私人长尺寸;

Public CountryEntity(){
}


解决方案

我认为让Long更好,因为通过检查 null 值来检查实体是否具有持久性标识更为正确(在MySQL中,您可以有一个值为0的ID)。像Spring这样的一些库基于逻辑中的Long类型(默认情况下)。请参阅实现



小原始物的优点:占用空间少一些。



PS:两者都是正确的&支持根据JPA规范和这个问题的答案是某种基于意见的。

Should the ID of your Entity be long (primitive type) or Long (object type)?

  • The ID is the primary key of my table and is never 'null' in the database.
  • My co-workers suggest to use an Object Type Long.
  • Hibernate Reverse Engeneering tool generates a primitive type long for id by default.

What to choose? long or Long?

@Entity
@Table(name = "COUNTRY")
public class CountryEntity implements java.io.Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "ID")
    private long id;
    @Column(name = "NAME")
    private String name;
    @Column(name = "CURRENCY")
    private String currency;
    @Column(name = "PEOPLE")
    private Long people;
    @Column(name = "SIZE")
    private Long size;

    public CountryEntity() {
    }

解决方案

I consider having Long is better, as is more correct to check whether an entity has persistent identity by checking against the null value (in MySQL you can have an ID of value 0). Also some libraries like Spring base on an ID of type Long (by default) in their logic. See this implementation for an example.

The small advantage of the primitive: it takes a bit less space.

PS:Both are correct & supported according to the JPA specification and the answers to this question are somehow opinion-based.

这篇关于JPA实体ID - 长或长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆