Java @列(nullable=false)在应用程序级别验证空检查
从我的理解来看,nullable=false(比如对于列customerid)只对使用hibernate创建模式有用,并且在持久化之前不应该执行任何类型的验证。我的数据库列没有这样的约束(它可以接受null值),而将实体持久化为customerid null,则会出现此错误Java @列(nullable=false)在应用程序级别验证空检查,java,spring-boot,hibernate,jpa,Java,Spring Boot,Hibernate,Jpa,从我的理解来看,nullable=false(比如对于列customerid)只对使用hibernate创建模式有用,并且在持久化之前不应该执行任何类型的验证。我的数据库列没有这样的约束(它可以接受null值),而将实体持久化为customerid null,则会出现此错误 Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value :b a
Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value :b
at org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:111) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:55) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at
在我将spring引导版本更新到2.4.2之后,这个错误开始出现
实体类
public class FaceIndexResponse extends AuditEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name="customer_id",nullable = false)
private String customerId;
@Column(name="application_id")
private String appid;}
我试图保存上述实体的服务类
public IndexFacesResult handle(FaceIndexModel model) throws IOException{
FaceIndexResponse response=new FaceIndexResponse();
response.setAppid(model.getApplicationId);
faceIndexResponseJpa.save(response);
}
Table "public.face_index_response"
Column | Type | Collation | Nullable | Default
----------------+-----------------------------+-----------+----------+----------------------------------
id | integer | | not null | generated by default as identity
customer_id | character varying(64) | | |
application_id | character varying(64) | | |
我不能在上面发表评论(rep不够高),也没有足够多的人知道这会回答这个问题,但是
看看这里:和
如果您有此设置,Hibernate将在DB之前检查null
spring.jpa.properties.hibernate.check_nullability
所以我认为您已经设置了该属性。您可以共享引发此异常的代码吗?我尝试使用springJpa保存实体,其中customerid为null。您检查过数据库表吗?再次确认该列在数据库中可为空。确保刷新已打开的任何数据库客户端窗口。数据库表中的“是”列不包含任何约束。奇怪。我一直认为它应该是这样工作的:DB抽象层强制执行它,这样您就不必有一个列约束,谢谢,但我已经尝试过了。我的配置文件中没有这样的属性。早些时候,它工作正常,但是spring启动升级也是hibernate版本,这是一个问题,您是否启用了hibernate验证程序?从SessionFactoryOptionBuilder(Hibernate core)this.checkNullability=cfgService.getSetting(CHECK_NULLABILITY,BOOLEAN,true);从TypeSafeActivator//当Bean验证存在时,在核心级别取消激活非空跟踪,除非用户显式//要求它,如果(cfgService.getSettings().get(Environment.CHECK_NULLABILITY)==null){activationContext.getSessionFactory().getSessionFactoryOptions().setCheckNullability(false)这很有帮助,谢谢