首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在objectdb中查询

在objectdb中查询
EN

Stack Overflow用户
提问于 2012-05-17 22:20:00
回答 1查看 344关注 0票数 0

我必须在objectdb中实现一个查询,并且对此一无所知。

问题是编写一个查询,该查询

代码语言:javascript
复制
Returns the collection of all laptops each of which has at least one
          other laptop preinstalled with the same processor.

我的笔记本电脑课程是

代码语言:javascript
复制
public class Laptop {

    String modelName; // key
    int price;        // in dollars
    boolean hasHDScreen; // has a HD Screen ?
    int hardDriveCapacity; // in GB

    Processor processor;  // the preinstalled processor
    Memory memory; // the preinstalled memory
    Company madeBy; // the inverse of company.makeLaptops
}

我的处理器类是

代码语言:javascript
复制
public class Processor {

    String modelName; // key 
    float clockSpeed; // in gigahertz (GHz)

    Company madeBy; // the inverse of Company.makeProcessors
}   

另外,我的函数定义如下

代码语言:javascript
复制
public static Collection<Laptop> sameProcessor(Query q) {
        /* Returns the collection of all laptops each of which has at least one
         * other laptop preinstalled with the same processor.
         */
        q.setClass(Laptop.class);
        q.setFilter("this.processor == ");

    } 

我怎样才能做到这一点?SQL也可以。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2012-05-19 03:40:48

终于克服了这一点。以下是解决方案

代码语言:javascript
复制
public static Collection<Laptop> sameProcessor(Query q) {
        /* Returns the collection of all laptops each of which has at least one
         * other laptop preinstalled with the same processor.
         */

        Collection result = new HashSet<Laptop>();
        q.setClass(Laptop.class);

        Collection allLaptops = (Collection) q.execute();

        Iterator it = allLaptops.iterator();

        while(it.hasNext()) {
            Laptop currentLaptop = ((Laptop)it.next());
            Processor p = currentLaptop.processor;
            if(p.installedOn(q).size()>=2) {

                result.add(currentLaptop);
            }
        }

        return (Collection<Laptop>)result;       
    } 

使用installedOn定义的方法如下:

代码语言:javascript
复制
public Collection<Laptop> installedOn(Query q) {

     /* Returns the collection of all laptops on which the target memory 
        is preinstalled. Represents the inverse of Laptop.memory.
     */
        Memory memory = this;
        q.setClass(Laptop.class);
        q.declareParameters("Memory m");
        q.setFilter("this.memory == m");
        Collection result = (Collection)q.execute(memory);
        return (Collection<Laptop>) result;

    }

谢谢。希望能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10637513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档