Speichern eines geänderten Schemas über die Api
Author: robert_wieser
Publication Date: 3/6/2012 9:24
Hallo Community,
wie kann ich über die Api eine Spalte in einer Tabelle hinzufügen und anschließende die Tabelle speichern?
Meinen Kode-Ansatz folgt.
Vielen Dank für eure Beitrage
Robert
de.espirit.or.schema.Schema orSchema = tableTemplate.getSchema().getOrSchema();
EntityType entityType = tableTemplate.getEntityType();
String entityTypeName = entityType.getName();
Table table = orSchema.getTable(entityTypeName);
Column col = new ColumnImpl(colName, 64, Column.TypeCode.TYPE_STRING, false);
Schema templateSchema = tableTemplate.getSchema();
try {
templateSchema.setLock(true);
table.add(col);
templateSchema.save();
templateSchema.setLock(false);
} catch (LockException e) {
e.printStackTrace();
} catch (ElementDeletedException e) {
e.printStackTrace();
}
Tags: database, datenbank
-
Author: stephan - 3/30/2012 10:07
Hallo Robert,
eine Lösung könnte z.B. so aussehen:
// run as contextscript on tabletemplate
TableTemplate tableTemplate = (TableTemplate) context.getStoreElement();
Schema _schema = tableTemplate.getSchema();
String _entityTypeName = "MyTable";
String _attributeName = "MyAttribute";
try {
_schema.setLock(true, false);
final Session orSession = _schema.getSession(false);
final de.espirit.or.schema.Schema orSchema = orSession.getSchema();
final EntityType entityType = orSchema.getEntityType(_entityTypeName);
SimpleAttribute _attribute = entityType.createSimpleAttribute(_attributeName, String.class);
_attribute.setSize(64);
orSession.syncSchemaWithDB(orSchema, false);
_schema.setOrSchema(orSchema);
_schema.save();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
_schema.setLock(false, false);
} catch (Exception ignore) {
// catch exception
}
}
Was bei dir sicherlich noch fehlte war der Sync des geänderten orSchemas in der aktuellen Session, sowie die Zuweiseung zum Templatestore-Schema.
Viele Grüße
Jörg
0
Bitte melden Sie sich an, um einen Kommentar zu hinterlassen.
Kommentare
1 Kommentar