ORMLite和惰性集合的问题 [英] Problems with ORMLite and lazy collections

查看:295
本文介绍了ORMLite和惰性集合的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的android项目中使用ormlite。我有两个类

I am using ormlite in my android project. I have two classes

@DatabaseTable(tableName = "usershows")
public class UserShow {
    @DatabaseField(id = true)
    private Integer showId;

    @ForeignCollectionField(eager = false)
    private ForeignCollection<Episode> episodes;
    ...
}

@DatabaseTable(tableName = "episodes")
public class Episode {
    @DatabaseField(id = true)
    private Integer episodeId;

    @DatabaseField(foreign = true)
    private UserShow show;
    ...
}

我保存我的UserShows对象,如在示例中

I am saving my UserShows objects like in example

UserShow show = new UserShow();
userShowDao.create(show);

for (Episode e: eps) {
    e.setShow(show);
    episodeDao.create(e); 
} 

UserShow对象有一个外部惰性集合集,但是当我试图获得all userShows:

UserShow object have a foreign lazy collection episodes, but when I am trying to get all userShows:

shows = userShowsDao().queryForAll();

我得到所有的节目对象集合的剧集。为什么会发生这种情况?集合是惰性的,我必须得到null或其他东西,但没有集合的Episode对象。如何使这个集合真的懒?如果ORMLite有能力获取对象没有延迟集合和初始化时,它真的需要可能是很酷。例如 Hibernate.initialize 方法。

I am getting all shows objects with collections of episodes. Why this happens? Collection is lazy and I must to get null or something else but no collection of Episode object. How to make this collection really lazy? It may be cool if ORMLite have ability get objects without lazy collections and initialize when it really need. For example as Hibernate.initialize method.

谢谢!

推荐答案

惰性集合已经过很好的测试,并被许多其他人使用,虽然可能有bug,但它更可能是被懒惰集合类。

The lazy collections have been well tested and are in use by many others so although there could be bugs with it, it is more likely that you are being tricked by the lazy collection class.

当从DAO检索每个 UserShow 对象时, episodes 不会为null,而是将设置为 LazyForeignCollection 的实例。但是,不会进行任何其他查询,并且集合中不会包含 Episode 数据。如果您随后调用集合上的某个方法,例如 userShow.getEpisodes()。iterator(),然后单独查询是在那个时候允许你迭代该节目的情节。这是惰性集合的工作方式。

When each of the UserShow objects is retrieved from the DAO, the episodes will not be null but instead will be set with an instance of LazyForeignCollection. However, no additional queries will be made and there will be no Episode data contained by the collection. If you then make a call to one of the methods on the collection such as userShow.getEpisodes().iterator(), then a separate query is made at that time to allow you to iterate through that show's episodes. That's how the lazy collections work.

如果你仍然认为惰性集合不工作,那么请告诉我们你如何确定节目有剧集数据。要查看在哪里执行了哪些查询,可以使用ORMLite 启用 Android日志记录

If you still think that the lazy collections aren't working then please show us how you are determining that the shows have episode data. To see what queries are being done where, you can enable Android logging with ORMLite.

编辑

@Georgy正在使用调试器来调查收集。调试器很可能调用导致收集查询被发出的相同的 iterator() toArray()在那个时刻。所以在调试器请求之前,集合中没有任何剧集。

It turns out that @Georgy was using the debugger to investigate the collection. The debugger is most likely calling the same iterator() or toArray() methods which cause the collection queries to be issued at that instant. So there weren't any episodes in the collection before the debugger asked for them.

这篇关于ORMLite和惰性集合的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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