Primitive or wrapper for hibernate primary keys

后端 未结 3 659
眼角桃花
眼角桃花 2020-11-29 02:15

I\'ve been looking at various hibernate tutorials and samples, for their identity/primary key property, some use a Java primitive type, some uses the wrapper type, that is;<

3条回答
  •  孤街浪徒
    2020-11-29 03:07

    I prefer using the wrapper types, because otherwise, when we use primitive types, the primary key id will be set to 0 by default, and 0 could be the value of an actual id column in the corresponding table of the entity, and this can sometimes lead to hard to pinpoint bugs.

    But using a wrapper, the default value of id will be null and a primary key column cannot have a null value, thus you can't update an entity that its id value is not set with a valid value by your application's logic.

提交回复
热议问题