Okay... I got past that exception and pasted my code below.

I now have reverse mapped the views... I only hope this works when it comes
time to retrieve and save objects from/to the database. X=


    @Override
    public boolean unmappedTable(Table table) {
        // this method is called to give this class an opportunity to map
the
        // table which would not be mapped otherwise.  Returning false says
        // the table wasn't mapped here.
        //Class klass = rmt.generateClass(table.getIdentifier().getName(),
null);
        String packageName = rmt.getPackageName();
        String tableName = table.getIdentifier().getName();
        String className = NameConverters.convertTableName(tableName);
        Class klass = rmt.generateClass(packageName+"."+className, null);
        ClassMapping cls = rmt.newClassMapping(klass, table);
        Column pk = null;
        for ( Column column : table.getColumns() ) {
            String columnName = column.getIdentifier().getName();
            String fieldName = rmt.getFieldName(columnName, cls);
            Class type = rmt.getFieldType(column, false);
            FieldMapping field = cls.addDeclaredFieldMapping(fieldName,
type);
            field.setExplicit(true);
            field.setColumns(new Column[]{column});
            // TODO: set the appropriate strategy for non-primitive types.
            field.setStrategy(new PrimitiveFieldStrategy(), null);
            if ("MODEL_VIEW".equals(tableName) &&
"MODEL_ID".equals(columnName)) {
                pk = column;
                field.setPrimaryKey(true);
            }
        }
        //cls.setPrimaryKeyColumns(new Column[]{pk});
        cls.setObjectIdType(null, false);
        cls.setIdentityType(ClassMapping.ID_DATASTORE);
        cls.setStrategy(new FullClassStrategy(), null);
        return true;
    }

--
Jason Zwolak


On Thu, Nov 21, 2013 at 9:39 AM, Jason Zwolak <jzwo...@gmail.com> wrote:

> 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
> made?
>
> 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):
>     @Override
>
>     public boolean unmappedTable(Table table) {
>
>         Class klass = rmt.generateClass(table.getIdentifier().getName(),
> null);
>
>         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"?>
>
> <schemas>
>
>     <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"/>
>
>         </table>
>
>     </schema>
>
> </schemas>
>
> Thanks for any help!!
> --
> Jason Zwolak
>

Reply via email to