8067

Query in objectdb

Question:

I have to implement a query in objectdb and have very little idea.

The problem is to write a query which

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

My Laptop class is

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 }

and my processor class is

public class Processor { String modelName; // key float clockSpeed; // in gigahertz (GHz) Company madeBy; // the inverse of Company.makeProcessors }

Also my function definition looks like below

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 == "); }

How can I achieve it ? SQL would be fine as well.

Thanks

Answer1:

Finally got over this one. Here's the solution

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

The method used installedOn definition is below:

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

Thanks. Hope it helps.

Recommend

  • Using integer instead of decimal
  • Objective C: Difference between self and super
  • How do you print a dollar sign $ in Dart
  • How to get Application name from process name?
  • Convert pandas._period.Period type Column names to Lowercase
  • Calculating the dollars right in the Money change program - C++ [closed]
  • Adding a pivot to a rollup
  • How do I use a hyperlink in link variable?
  • Reusing the CQ5 Form into the mywebsite components is not showing up the End of the Form section
  • Fatal error: Call to a member function fetch() on a non-object?
  • PE file - what's missing?
  • Conversion from C++ to Delphi (simple)
  • Excel VBA Intersect
  • Python equivalent of Scala's exists() function?
  • @Autowired for @ModelAttribute
  • Spring Boot OAuth2 SSO with access/refresh tokens is not stored in a database correctly
  • reduce/reduce conflicts using ocamlyacc
  • Pyglet uses too much cpu
  • how to set id for each item in ListView
  • Show records ordered with maximum price first in PHP & MySQL
  • Messed up characters in webpages (especially social media)
  • Initialization section of the package
  • calling a fragment from fragment
  • to implement a spinner in angular2+
  • MVC - @Html.CheckBoxFor
  • Find JSON nested nodes using multiple string values
  • Hector: how to query parts of a Composite Type
  • MeeGo Handset Emulator not starting on Windows 7
  • Application level floating views with navigation in Android
  • Memory error in python- how to use more memory
  • AndEngine Applying Transparancy to AndEngine View
  • converting text file into xml using php?
  • Using a canvas object in a thread to do simple animations - Java
  • System.InvalidCastException: Specified cast is not valid
  • How can I extract results of aggregate queries in slick?
  • OOP Javascript - Is “get property” method necessary?
  • Avoid links criss cross / overlap in d3.js using force layout
  • PHP - How to update data to MySQL when click a radio button
  • Optimizing database types to compact database (SQLite)
  • Bitwise OR returns boolean when one of operands is nil