You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Christopher Giblin <CG...@zurich.ibm.com> on 2009/09/09 10:31:43 UTC

metadata api?

Hi,
I defined object-relational mappings in orm.xml.  Works fine.

Is an API available which allows querying of the orm definition for a given
object? I am programming with reflection and would like to know, given an
object, which of its attributes are primary or foreign keys.

Thanks, chris


Re: metadata api?

Posted by Christopher Giblin <CG...@zurich.ibm.com>.
Hi,
If I go over the column, I get closer::

   pk.getColumns()[0].getName()

Wondering how I would get the attribute name instead of column name?
In following example, how would I get "groupId" instead of "group_id" ?
  <attributes>
      <id name="groupId">
        <column name="group_id"/>
        <generated-value strategy="IDENTITY"/>
      </id>
   ...

-chris

> Thanks Jeremy,
>
> However, pk.getName() returns null as does fk[i].getName().
> In the debugger, I see the table clsMapping.getTable() is the expected
> table.
> Also, pk is instantiated and fk[] has the right number of foreign keys.
> They just don't return their respective names, which is what I need.
>
> How do I retrieve the name of these keys? Or is this a bug?
>
> Thanks,chris
>
> > Hi Chris,
> >
> > OpenJPA provides APIs that allow you to interrogate class mappings.
> Using a
> > direct approach (OpenJPA resolves class mapping and metadata as part of
> > creating an emf/em), this code gives you access to the primary and
> foreign
> > keys of an entity via the schema information stored in the mapping.
> >
> > import org.apache.openjpa.jdbc.meta.ClassMapping;
> > import org.apache.openjpa.jdbc.schema.Column;
> > import org.apache.openjpa.jdbc.schema.ForeignKey;
> > import org.apache.openjpa.jdbc.schema.PrimaryKey;
> > import org.apache.openjpa.persistence.JPAFacadeHelper;
> >
> > ...
> >         EntityManager em = emf.createEntityManager();
> >         ClassMapping clsMapping =
> > (ClassMapping)JPAFacadeHelper.getMetaData(em, SomeEntity.class);
> >
> >         ForeignKey[] fks = clsMapping.getTable().getForeignKeys();
> >         PrimaryKey pk = clsMapping.getTable().getPrimaryKey();
> >
> > hth,
> > -Jeremy
> >
> > On Wed, Sep 9, 2009 at 3:31 AM, Christopher Giblin
> <CG...@zurich.ibm.com>wrote:
> >
> > >
> > > Hi,
> > > I defined object-relational mappings in orm.xml.  Works fine.
> > >
> > > Is an API available which allows querying of the orm definition for a
> given
> > > object? I am programming with reflection and would like to know,
given
> an
> > > object, which of its attributes are primary or foreign keys.
> > >
> > > Thanks, chris
> > >
> > >
>


Re: metadata api?

Posted by Christopher Giblin <CG...@zurich.ibm.com>.
Thanks Jeremy,

However, pk.getName() returns null as does fk[i].getName().
In the debugger, I see the table clsMapping.getTable() is the expected
table.
Also, pk is instantiated and fk[] has the right number of foreign keys.
They just don't return their respective names, which is what I need.

How do I retrieve the name of these keys? Or is this a bug?

Thanks,chris

> Hi Chris,
>
> OpenJPA provides APIs that allow you to interrogate class mappings.
Using a
> direct approach (OpenJPA resolves class mapping and metadata as part of
> creating an emf/em), this code gives you access to the primary and
foreign
> keys of an entity via the schema information stored in the mapping.
>
> import org.apache.openjpa.jdbc.meta.ClassMapping;
> import org.apache.openjpa.jdbc.schema.Column;
> import org.apache.openjpa.jdbc.schema.ForeignKey;
> import org.apache.openjpa.jdbc.schema.PrimaryKey;
> import org.apache.openjpa.persistence.JPAFacadeHelper;
>
> ...
>         EntityManager em = emf.createEntityManager();
>         ClassMapping clsMapping =
> (ClassMapping)JPAFacadeHelper.getMetaData(em, SomeEntity.class);
>
>         ForeignKey[] fks = clsMapping.getTable().getForeignKeys();
>         PrimaryKey pk = clsMapping.getTable().getPrimaryKey();
>
> hth,
> -Jeremy
>
> On Wed, Sep 9, 2009 at 3:31 AM, Christopher Giblin
<CG...@zurich.ibm.com>wrote:
>
> >
> > Hi,
> > I defined object-relational mappings in orm.xml.  Works fine.
> >
> > Is an API available which allows querying of the orm definition for a
given
> > object? I am programming with reflection and would like to know, given
an
> > object, which of its attributes are primary or foreign keys.
> >
> > Thanks, chris
> >
> >


Re: metadata api?

Posted by Jeremy Bauer <te...@gmail.com>.
Hi Chris,

OpenJPA provides APIs that allow you to interrogate class mappings.  Using a
direct approach (OpenJPA resolves class mapping and metadata as part of
creating an emf/em), this code gives you access to the primary and foreign
keys of an entity via the schema information stored in the mapping.

import org.apache.openjpa.jdbc.meta.ClassMapping;
import org.apache.openjpa.jdbc.schema.Column;
import org.apache.openjpa.jdbc.schema.ForeignKey;
import org.apache.openjpa.jdbc.schema.PrimaryKey;
import org.apache.openjpa.persistence.JPAFacadeHelper;

...
        EntityManager em = emf.createEntityManager();
        ClassMapping clsMapping =
(ClassMapping)JPAFacadeHelper.getMetaData(em, SomeEntity.class);

        ForeignKey[] fks = clsMapping.getTable().getForeignKeys();
        PrimaryKey pk = clsMapping.getTable().getPrimaryKey();

hth,
-Jeremy

On Wed, Sep 9, 2009 at 3:31 AM, Christopher Giblin <CG...@zurich.ibm.com>wrote:

>
> Hi,
> I defined object-relational mappings in orm.xml.  Works fine.
>
> Is an API available which allows querying of the orm definition for a given
> object? I am programming with reflection and would like to know, given an
> object, which of its attributes are primary or foreign keys.
>
> Thanks, chris
>
>