0

objectdb にクエリを実装する必要がありますが、ほとんどわかりません。

問題は、クエリを書くことです

Returns the collection of all laptops each of which has at least one
          other laptop preinstalled with the same processor.

私のラップトップクラスは

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
}

私のプロセッサクラスは

public class Processor {

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

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

また、私の関数定義は以下のようになります

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でもいいです。

ありがとう

4

1 に答える 1

1

やっとこいつを乗り越えた。これが解決策です

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定義は次のとおりです。

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;

    }

ありがとう。それが役に立てば幸い。

于 2012-05-18T19:40:48.237 に答える