openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jason Zwolak <>
Subject How to reverse map database views?
Date Thu, 21 Nov 2013 17:39:28 GMT
Since database views don't have keys, openjpa's reverse mapping tool
doesn't map them by default.

The views I'm using have a column that is unique for every row in the view
and I'd like to use it as the @Id in the generated POJOs.  Can I somehow
give OpenJPA the column to use in each view so that a reverse map can be

I have a ReverseCustomizer class that I tried to use to do this and all the
views got sent to unmappedTable.  I could also create a custom DBDictionary
if that would help.

If using the ReverseCustomizer is the way to go, then some more information
about how to perform the map in the unmappedTable method would be useful.
 The JavaDocs say this:

Notification that a table has gone unmapped. You can map the table yourself
> using this method. When mapping, use
> ReverseMappingTool.generateClass(java.lang.String, java.lang.Class) to
> create the class,ReverseMappingTool.newClassMapping(java.lang.Class,
> org.apache.openjpa.jdbc.schema.Table) to create the class metadata, and
> then ClassMapping.addDeclaredFieldMapping(java.lang.String,
> java.lang.Class) to add field metadata.

And I wrote some code and got this exception:

Exception in thread "main" <openjpa-2.4.0-SNAPSHOT-r422266:1539200M fatal
user error> org.apache.openjpa.util.MetaDataException: No table was given
for persistent type "MODEL_VIEW".

Here's my code (for testing I'm only mapping a single view and this code is
for that view):

    public boolean unmappedTable(Table table) {

        Class klass = rmt.generateClass(table.getIdentifier().getName(),

        ClassMapping cm = rmt.newClassMapping(klass, table);

        Column pk = null;

        for ( Column column : table.getColumns() ) {

            String name = column.getIdentifier().getName();

            if ("MODEL_ID".equals(name)) {

                pk = column;


            // this is a hack just to get the code running...
            // later I'll replace it with the appropriate type on a per
column basis
            cm.addDeclaredFieldMapping(name, Integer.class);


        cm.setPrimaryKeyColumns(new Column[]{pk});

        return true;


I'm calling the reverse mapping tool with:

java -classpath $CLASSPATH \

    org.apache.openjpa.jdbc.meta.ReverseMappingTool \

    -pkg com.mybiz.generated.entities -d src \

    build/schema.xml -annotations true -metadata none \

    -cc com.mybiz.MyReverseCustomizer

And schema.xml looks like (I removed all columns except the two seen here
for brevity):

<?xml version="1.0" encoding="UTF-8"?>


    <schema name="MY_SCHEMA">

        <table name="MODEL_VIEW">

            <column name="MODEL_ID" type="decimal" type-name="NUMBER"
not-null="true" size="22"/>

            <column name="MODEL_DESCRIPTION" type="varchar"
type-name="VARCHAR2" not-null="true" size="240"/>




Thanks for any help!!
Jason Zwolak

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message