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 <[email protected]>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 > > > >
