JDO : JDOQL Querying interfaces

Applicable to RDBMS, ODF, Excel, OOXML, XML, HBase, MongoDB.

Typical usage of JDOQL is to query for instances of a class. DataNucleus also allows you to query for instances of implementations of an interface. Let's take an example. We have an interface

@PersistenceCapable
public interface ComputerPeripheral
{
    @PrimaryKey
    long getId();
    void setId(long val);

    @Persistent
    String getManufacturer();
    void setManufacturer(String name);

    @Persistent
    String getModel();
    void setModel(String name);
}

and we have the following implementations

@PersistenceCapable
public class Mouse implements ComputerPeripheral
{
    ...
}

@PersistenceCapable
public class Keyboard implements ComputerPeripheral
{
    ...
}

So we have made our interface persistable, and defined the identity property(ies) there. The implementations of the interface will use the identity defined in the interface. To query it we simply do

Query q = pm.newQuery(ComputerPeripheral.class);
List<ComputerPeripheral> results = (List<ComputerPeripheral>)q.execute();

The key rules are

  • You must define the interface as persistent
  • The interface must define the identity/primary key member(s)
  • The implementations must have the same definition of identity and primary key