2012-05-17 1 views
0

나는 objectdb에 쿼리를 구현해야하고 거의 아이디어가 없습니다.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은 괜찮을 것이다.

감사합니다.

+0

최대 절전 모드를 사용 하시겠습니까? – ant

+0

@ant : 단지 JDOQL 쿼리가 아닙니다. –

+0

@ant : 그러나 최대 절전 모드의 쿼리는 또한 편리 할 수 ​​있습니다. 왜냐하면 나는 JAVA와 ObjectDb 질의어로 변환하려고 시도 할 수 있기 때문이다. –

답변

1

마지막으로이 문제를 해결했습니다. 여기 방법은 installedOn 정의를 사용

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;  
    } 

솔루션을 것은 다음과 같습니다 :

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; 

    } 

감사합니다. 희망이 도움이됩니다.