使用2个数据库时Javers ENTITY_INSTANCE_WITH_NULL_ID [英] Javers ENTITY_INSTANCE_WITH_NULL_ID when using 2 databases

查看:271
本文介绍了使用2个数据库时Javers ENTITY_INSTANCE_WITH_NULL_ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Postgres中存储数据(使用JPA Lazy Load)并且在Jasper中存储javers时,我遇到了这个异常"ENTITY_INSTANCE_WITH_NULL_ID"

I have this exception "ENTITY_INSTANCE_WITH_NULL_ID" when I store data in Postgres (using JPA Lazy Load) and I store javers in MongoDB

Spring Boot:1.4.0.RELEASE

Spring Boot: 1.4.0.RELEASE

Sprig Data JPA:1.4.0.RELEASE

Sprig Data JPA: 1.4.0.RELEASE

Javers:2.3.0

Javers: 2.3.0

我调试了一下,发现如果object是一个惰性对象,则ID为null: org.javers.core.metamodel.type.EntityType:88对象cdoId = getIdProperty().get(instance);"

I debugged and saw that ID is null if object is a lazy object: org.javers.core.metamodel.type.EntityType:88 "Object cdoId = getIdProperty().get(instance);"

推荐答案

将对象提交给JaVers时,其先前版本是从JaversRepository加载的,并与当前版本进行比较(您刚刚传递给commit()方法的那个版本) ). 在这种情况下,JaVers使用GlobalId查询查找以前的版本,因此使用TypeName +实体ID. 这就是为什么实体的ID不能为空的原因.

When you commit an object to JaVers, its previous version is loaded from JaversRepository and compared with the current version (that version you have just passed to commit() method). In this case JaVers finds the previous version using GlobalId query so TypeName + entity ID. That's why ID can't be null for Entities.

有两种可能性:

  1. 如果该类中的null ID是正常的(根据您的域模型),则应将其映射为 HibernateUnproxyObjectAccessHook 来进行清理:初始化和取消代理您的域对象.
  1. If null ID is normal in this class (according to your domain model) you should map it as ValueObject in JaVers.
  2. If you are using Hibernate, there is common problem with lazy loading proxies. For certain queries, Hibernate doesn't return your real domain objects but dynamic proxy objects which are essentially empty (hence null ID). This technique maybe looks smart but makes your objects garbage until they are initialized by Hibernate. JaVers provides HibernateUnproxyObjectAccessHook which does the cleaning: initializing and un-proxying of your domain objects.

JaversBuilder.javers().withObjectAccessHook( 新的HibernateUnproxyObjectAccessHook()).build()

JaversBuilder.javers().withObjectAccessHook( new HibernateUnproxyObjectAccessHook()).build()

默认情况下,此挂钩在javers-spring-boot-starter-sql中启用,但在javers-spring-boot-starter-mongo中未启用.如果您使用的是Mongo启动器,请在启用了挂钩的情况下自行创建JaVers bean,请参阅

This hook is enabled by default in javers-spring-boot-starter-sql but not in javers-spring-boot-starter-mongo. If you are using Mongo starter, create a JaVers bean on your own, with the hook enabled, see JaversMongoAutoConfiguration.

这篇关于使用2个数据库时Javers ENTITY_INSTANCE_WITH_NULL_ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆