如何将 JaVers 与地图一起使用?
How to use JaVers with Map?
我有两张地图,可以由不同的作者修改。在这个特定的用例中,我有一个限制,我不应该使用 POJO 或域对象,只能使用地图。
@Test
fun `Shadow from a map`() {
val javers = JaversBuilder.javers().build()
val exercise = mutableMapOf("_id" to "1", "title" to "my exercise", "starred" to false)
javers.commit("bob", exercise)
exercise["starred"] = true
javers.commit("bob", exercise)
exercise["title"] = "my great exercise"
exercise["starred"] = true
javers.commit("alice", exercise)
val training = mutableMapOf("_id" to "2", "title" to "my training")
javers.commit("alice", training)
training["title"] = "my great training"
javers.commit("jack", training)
val query: JqlQuery = QueryBuilder.byInstance(exercise).build()
val shadows: List<Shadow<Map<*,*>>> = javers.findShadows(query)
assertThat(shadows.size, `is`(3))
}
测试执行显示如下错误:
MALFORMED_JQL: Invalid JQL query, object passed to byInstance(Object) query should be instance of Entity or ValueObject, got MapType{ baseType: 'class java.util.LinkedHashMap' } - '{_id=1, title=my great exercise, starred=true}'.
Did you mean byInstanceId(Object localId, Class entityClass)?
JaversException MALFORMED_JQL: Invalid JQL query, object passed to byInstance(Object) query should be instance of Entity or ValueObject, got MapType{ baseType: 'class java.util.LinkedHashMap' } - '{_id=1, title=my great exercise, starred=true}'.
Did you mean byInstanceId(Object localId, Class entityClass)?
at org.javers.repository.jql.FilterDefinition$InstanceFilterDefinition.compile(FilterDefinition.java:91)
at org.javers.repository.jql.JqlQuery.compile(JqlQuery.java:129)
at org.javers.repository.jql.QueryCompiler.compile(QueryCompiler.java:19)
at org.javers.repository.jql.ShadowStreamQueryRunner$StreamQuery.<init>(ShadowStreamQueryRunner.java:59)
at org.javers.repository.jql.ShadowStreamQueryRunner.queryForShadowsStream(ShadowStreamQueryRunner.java:34)
at org.javers.repository.jql.QueryRunner.queryForShadowsStream(QueryRunner.java:32)
at org.javers.core.JaversCore.findShadowsAndStream(JaversCore.java:194)
at org.javers.core.JaversCore.findShadows(JaversCore.java:188)
我应该如何构建 JqlQuery 来查找阴影?
您可以比较两个顶级地图,但不能将地图提交到 Javers 存储库(应抛出异常),因为地图没有标识符。
我有两张地图,可以由不同的作者修改。在这个特定的用例中,我有一个限制,我不应该使用 POJO 或域对象,只能使用地图。
@Test
fun `Shadow from a map`() {
val javers = JaversBuilder.javers().build()
val exercise = mutableMapOf("_id" to "1", "title" to "my exercise", "starred" to false)
javers.commit("bob", exercise)
exercise["starred"] = true
javers.commit("bob", exercise)
exercise["title"] = "my great exercise"
exercise["starred"] = true
javers.commit("alice", exercise)
val training = mutableMapOf("_id" to "2", "title" to "my training")
javers.commit("alice", training)
training["title"] = "my great training"
javers.commit("jack", training)
val query: JqlQuery = QueryBuilder.byInstance(exercise).build()
val shadows: List<Shadow<Map<*,*>>> = javers.findShadows(query)
assertThat(shadows.size, `is`(3))
}
测试执行显示如下错误:
MALFORMED_JQL: Invalid JQL query, object passed to byInstance(Object) query should be instance of Entity or ValueObject, got MapType{ baseType: 'class java.util.LinkedHashMap' } - '{_id=1, title=my great exercise, starred=true}'.
Did you mean byInstanceId(Object localId, Class entityClass)?
JaversException MALFORMED_JQL: Invalid JQL query, object passed to byInstance(Object) query should be instance of Entity or ValueObject, got MapType{ baseType: 'class java.util.LinkedHashMap' } - '{_id=1, title=my great exercise, starred=true}'.
Did you mean byInstanceId(Object localId, Class entityClass)?
at org.javers.repository.jql.FilterDefinition$InstanceFilterDefinition.compile(FilterDefinition.java:91)
at org.javers.repository.jql.JqlQuery.compile(JqlQuery.java:129)
at org.javers.repository.jql.QueryCompiler.compile(QueryCompiler.java:19)
at org.javers.repository.jql.ShadowStreamQueryRunner$StreamQuery.<init>(ShadowStreamQueryRunner.java:59)
at org.javers.repository.jql.ShadowStreamQueryRunner.queryForShadowsStream(ShadowStreamQueryRunner.java:34)
at org.javers.repository.jql.QueryRunner.queryForShadowsStream(QueryRunner.java:32)
at org.javers.core.JaversCore.findShadowsAndStream(JaversCore.java:194)
at org.javers.core.JaversCore.findShadows(JaversCore.java:188)
我应该如何构建 JqlQuery 来查找阴影?
您可以比较两个顶级地图,但不能将地图提交到 Javers 存储库(应抛出异常),因为地图没有标识符。