JPA(休眠)列映射中的原始类和包装类之间有什么区别? [英] What's difference between primitive and wrapper class in JPA (Hibernate) column mappings?

查看:66
本文介绍了JPA(休眠)列映射中的原始类和包装类之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,数据库表中有一个整数列. 然后在Java模型中,它既可以映射为原始int ,也可以映射为 Integer . 我的问题是在这种情况下,int和Integer有什么区别?和性能有关吗? 谢谢!

For instance, there’s an integer column in a database table. Then in java model, it can be mapped both as primitive int and Integer. My question is what's difference between the int and Integer in this case? And performance concern? Thanks!

推荐答案

我倾向于避免使用基元.对于Id属性尤其如此.这样可以通过测试null来检测尚未设置的值.如果使用Java 5或更高版本,则自动装箱可以减轻痛苦(并且不影响性能).而且还有其他属性.正如@skaffman指出的那样,原语不适用于可为空的列,我希望代码尽可能地灵活.

I tend to avoid using primitives. This is especially true for the Id attribute. This makes it possible to detect a not yet set value by testing for null. If using Java 5 or above, auto-boxing takes away the pain (and is not a performance concern). But also for other attributes. As pointed out by @skaffman, primitives are not suitable for nullable columns and I prefer the code to be as flexible as possible.

这篇关于JPA(休眠)列映射中的原始类和包装类之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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