private Mappings classMapping(Database database, Mappings mappings) { for (Schema schema : database.getSchemas()) { for (com.manydesigns.portofino.model.database.Table aTable : schema.getTables()) { logger.debug("Class - {}", aTable.getQualifiedName()); com.manydesigns.portofino.model.database.PrimaryKey primaryKey = aTable.getPrimaryKey(); if (primaryKey == null) { logger.debug("Skipping table without primary key: {}", aTable.getQualifiedName()); continue; } if (!primaryKey.isValid()) { logger.debug("Skipping table with invalid primary key: {}", aTable.getQualifiedName()); continue; } RootClass clazz = createTableMapping(mappings, aTable); if(clazz != null) { mappings.addClass(clazz); mappings.addImport(clazz.getEntityName(), clazz.getEntityName()); } } } return mappings; }
private void normalizePrimaryKeys(Table table, String entityName) { PrimaryKey pk = table.getPrimaryKey(); if (pk != null) { String name = strategy.primaryKeyName(entityName, table.getName()); pk.setName(name); } }
public static String getPrimaryColName(Table table) throws ServiceException { String colName = ""; try { PrimaryKey pk = table.getPrimaryKey(); List lst = pk.getColumns(); Column col = (Column) lst.get(0); colName = col.getName(); } catch(Exception e) { throw ServiceException.FAILURE("HibernateUtil.getPrimaryColName", e); } return colName; }
protected void createPKComposite(Mappings mappings, com.manydesigns.portofino.model.database.Table mdTable, String pkName, RootClass clazz, Table tab, List<com.manydesigns.portofino.model.database.Column> columnPKList) { PrimaryKey primaryKey = new PrimaryKey(); primaryKey.setName(pkName); primaryKey.setTable(tab); clazz.setEmbeddedIdentifier(true); Component component = new Component(mappings, clazz); component.setDynamic(mdTable.getActualJavaClass()==null); String name; name = mdTable.getQualifiedName(); component.setRoleName(name + ".id"); component.setEmbedded(true); //component.setNodeName("id"); component.setKey(true); component.setNullValue("undefined"); if (!component.isDynamic()){ component.setComponentClassName (mdTable.getJavaClass()); //TODO verificare se non si intende actualJavaClass } boolean hasErrors = false; for (com.manydesigns.portofino.model.database.Column column : columnPKList) { if (column == null ) { throw new InternalError("Null column"); } Column col = createColumn(mappings, tab, column); hasErrors = col == null || hasErrors; if(col != null) { primaryKey.addColumn(col); Property prop = createProperty(column, col.getValue()); prop.setCascade("none"); //prop.setPropertyAccessorName("property"); interferisce con il generator più sotto prop.setPersistentClass(clazz); component.addProperty(prop); //Generator not supported for embedded map identifier //See https://forum.hibernate.org/viewtopic.php?t=945273 //See Component.buildIdentifierGenerator() /*String columnName = column.getColumnName(); PrimaryKeyColumn pkCol = mdTable.getPrimaryKey().findPrimaryKeyColumnByName(columnName); if(pkCol == null) { logger.error("Column without corresponding PrimaryKeyColumn: {}", columnName); hasErrors = true; continue; } Generator generator = pkCol.getGenerator(); setPKColumnGenerator(mappings, clazz, tab, column, value, generator);*/ } } if (hasErrors) { // TODO : se la PK non e' buona, tutta la tabella dovrebbe saltare logger.error("Table "+name+": Skipping primary key"); return; } tab.setIdentifierValue(component); clazz.setIdentifier(component); clazz.setDiscriminatorValue(name); tab.setPrimaryKey(primaryKey); }
protected void createPKSingle(Mappings mappings, com.manydesigns.portofino.model.database.Table mdTable, String pkName, RootClass clazz, Table tab, List<com.manydesigns.portofino.model.database.Column> columnPKList) { PrimaryKeyColumn pkcol = mdTable.getPrimaryKey().getPrimaryKeyColumns().get(0); com.manydesigns.portofino.model.database.Column column = columnPKList.get(0); final PrimaryKey primaryKey = new PrimaryKey(); primaryKey.setName(pkName); //TODO quote? primaryKey.setTable(tab); tab.setPrimaryKey(primaryKey); Column col = createColumn(mappings, tab, column); if (col == null) { // TODO : se la PK non e' buona, tutta la tabella dovrebbe saltare logger.error("Skipping primary key "+pkName); return; } SimpleValue id = (SimpleValue) col.getValue(); //Make the defaults explicit. See section 5.1.4.5. Assigned identifiers in the Hibernate reference //(http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html) id.setIdentifierGeneratorStrategy("assigned"); id.setNullValue("undefined"); tab.getPrimaryKey().addColumn(col); Property prop = createProperty(column, id); clazz.addProperty(prop); prop.setPropertyAccessorName(mappings.getDefaultAccess()); prop.setInsertable(false); prop.setUpdateable(false); Generator generator = pkcol.getGenerator(); setPKColumnGenerator(mappings, clazz, tab, column, id, generator); tab.setIdentifierValue(id); clazz.setIdentifier(id); clazz.setIdentifierProperty(prop); clazz.setDiscriminatorValue(mdTable.getQualifiedName()); }
private void validatePrimaryKeys(Table table) { PrimaryKey primaryKey = table.getPrimaryKey(); if (primaryKey != null) { validateMaxLength(primaryKey.getName()); } }
protected void createPKComposite(Mappings mappings, com.manydesigns.portofino.model.database.Table mdTable, String pkName, RootClass clazz, Table tab, List<com.manydesigns.portofino.model.database.Column> columnPKList) { PrimaryKey primaryKey = new PrimaryKey(); primaryKey.setName(pkName); primaryKey.setTable(tab); clazz.setEmbeddedIdentifier(true); Component component = new Component(mappings, clazz); component.setDynamic(mdTable.getActualJavaClass()==null); String name; name = mdTable.getQualifiedName(); component.setRoleName(name + ".id"); component.setEmbedded(true); //component.setNodeName("id"); component.setKey(true); component.setNullValue("undefined"); if (!component.isDynamic()){ component.setComponentClassName (mdTable.getJavaClass()); //TODO verificare se non si intende actualJavaClass } boolean hasErrors = false; for (com.manydesigns.portofino.model.database.Column column : columnPKList) { if (column == null ) { throw new InternalError("Null column"); } Column col = createColumn(mappings, tab, column); hasErrors = col == null || hasErrors; if(col != null) { primaryKey.addColumn(col); Property prop = createProperty(column, col.getValue()); prop.setCascade("none"); //prop.setPropertyAccessorName("property"); interferisce con il generator più sotto prop.setPersistentClass(clazz); component.addProperty(prop); //Generator not supported for embedded map identifier //See https://forum.hibernate.org/viewtopic.php?t=945273 //See Component.buildIdentifierGenerator() /*String columnName = column.getColumnName(); PrimaryKeyColumn pkCol = mdTable.getPrimaryKey().findPrimaryKeyColumnByName(columnName); if(pkCol == null) { logger.error("Column without corresponding PrimaryKeyColumn: {}", columnName); hasErrors = true; continue; } Generator generator = pkCol.getGenerator(); setPKColumnGenerator(mappings, clazz, tab, column, value, generator);*/ } } if (hasErrors) { // TODO PAOLO: se la PK non e' buona, tutta la tabella dovrebbe saltare logger.error("Skipping primary key"); return; } tab.setIdentifierValue(component); clazz.setIdentifier(component); clazz.setDiscriminatorValue(name); tab.setPrimaryKey(primaryKey); }
protected void createPKSingle(Mappings mappings, com.manydesigns.portofino.model.database.Table mdTable, String pkName, RootClass clazz, Table tab, List<com.manydesigns.portofino.model.database.Column> columnPKList) { PrimaryKeyColumn pkcol =mdTable.getPrimaryKey().getPrimaryKeyColumns().get(0); com.manydesigns.portofino.model.database.Column column = columnPKList.get(0); final PrimaryKey primaryKey = new PrimaryKey(); primaryKey.setName(pkName); primaryKey.setTable(tab); tab.setPrimaryKey(primaryKey); Column col = createColumn(mappings, tab, column); if (col == null) { // TODO PAOLO: se la PK non e' buona, tutta la tabella dovrebbe saltare logger.error("Skipping primary key"); return; } SimpleValue id = (SimpleValue) col.getValue(); //Make the defaults explicit. See section 5.1.4.5. Assigned identifiers in the Hibernate reference //(http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html) id.setIdentifierGeneratorStrategy("assigned"); id.setNullValue("undefined"); tab.getPrimaryKey().addColumn(col); Property prop = createProperty(column, id); clazz.addProperty(prop); prop.setPropertyAccessorName(mappings.getDefaultAccess()); //PropertyGeneration generation = PropertyGeneration.parse(null); //prop.setGeneration(generation); prop.setInsertable(false); prop.setUpdateable(false); Generator generator = pkcol.getGenerator(); setPKColumnGenerator(mappings, clazz, tab, column, id, generator); tab.setIdentifierValue(id); clazz.setIdentifier(id); clazz.setIdentifierProperty(prop); clazz.setDiscriminatorValue(mdTable.getQualifiedName()); }
@Override public void translate(TranslatorContext tc) { DatabaseCollector collector = tc.getDatabaseCollector(); Map<String, TableIdentifier> mapped = new HashMap<>(); RDBMS rdbms = setupRDBMS(tc); Map<String, Join> tableToJoin = new HashMap<>(); Map<TableIdentifier,PrimaryKey> pks = new HashMap<>(); rdbms.setDialect((String) tc.getMap().get("rdbmsDialect")); for (Iterator<Table> i = collector.iterateTables(); i.hasNext();) { Table table = i.next(); if(table.getColumnSpan()==0) { LOGGER.warn("Table without column found and it will be ignored. Its name is '" + table + "'."); continue; } /* // TODO analyze this case if(revengStrategy.isManyToManyTable(table)) {} */ Map<String, ForeignKey> fks = new HashMap<>(); TableIdentifier tableIdentifier = TableIdentifier.create(table); String id = tableIdentifier.toString(); pks.put(tableIdentifier,table.getPrimaryKey()); if(!mapped.containsKey(id)) { mapped.put(id, tableIdentifier); } else { throw new IllegalStateException("Table mapped twice"); } Join join = null; if(tableToJoin.get(id) == null){ join = new Join(); join.setProjectionMappings(new ArrayList<ProjectionMapping>()); join.setTables(new ArrayList<com.redhat.lightblue.metadata.rdbms.model.Table>()); join.setJoinTablesStatement(""); rdbms.getSQLMapping().getJoins().add(join); tableToJoin.put(id,join); }else{ join = tableToJoin.get(id); } for (Iterator<ForeignKey> j = table.getForeignKeyIterator(); j.hasNext();) { ForeignKey fk = j.next(); Table referencedTable = fk.getReferencedTable(); TableIdentifier ti = TableIdentifier.create(referencedTable); tableToJoin.put(ti.toString(),join); for (Iterator<Column> z = fk.getColumns().iterator(); z.hasNext();) { Column c = z.next(); fks.put(c.getName(),fk); String joinTable = join.getJoinTablesStatement(); if(joinTable.length() != 0){ joinTable = joinTable + " AND "; } join.setJoinTablesStatement(joinTable + table.getName() + "." + c.getName() + "=" + referencedTable.getName() + "." + c.getName()); } } for (Iterator<Column> j = table.getColumnIterator(); j.hasNext();) { Column column = j.next(); if(fks.get(column.getName())== null ){ ColumnToField field = setupColumnToField(table, column); rdbms.getSQLMapping().getColumnToFieldMap().add(field); ProjectionMapping projectionMapping = setupProjectionMapping(column); join.getProjectionMappings().add(projectionMapping); } } com.redhat.lightblue.metadata.rdbms.model.Table rdbmTable = new com.redhat.lightblue.metadata.rdbms.model.Table(); rdbmTable.setName(table.getName()); join.getTables().add(rdbmTable); } }