26

有个原则就是实体类还是controller入参都应该是 包装类型

 5 years ago
source link: http://www.cnblogs.com/liuge36/p/12631242.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

问题说明

我在使用JPA作为项目的ORM框架的时候,在分页查询中,不管咋样使用查询不出来数据,然后发现Hibernate构建的查询SQL中,在where子句中带上了createTime=0这个条件。这样肯定是过滤掉了全部数据了。

MRv67bB.png!web

疑问:为啥这里Hibernate会在SQL中 自动加上这个createTime=0 这个条件呢?很奇怪~~

直到我看见 在这里实例化的对象中,

aEnErum.png!web

对象的createTime属性我定义的是 int 这个基本数据类型。

恍然大悟

实例化对象的时候,基本数据类型都会有初始值的,比如这里的int初始值 不就是 0嘛,然后Hibernate 就会将对象中不为null的字段,都添加在where 后面作为条件。如下

MfeiEfn.png!web

我把createTime字段的类型改为 包装类型interger 就可以了。

然后,我把这个情况和我的小伙伴讲了,

JnyeuiE.png!webny6RbeN.png!web

我又查了阿里巴巴开发规范,哎~~果真

6V3yEny.png!web

特写此文,让我自己长记长性把!无奈自己比较菜,加油加油吧!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK