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;它解决了你的问题。
Exception2.
@Column(name = “bar”, nullable = false, columnDefinition = “bigint(20) default 0″) private int bar;it solves your problem.
更多推荐
发布评论