【发布时间】:2020-11-17 14:45:22
【问题描述】:
我收到“org.springframework.dao.DataIntegrityViolationException:无法执行语句;SQL [n/a];约束 [null];嵌套异常是 org.hibernate.exception.ConstraintViolationException:无法执行语句”异常同时将对象保存到存储库中。
要求:计划只有一个定价对象,所以我添加了@OneToOne 映射。下面是预期的表结构。
Plan table
-------------
id | planname
Cpricing table
----------
id | cdata | plan_id(fk)
我在实体类中有以下代码更改:
Plan {
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "pl")//created one to one mapping between the cPricing
private CPricing priceInfo;
}
CPricing {
@OneToOne(fetch = FetchType.LAZY) //created one to one mapping between the plan
@JoinColumn(name = "plan_id", nullable = false, unique = true)
private Plan pl;
}
尝试保存包含 CPricing 对象的 Plan 对象时引发异常。
映射是否正确?
【问题讨论】:
-
能否请您分享整个堆栈跟踪,您的实体类中的一个字段似乎是 null 导致问题持续存在。
标签: java database hibernate spring-data-jpa hibernate-mapping