Here's another, perhaps even simpler cut at it:

@PersistenceCapable
public interface Person {}

@PersistenceCapable
@Implements(Person.class)
public class MyPerson implements Person {}

public class Main {

    @SuppressWarnings("unchecked")
    public static void main(String... args) {
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("jpox.properties");
        PersistenceManager pm = pmf.getPersistenceManager();
        Person inst = new MyPerson();

        System.out.println(inst);

        pm.currentTransaction().begin();
        pm.makePersistent(inst);
        pm.currentTransaction().commit();

System.out.println(pm.newQuery(pm.getExtent(Person.class, true)).execute());
    }

}


When running the above, JPOX auto-creates the following schema:

CREATE
    TABLE myperson
    (
        myperson_id bigint(20) NOT NULL,
        PRIMARY KEY USING BTREE (myperson_id)
    )

CREATE
    TABLE personimpl
    (
        personimpl_id bigint(20) NOT NULL,
        PRIMARY KEY USING BTREE (personimpl_id)
    )


The output of the main() method is as follows:

[EMAIL PROTECTED]
[]


After running, the MYPERSON table has 1 row (as expected), the PERSONIMPL table has 0 rows.

The query in the main method is only pulling back rows from the PERSONIMPL table, thus the empty list.

It seems strange to me that the implementation is creating its own implementation (PersonImpl) when I've clearly expressed that I want to provide my own (MyPerson).

Thanks,

- Chris

P.S.: the results are the same whether I use an extent as above, or just directly query on the interface.


On Sep 1, 2007, at 8:53 AM, Erik Bengtson wrote:

Chris,

I think you have to use the Implements annotation. Something like:

        @PersistenceCapable(table="person")
@Implements(Person.class)
        public class MyPerson implements Person {}



-----Message d'origine-----
De : cbeams [mailto:[EMAIL PROTECTED]
Envoyé : samedi 1 septembre 2007 9:14
À : jdo-user@db.apache.org
Objet : Query by interface


As a user, here's what I would expect to work:


        @PersistenceCapable(table="person")
        public interface Person {}

        @PersistenceCapable(table="person")
        public class MyPerson implements Person {}


        public class Main {
                public static void main(String... args) {
                        PersistenceManagerFactory pmf =
JDOHelper.getPersistenceManagerFactory("pmf.properties");
                        PersistenceManager pm = pmf.getPersistenceManager();
                        
                        pm.currentTransaction().begin();
                        pm.makePersistent(new MyPerson());  // persist the
concrete type
                        pm.currentTransaction().commit();

                        Query query = pm.newQuery(Person.class);  // query
by the interface
                        query.setUnique(true);
                        Person person = (Person)query.execute();

                        assert person instanceof MyPerson == true;
                }       
        }

This does not work, however (at least with JPOX).  My instanceof
assertion at bottom fails, because person is actually a type
generated by the implementation (PersonImpl in the case of JPOX).
While I understand that this class generation approach may have some
uses, I'm actually dealing with I believe is a much simpler use case
that doesn't seem to be supported:  I simply want to be able to
persist an instance of a concrete type and subsequently query for
that object by it's interface.

Am I missing something?  Note that I'm not looking for any JPOX-
specific tips here; just some guidance on usage per the spec.

Thanks,

- Chris

Chris Beams










Reply via email to