JPA映射中的基元类型。(Primitive type in JPA mapping. What if the database column may be NULL?)

编程入门 行业动态 更新时间:2024-10-11 15:14:27
JPA映射中的基元类型。(Primitive type in JPA mapping. What if the database column may be NULL?)

鉴于这个类与JPA映射(使用JPA 1.0和Hibernate):

@Entity public class Foo { private int bar; /* ... */ } 如果我尝试加载一个BAR列设置为NULL的记录,会发生什么情况? 当相应的列为NULL时,我可以指定如何设置bar字段吗?

笔记

我知道这不是最佳做法。 这个问题更多是出于好奇,它受到这种情况的启发:

数据库表是一个临时表:添加NOT NULL约束是不切实际的。 预计数据不正确,我的代码的重点是在将数据加载到“真实”数据库之前验证,清理和/或拒绝数据。 我有一些字段可以接受的默认值。 例如,一个boolean标志,应该默认为false 。

Given this class mapped with JPA (using JPA 1.0 and Hibernate):

@Entity public class Foo { private int bar; /* ... */ } What happens if I try to load a record which has the BAR column set to NULL? Can I specify how to set the bar field when the corresponding column is NULL?

Notes

I know this is not a best practice. The question is more out of curiosity and it is inspired by this situation:

The database table is a staging table: adding a NOT NULL constraint is impractical. Bad data is expected, and the point of my code is to validate, clean up and/or reject data before loading it into the "real" database. I have acceptable default values for some fields. For example, a boolean flag which should default to false.

最满意答案

例外

2。

@Column(name = “bar”, nullable = false, columnDefinition = “bigint(20) default 0″) private int bar;

它解决了你的问题。

Exception

2.

@Column(name = “bar”, nullable = false, columnDefinition = “bigint(20) default 0″) private int bar;

it solves your problem.

更多推荐

本文发布于:2023-08-06 09:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446866.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   type   Primitive   JPA   column

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!