|
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
public interface ComputerPeripheral
{
long getId();
void setId(long val);
String getManufacturer();
void setManufacturer(String name);
String getModel();
void setModel(String name);
}
and we have the following implementations
public class Mouse implements ComputerPeripheral
{
...
}
public class Keyboard implements ComputerPeripheral
{
...
}
So to make it possible to query over
ComputerPeripheral
we need to make it persistable, hence
defining metadata like this (or alternatively using annotations)
<interface name="ComputerPeripheral">
<property name="id" primary-key="true"/>
<property name="manufacturer"/>
<property name="model"/>
</interface>
<class name="Mouse" table="COMPUTER_MOUSE">
<property name="id" primary-key="true"/>
<property name="manufacturer"/>
<property name="model"/>
</class>
<class name="Keyboard" table="COMPUTER_KEYBOARD">
<property name="id" primary-key="true"/>
<property name="manufacturer"/>
<property name="model"/>
</class>
so we have the interface defining the identity (and consistent in the implementations), and now 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
-
This is only possible in version 2.2 M3 and later
|
|