【问题标题】:Objectify Ref in a subclass在子类中对象化 Ref
【发布时间】:2015-03-03 03:46:56
【问题描述】:

我正在尝试通过 Reference 获取列表。

我的课是这样的:

  • 在守护程序上运行的许可证,
  • 许可证可以是 LicenseCountryCondition 或具有 Ref 的另一个子类(对于 LicenseCountryCondition,参数是国家/地区的 Ref)。

许可证:

@Entity
@Cache
@Index
public class License {
    @Id
    Long id;
    private String name;
    private String startDate;
    private String expDate;
    private int timeStamp;
    private int status;
    Ref<Daemon> daemon;
    private boolean inactive;
}

许可证国家条件:

@Index
@Subclass(index=true)
public class LicenseCountryCondition extends License{
    Ref<Country> country;
}

如果我想查找在特定守护进程上工作的LicenseCountryCondition列表,我会这样做:

Daemon dae=ofy().load().type(Daemon.class).filter("name", "example").first().now();  

    List<LicenseCountryCondition>test=ofy().load().type(LicenseCountryCondition.class).filter("daemon",dae).list();
                for(LicenseCountryCondition i:test){
                    System.out.println(i.getName());
                    System.out.println(i.getDaemon().getName());
                }

我得到了很好的结果。

但是,当我尝试让 LicenseCountryCondition 列表特定国家工作时,它不起作用:

Country ctr=ofy().load().type(Country.class).filter("name", "France").first().now();
    List<LicenseCountryCondition> test=ofy().load().type(LicenseCountryCondition.class).filter("country",ctr).list();
        for(LicenseCountryCondition i:test){
            System.out.println(i.getName());
        }

我可以得到这份清单吗? (我看到this但不是同一个问题)

感谢您的关注。

【问题讨论】:

    标签: java google-app-engine filtering subclass objectify


    【解决方案1】:

    确保您对法国的查询实际上返回一个真实的国家/地区(不为空)。

    您所写的内容并没有明显的错误,但是这里发生的事情太多了,而且还有太多未指定的数据库状态,以至于有人无法回答这个问题。最好的办法是将创建一些实体的测试用例(因此数据库状态已知)放在一起,然后演示您认为应该成功但仍然失败的查询。

    【讨论】:

    • 感谢您的回答。我不知道为什么,但是当我今天早上尝试时,它起作用了。我只是看到我的两个 LicenceCountryCondition 仍然没有索引,我想我昨天才和他们一起工作
    • 您是否可以保存未索引的值,然后稍后添加@Index 注释?注释控制属性是否在保存时被索引;如果添加注释,则必须重新保存所有相关实体。
    • 是的,我现在明白我的错误了。我在想,因为超级,我不必在我的子类上添加@index,但现在我意识到这很愚蠢,因为我的子类上有需要索引的参数......而且我处理测试数据,所以我在理解问题后重新创建数据。感谢您的关注 =)
    猜你喜欢
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    • 2010-09-19
    • 1970-01-01
    • 2023-01-30
    相关资源
    最近更新 更多