ORMLite-强制读取的对象具有相同的标识 [英] ORMLite - force read objects to have same identity

查看:128
本文介绍了ORMLite-强制读取的对象具有相同的标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ORMLite读取对象的层次结构.它的形状像一棵树,父母有一个@ForeignCollection0+个子代,每个子代都用@DatabaseField(foreign=true)引用了它的父代.我正在阅读并保存整个层次结构.

I'm reading a hierarchy of objects with ORMLite. It is shaped like a tree, parents have a @ForeignCollection of 0+ children, and every child refers to its parent with @DatabaseField(foreign=true). I'm reading and saving the whole hierarchy at once.

由于我是ORM和ORMLite的新手,所以我不知道当读取数据库中具有相同ID 的对象时,它们不会是创建为与 Same Identity 相同的对象,但作为多个具有相同ID 的重复对象.意思是,我现在正面临一个问题(假设->"代表指")A-> B-> C!= C-> B-> A.

As I'm new to ORM in general, and to ORMLite as well, I didn't know that when objects with the same ID in the database are read, they won't be created as the actually same object with the same Identity, but as several duplicates having the same ID. Meaning, I'm now facing the problem that (let's say "->" stands for "refers to") A -> B -> C != C -> B -> A.

我正在考虑通过提供的DAO手动读取它们并通过它们的ID将它们放在一起来解决此问题,确保具有相同ID的对象具有相同的身份.

I was thinking to solve the problem by manually reading them through the provided DAOs and puting them together by their ID, assuring that objects with the same ID have the same identity.

是否有ORMLite本机解决方案?如果是,那是什么,如果不是,那么解决此问题的常用方法是什么?这是ORM的普遍问题吗?它有名字吗(我想进一步了解它)?

Is there are ORMLite-native way of solving this? If yes, what is it, if not, what are common ways of solving this problem? Is this a general problem of ORM? Does it have a name (I'd like to learn more about it)?

我的层次结构是,一栋建筑物包含几层,每一层都知道其所在的建筑物,每一层包含几个区域,每一区都知道其所在的楼层.

My hierarchy is so that one building contains several floors, where each floor knows its building, and each floor contains several zones, where every zone knows its floor.

推荐答案

不幸的是,还没有ORMLite本机的方法可以解决此问题.因此,更复杂的ORM系统(例如Hibernate)具有缓存层,这些缓存层专门存在于 . ORMLite没有缓存层,因此它不知道它只是最近"返回了一个具有相同ID的对象.这是Hibernate缓存的文档:

Unfortunately there is not a ORMLite-native way of solving this problem. More complex ORM systems (such as Hibernate) have caching layers which are there specifically for this reason. ORMLite does not have a cache layer so it doesn't know that it just returned an object with the same id "recently". Here's documentation of Hibernate caching:

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html

但是,ORMLite被设计为精简版,并且缓存层违反了该名称IMO.我在ORMLite中看到的关于您的问题的唯一[不幸的]解决方案是做您正在做的事情-根据ID重建对象树.如果您提供有关层次结构的更多详细信息,我们也许可以提供更具体的帮助.

However, ORMLite is designed to be Lite and cache layers violate that designation IMO. About the only [unfortunate] solution that I see to your issue in ORMLite is to do what you are doing -- rebuilding the object tree based on the ids. If you give more details about your hierarchy we may be able to help more specifically.

因此,在考虑了更多@riwi案例之后,我想到,如果您的建筑物包含楼层集合,则没有理由不能将集合中每个楼层上的建筑物对象设为用父建筑对象设置.咄. ORMLite具有实现此目标所需的所有信息.我实现了此行为,并在4.24版中将其发布.

So after thinking about your case a bit more @riwi, it occurred to me that if you have a Building that contains a collection of Floors, there is no reason why the Building object on each of the Floors in the collection cannot be set with the parent Building object. Duh. ORMLite has all of the information it needs to make this happen. I implemented this behavior and it was released in version 4.24.

ORMLite 版本4.26开始,我们在对象缓存中添加了初始版本,可以支持要求的功能.这是文档:

As of ORMLite version 4.26 we added an initial take on an object-cache that can support the requested features asked for. Here are the docs:

http://ormlite.com/docs/object-cache

这篇关于ORMLite-强制读取的对象具有相同的标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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