Here is a
hack to get access to the currently analyzed Table object in a ReverseEngineeringStrategy class:
Code:
public class Strategy extends DelegatingReverseEngineeringStrategy {
// Instance to hold the Table object describing the table on which work is
// currently being performed. This is necessary because we need to suppress
// the primary keys from being mapped to the pojos, but we have no access to
// the information in the meta attribute routines. Valid as long as
// isManyToMany is called first in the binder's createPersistentClasses.
private org.hibernate.mapping.Table currentTable;
public Strategy(ReverseEngineeringStrategy delegate) {
super(delegate);
}
@Override
public boolean isManyToManyTable(Table table) {
// Store the Table object for further analysis down the road.
currentTable = table;
return super.isManyToManyTable(table);
}
@Override
public Map columnToMetaAttributes(TableIdentifier identifier, String column) {
Map<String, MetaAttribute> metaAttributes = new HashMap<String, MetaAttribute>();
String identifierName = "";
// Find the primary key column name from the stored Table object
if (currentTable != null && currentTable.hasPrimaryKey()) {
List columns = currentTable.getPrimaryKey().getColumns();
if (columns.size() == 1) {
Column keyColumn = (Column) columns.get(0);
identifierName = keyColumn.getName();
}
}
// Suppress pojo mapping of primary keys and specified column names
if ((!inForeignKeyRoutine && identifierName.equalsIgnoreCase(column))
|| (Arrays.binarySearch(excludePojoColumnNames, column) >= 0)) {
MetaAttribute genProperty = new MetaAttribute("gen-property");
genProperty.addValue("false");
metaAttributes.put("gen-property", genProperty);
}
return metaAttributes;
}
}
Please note that this is a
hack to expose the Table object to the rest of the ReverseEngineeringStrategy methods (columnToMetaAttributes in this case) and is only valid so long as Max keeps the isManyToMany call the first Strategy method employed in the binder's createPersistentClasses method. This works on on beta8, but is not guaranteed to work on any subsequent releases. There is also no guarantee that the Table object will be fully hydrated with complete information at this stage in the reverse engineering process, so use with caution.