在Hibernate映射中使用包装器Integer类或int原语 [英] Using wrapper Integer class or int primitive in hibernate mapping

查看:165
本文介绍了在Hibernate映射中使用包装器Integer类或int原语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我工作的公司中,我们讨论了是否应该更好地使用基类的包装类(java.lang.Integer,java.lang.Long)或者是否直接在POJO将实体映射到Hibernate中的表。



这个想法是我们希望这些值在数据库中不为空。



赞成使用原语的参数:


  • 将这些值作为int处理意味着它们在
    中永远不能为null,这样就无法在字段中无意中获得空引用

  • int = 32/64位内存。整数= 16个字节的内存
    并且也比较慢



有利于使用包装器对象的参数:


  • 我们可以在数据库级别添加一个约束来始终防止空值
    到达那里

  • 我们最终可能会误导数据,只要用户没有
    设置一个值并且错误的数据是一个难以捉摸的数据,我们可以
    在数据库中拥有0而不是空值。

  • 对象比基元拥有更多的表现力。我们有空值和
    整数值,所以我们可以使用
    例子的注解(javax.validation.constraints.NotNull)更容易地验证它们。


解决方案

使用包装,让你的生活变得简单。无论如何,您应该在数据库中强制实现可空性。



如果它们在数据库中可以为空,则使用包装器。如果它们不可为空,并且使用包装器,那么如果尝试向数据库中插入一个空值,则会发生异常。



如果数据模型不'不要指定它,然后去约定,一直使用包装。这样人们就不必考虑,或者认为0的值意味着空。



我也会询问你的断言,它的性能会降低。你有没有测量过这个?我的意思是真的衡量它?在与数据库交谈时,需要考虑的事项比16位和32位之间的差异多得多。

只需使用简单一致的解决方案即可。在任何地方都可以使用包装,除非有人给你一个很好的理由(用准确的统计数据)来做其他事情。


In the company that I work for we have this major discussion on whether it should be better to use wrapping classes for primitives (java.lang.Integer, java.lang.Long) or whether to use the primitive types directly in the POJOs that map Entities to Tables in Hibernate.

The idea is that we want these values to not be null in the database.

The arguments in favor of using primitives:

  • Handling these values as int means that they can never be null, in this way making it impossible to inadvertently get a null reference on the field.
  • int=32/64 bits of memory. Integer = 16 bytes of memory and is also slower

The arguments in favor of using wrapper objects:

  • We can add a constraint at the database level to always prevent null values from getting there
  • We can end up with misleading data, we can have 0's instead of nulls in the database whenever the user doesn't set a value and buggy data is a tough catch.
  • Objects have more expressive power than primitives. We have null values and also integer values, so we can validate them easier using annotations for example (javax.validation.constraints.NotNull).

解决方案

Use wrappers, make your life simple.

Your data model should dictate this. You should be enforcing nullability in the database anyway.

If they are nullable in the database, then use wrappers. If they are not nullable, and you use wrappers, then you'll get an exception if you try and insert a null into the database.

If your data model doesn't dictate it, then go for a convention, use wrappers all of the time. That way people don't have to think, or decide that a value of 0 means null.

I would also query your assertion that it would less performant. Have you measured this? I mean really measured it? When you're talking to a database, there are a lot more considerations than the difference between 16 bits and 32 bits.

Just use the simple, consistent solution. Use wrappers everywhere, unless somebody gives you a very good reason (with accurate measured statistics) to do otherwise.

这篇关于在Hibernate映射中使用包装器Integer类或int原语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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