[英]spring boot migration from 1.5 to 2.1, hibernate errors
I migrated my spring boot version from 1.5 to 2.1.我将 spring 引导版本从 1.5 迁移到 2.1。 Now started using hibernate version 5.3.12 vs previously 5.0.12.
现在开始使用 hibernate 版本 5.3.12 与之前的 5.0.12。 And now I am seeing lot of previously uncaught hibernate exceptions like:
现在我看到很多以前未捕获的 hibernate 异常,例如:
On @OneToOne mapping annotation because Cascade type was missing:在 @OneToOne 映射注释上,因为缺少 Cascade 类型:
ERROR: update or delete on table violates foreign key constraint
On a @Test when I am trying to get a record by: repository.findById()当我试图通过以下方式获取记录时,在 @Test 上:repository.findById()
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
on old project with hibernate 5.0.12 these errors don't appear, but as soon as I switch to new spring boot project, these start appearing with the only change factor being upgraded spring dependencies and hibernate version. on old project with hibernate 5.0.12 these errors don't appear, but as soon as I switch to new spring boot project, these start appearing with the only change factor being upgraded spring dependencies and hibernate version.
Tried to search on this but couldn't find anything specific about what changed in this hibernate version vs the previous ones.尝试对此进行搜索,但找不到任何关于此 hibernate 版本与以前版本相比发生了什么变化的具体内容。 Has anyone encountered similar issues?
有没有人遇到过类似的问题? any help or hint would be much appreciated since I am stuck on these.
任何帮助或提示将不胜感激,因为我坚持这些。
So, the reason of all of them that you try to serialize your objects out of Service layer.因此,您尝试将对象序列化到服务层之外的所有原因。 (Transactional annotation)
(事务注解)
@Entity
class A {
@OneToOne(fetch = FetchType.LAZY)
private B b;
}
If you try to serialize class A out of service layer, then hibernate session is already closed, and u have a mentioned exception如果您尝试序列化 class A out of service layer,那么 hibernate session 已经关闭,并且您有提到的异常
Check this one https://www.baeldung.com/hibernate-lazy-eager-loading检查这个https://www.baeldung.com/hibernate-lazy-eager-loading
The correct way to fix it, is to assemble DTO (data transfer object) and return it from service layer to controller.修复它的正确方法是组装 DTO(数据传输对象)并将其从服务层返回到 controller。 And afterwards serialize DTO.
然后序列化 DTO。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.