openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Giblin <...@zurich.ibm.com>
Subject Re: metadata api?
Date Sun, 13 Sep 2009 12:22:29 GMT
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
> <CGI@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
> > >
> > >
>


Mime
View raw message