use of org.datanucleus.store.rdbms.exceptions.NoTableManagedException in project datanucleus-rdbms by datanucleus.
the class ColumnCreator method createColumnsForField.
/**
* Method to create the column(s) for a field in either a join table or for a reference field.
* @param javaType The java type of the field being stored
* @param mapping The JavaTypeMapping (if existing, otherwise created and returned by this method)
* @param table The table to insert the columns into (join table, or primary table (if ref field))
* @param storeMgr Manager for the store
* @param mmd MetaData for the field (or null if a collection field)
* @param isPrimaryKey Whether to create the columns as part of the PK
* @param isNullable Whether the columns should be nullable
* @param serialised Whether the field is serialised
* @param embedded Whether the field is embedded
* @param fieldRole The role of the field (when part of a join table)
* @param columnMetaData MetaData for the column(s)
* @param clr ClassLoader resolver
* @param isReferenceField Whether this field is part of a reference field
* @param ownerTable Table of the owner of this member (optional, for when the member is embedded)
* @return The JavaTypeMapping for the table
*/
public static JavaTypeMapping createColumnsForField(Class javaType, JavaTypeMapping mapping, Table table, RDBMSStoreManager storeMgr, AbstractMemberMetaData mmd, boolean isPrimaryKey, boolean isNullable, boolean serialised, boolean embedded, FieldRole fieldRole, ColumnMetaData[] columnMetaData, ClassLoaderResolver clr, boolean isReferenceField, Table ownerTable) {
IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
if (mapping instanceof ReferenceMapping || mapping instanceof PersistableMapping) {
// PC/interface/Object mapping
JavaTypeMapping container = mapping;
if (mapping instanceof ReferenceMapping) {
// Interface/Object has child mappings for each implementation
container = storeMgr.getMappingManager().getMapping(javaType, serialised, embedded, mmd != null ? mmd.getFullFieldName() : null);
((ReferenceMapping) mapping).addJavaTypeMapping(container);
}
// Get the table that we want our column to be a FK to. This could be the owner table, element table, key table, value table etc
DatastoreClass destinationTable = null;
try {
destinationTable = storeMgr.getDatastoreClass(javaType.getName(), clr);
} catch (NoTableManagedException ntme) {
if (ownerTable != null && ownerTable instanceof DatastoreClass) {
destinationTable = (DatastoreClass) ownerTable;
} else {
throw ntme;
}
}
if (destinationTable == null) {
// Maybe the owner hasn't got its own table (e.g "subclass-table" or "complete-table"+abstract)
// Alternate is when we have an embedded type which itself has an embedded collection - not catered for at all currently
AbstractClassMetaData ownerCmd = storeMgr.getMetaDataManager().getMetaDataForClass(javaType, clr);
if (ownerCmd.getBaseAbstractClassMetaData().getInheritanceMetaData().getStrategy() == InheritanceStrategy.COMPLETE_TABLE) {
// COMPLETE-TABLE but abstract root, so find one of the subclasses with a table and use that for now
Collection<String> ownerSubclassNames = storeMgr.getSubClassesForClass(javaType.getName(), true, clr);
if (ownerSubclassNames != null && ownerSubclassNames.size() > 0) {
for (String ownerSubclassName : ownerSubclassNames) {
ownerCmd = storeMgr.getMetaDataManager().getMetaDataForClass(ownerSubclassName, clr);
try {
destinationTable = storeMgr.getDatastoreClass(ownerSubclassName, clr);
} catch (NoTableManagedException ntme) {
}
if (destinationTable != null) {
break;
}
}
}
} else {
AbstractClassMetaData[] ownerCmds = storeMgr.getClassesManagingTableForClass(ownerCmd, clr);
if (ownerCmds == null || ownerCmds.length == 0) {
throw new NucleusUserException(Localiser.msg("057023", javaType.getName())).setFatal();
}
// Use the first one since they should all have the same id column(s)
destinationTable = storeMgr.getDatastoreClass(ownerCmds[0].getFullClassName(), clr);
}
}
if (destinationTable != null) {
// Foreign-Key to the destination table ID mapping
JavaTypeMapping m = destinationTable.getIdMapping();
// For each column in the destination mapping, add a column here
ColumnMetaDataContainer columnContainer = null;
if (columnMetaData != null && columnMetaData.length > 0) {
columnContainer = (ColumnMetaDataContainer) columnMetaData[0].getParent();
}
CorrespondentColumnsMapper correspondentColumnsMapping = new CorrespondentColumnsMapper(columnContainer, table, columnMetaData, m, true);
for (int i = 0; i < m.getNumberOfDatastoreMappings(); i++) {
JavaTypeMapping refDatastoreMapping = storeMgr.getMappingManager().getMapping(m.getDatastoreMapping(i).getJavaTypeMapping().getJavaType());
ColumnMetaData colmd = correspondentColumnsMapping.getColumnMetaDataByIdentifier(m.getDatastoreMapping(i).getColumn().getIdentifier());
try {
DatastoreIdentifier identifier = null;
if (fieldRole == FieldRole.ROLE_MAP_KEY && columnContainer == null) {
// Map KEY field and no metadata defined
if (isReferenceField) {
// Create reference identifier
identifier = idFactory.newReferenceFieldIdentifier(mmd, storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(javaType, clr), m.getDatastoreMapping(i).getColumn().getIdentifier(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(javaType), fieldRole);
} else {
// Create join table identifier
AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
// TODO If the mmd is an "embedded" type this can create invalid identifiers
// TODO Cater for more than 1 related field
identifier = idFactory.newJoinTableFieldIdentifier(mmd, relatedMmds != null ? relatedMmds[0] : null, m.getDatastoreMapping(i).getColumn().getIdentifier(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(javaType), fieldRole);
}
} else {
if (colmd.getName() == null) {
// User hasn't provided a name, so we use default naming
if (isReferenceField) {
// Create reference identifier
identifier = idFactory.newReferenceFieldIdentifier(mmd, storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(javaType, clr), m.getDatastoreMapping(i).getColumn().getIdentifier(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(javaType), fieldRole);
} else {
// Create join table identifier
AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
// TODO If the mmd is an "embedded" type this can create invalid identifiers
// TODO Cater for more than 1 related field
identifier = idFactory.newJoinTableFieldIdentifier(mmd, relatedMmds != null ? relatedMmds[0] : null, m.getDatastoreMapping(i).getColumn().getIdentifier(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(javaType), fieldRole);
}
} else {
// User defined name, so we use that.
identifier = idFactory.newColumnIdentifier(colmd.getName());
}
}
// Only add the column if not currently present
Column column = table.addColumn(javaType.getName(), identifier, refDatastoreMapping, colmd);
m.getDatastoreMapping(i).getColumn().copyConfigurationTo(column);
if (isPrimaryKey) {
column.setPrimaryKey();
}
if (isNullable) {
column.setNullable(true);
}
storeMgr.getMappingManager().createDatastoreMapping(refDatastoreMapping, column, m.getDatastoreMapping(i).getJavaTypeMapping().getJavaTypeForDatastoreMapping(i));
} catch (DuplicateColumnException ex) {
throw new NucleusUserException("Cannot create column for field " + mmd.getFullFieldName() + " column metadata " + colmd, ex);
}
try {
((PersistableMapping) container).addJavaTypeMapping(refDatastoreMapping);
} catch (ClassCastException e) {
throw new NucleusUserException("Failed to create column for field " + mmd.getFullFieldName() + ". Cannot cast mapping to PersistableMapping.", e);
}
}
}
} else {
// Non-PC mapping
// Add column for the field
Column column = null;
ColumnMetaData colmd = null;
if (columnMetaData != null && columnMetaData.length > 0) {
colmd = columnMetaData[0];
}
DatastoreIdentifier identifier = null;
if (colmd != null && colmd.getName() != null) {
// User specified name
identifier = idFactory.newColumnIdentifier(colmd.getName());
} else {
// No user-supplied name so generate one
identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(javaType), fieldRole);
}
column = table.addColumn(javaType.getName(), identifier, mapping, colmd);
storeMgr.getMappingManager().createDatastoreMapping(mapping, column, mapping.getJavaTypeForDatastoreMapping(0));
if (isNullable) {
column.setNullable(true);
}
}
return mapping;
}
use of org.datanucleus.store.rdbms.exceptions.NoTableManagedException in project datanucleus-rdbms by datanucleus.
the class MapTable method getExpectedForeignKeys.
/**
* Accessor for the expected foreign keys for this table.
* @param clr The ClassLoaderResolver
* @return The expected foreign keys.
*/
public List getExpectedForeignKeys(ClassLoaderResolver clr) {
assertIsInitialized();
boolean autoMode = false;
if (storeMgr.getStringProperty(RDBMSPropertyNames.PROPERTY_RDBMS_CONSTRAINT_CREATE_MODE).equals("DataNucleus")) {
autoMode = true;
}
ArrayList foreignKeys = new ArrayList();
try {
// FK from join table to owner table
DatastoreClass referencedTable = storeMgr.getDatastoreClass(ownerType, clr);
if (referencedTable != null) {
// Take <foreign-key> from <join>
ForeignKeyMetaData fkmd = null;
if (mmd.getJoinMetaData() != null) {
fkmd = mmd.getJoinMetaData().getForeignKeyMetaData();
}
if (fkmd != null || autoMode) {
ForeignKey fk = new ForeignKey(ownerMapping, dba, referencedTable, true);
fk.setForMetaData(fkmd);
foreignKeys.add(fk);
}
}
if (!isSerialisedValuePC()) {
if (isEmbeddedValuePC()) {
// Add any FKs for the fields of the (embedded) value
EmbeddedValuePCMapping embMapping = (EmbeddedValuePCMapping) valueMapping;
for (int i = 0; i < embMapping.getNumberOfJavaTypeMappings(); i++) {
JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
AbstractMemberMetaData embFmd = embFieldMapping.getMemberMetaData();
if (ClassUtils.isReferenceType(embFmd.getType()) && embFieldMapping instanceof ReferenceMapping) {
// Field is a reference type, so add a FK to the table of the PC for each PC implementation
Collection fks = TableUtils.getForeignKeysForReferenceField(embFieldMapping, embFmd, autoMode, storeMgr, clr);
foreignKeys.addAll(fks);
} else if (storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(embFmd.getType(), clr) != null && embFieldMapping.getNumberOfDatastoreMappings() > 0 && embFieldMapping instanceof PersistableMapping) {
// Field is for a PC class with the FK at this side, so add a FK to the table of this PC
ForeignKey fk = TableUtils.getForeignKeyForPCField(embFieldMapping, embFmd, autoMode, storeMgr, clr);
if (fk != null) {
foreignKeys.add(fk);
}
}
}
} else if (mmd.getMap().valueIsPersistent()) {
// FK from join table to value table
referencedTable = storeMgr.getDatastoreClass(mmd.getMap().getValueType(), clr);
if (referencedTable != null) {
// Take <foreign-key> from <value>
ForeignKeyMetaData fkmd = null;
if (mmd.getValueMetaData() != null) {
fkmd = mmd.getValueMetaData().getForeignKeyMetaData();
}
if (fkmd != null || autoMode) {
ForeignKey fk = new ForeignKey(valueMapping, dba, referencedTable, true);
fk.setForMetaData(fkmd);
foreignKeys.add(fk);
}
}
}
}
if (!isSerialisedKeyPC()) {
if (isEmbeddedKeyPC()) {
// Add any FKs for the fields of the (embedded) key
EmbeddedKeyPCMapping embMapping = (EmbeddedKeyPCMapping) keyMapping;
for (int i = 0; i < embMapping.getNumberOfJavaTypeMappings(); i++) {
JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
AbstractMemberMetaData embFmd = embFieldMapping.getMemberMetaData();
if (ClassUtils.isReferenceType(embFmd.getType()) && embFieldMapping instanceof ReferenceMapping) {
// Field is a reference type, so add a FK to the table of the PC for each PC implementation
Collection fks = TableUtils.getForeignKeysForReferenceField(embFieldMapping, embFmd, autoMode, storeMgr, clr);
foreignKeys.addAll(fks);
} else if (storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(embFmd.getType(), clr) != null && embFieldMapping.getNumberOfDatastoreMappings() > 0 && embFieldMapping instanceof PersistableMapping) {
// Field is for a PC class with the FK at this side, so add a FK to the table of this PC
ForeignKey fk = TableUtils.getForeignKeyForPCField(embFieldMapping, embFmd, autoMode, storeMgr, clr);
if (fk != null) {
foreignKeys.add(fk);
}
}
}
} else if (mmd.getMap().keyIsPersistent()) {
// FK from join table to key table
referencedTable = storeMgr.getDatastoreClass(mmd.getMap().getKeyType(), clr);
if (referencedTable != null) {
// Take <foreign-key> from <key>
ForeignKeyMetaData fkmd = null;
if (mmd.getKeyMetaData() != null) {
fkmd = mmd.getKeyMetaData().getForeignKeyMetaData();
}
if (fkmd != null || autoMode) {
ForeignKey fk = new ForeignKey(keyMapping, dba, referencedTable, true);
fk.setForMetaData(fkmd);
foreignKeys.add(fk);
}
}
}
}
} catch (NoTableManagedException e) {
// expected when no table exists
}
return foreignKeys;
}
use of org.datanucleus.store.rdbms.exceptions.NoTableManagedException in project datanucleus-rdbms by datanucleus.
the class PersistableJoinTable method getExpectedForeignKeys.
/**
* Accessor for the expected foreign keys for this table.
* @param clr The ClassLoaderResolver
* @return The expected foreign keys.
*/
public List getExpectedForeignKeys(ClassLoaderResolver clr) {
assertIsInitialized();
// Find the mode that we're operating in for FK addition
boolean autoMode = false;
if (storeMgr.getStringProperty(RDBMSPropertyNames.PROPERTY_RDBMS_CONSTRAINT_CREATE_MODE).equals("DataNucleus")) {
autoMode = true;
}
ArrayList foreignKeys = new ArrayList();
try {
// FK from join table to owner table
DatastoreClass referencedTable = storeMgr.getDatastoreClass(mmd.getClassName(true), clr);
if (referencedTable != null) {
// Single owner table, so add a single FK to the owner as appropriate
// Take <foreign-key> from <join>
ForeignKey fk = null;
ForeignKeyMetaData fkmd = null;
if (mmd.getJoinMetaData() != null) {
fkmd = mmd.getJoinMetaData().getForeignKeyMetaData();
}
if (fkmd != null || autoMode) {
fk = new ForeignKey(ownerMapping, dba, referencedTable, true);
fk.setForMetaData(fkmd);
}
if (fk != null) {
foreignKeys.add(fk);
}
}
// FK from join table to related table
referencedTable = storeMgr.getDatastoreClass(mmd.getTypeName(), clr);
if (referencedTable != null) {
// Take <foreign-key> from <field>
ForeignKey fk = null;
ForeignKeyMetaData fkmd = mmd.getForeignKeyMetaData();
if (fkmd != null || autoMode) {
fk = new ForeignKey(relatedMapping, dba, referencedTable, true);
fk.setForMetaData(fkmd);
}
if (fk != null) {
foreignKeys.add(fk);
}
}
} catch (NoTableManagedException e) {
// expected when no table exists
}
return foreignKeys;
}
use of org.datanucleus.store.rdbms.exceptions.NoTableManagedException in project datanucleus-rdbms by datanucleus.
the class TableImpl method getExpectedForeignKeys.
// ----------------------- Internal Implementation Methods -----------------
/**
* Accessor for the expected foreign keys for this table in the datastore.
* Currently only checks the columns for referenced tables (i.e relationships) and returns those.
* @param clr The ClassLoaderResolver
* @return List of foreign keys.
*/
public List<ForeignKey> getExpectedForeignKeys(ClassLoaderResolver clr) {
assertIsInitialized();
// The following Set is to avoid the duplicate usage of columns that have already been used in conjunction with another column
Set<Column> colsInFKs = new HashSet<>();
List<ForeignKey> foreignKeys = new ArrayList<>();
Iterator i = columns.iterator();
while (i.hasNext()) {
Column col = (Column) i.next();
if (!colsInFKs.contains(col)) {
try {
DatastoreClass referencedTable = storeMgr.getDatastoreClass(col.getStoredJavaType(), clr);
if (referencedTable != null) {
for (int j = 0; j < col.getJavaTypeMapping().getNumberOfDatastoreMappings(); j++) {
colsInFKs.add(col.getJavaTypeMapping().getDatastoreMapping(j).getColumn());
}
ForeignKey fk = new ForeignKey(col.getJavaTypeMapping(), dba, referencedTable, true);
foreignKeys.add(fk);
}
} catch (NoTableManagedException e) {
// expected when no table exists
}
}
}
return foreignKeys;
}
use of org.datanucleus.store.rdbms.exceptions.NoTableManagedException in project datanucleus-rdbms by datanucleus.
the class RDBMSStoreManager method getDatastoreClass.
/**
* Returns the primary datastore table serving as backing for the given class.
* If the class is not yet known to the store manager, {@link #manageClasses} is called
* to add it. Classes which have inheritance strategy of "new-table" and
* "superclass-table" will return a table here, whereas "subclass-table" will
* return null since it doesn't have a table as such.
* <p>
* @param className Name of the class whose table is be returned.
* @param clr The ClassLoaderResolver
* @return The corresponding class table.
* @exception NoTableManagedException If the given class has no table managed in the database.
*/
public DatastoreClass getDatastoreClass(String className, ClassLoaderResolver clr) {
DatastoreClass ct = null;
if (className == null) {
NucleusLogger.PERSISTENCE.error(Localiser.msg("032015"));
return null;
}
schemaLock.readLock().lock();
try {
StoreData sd = storeDataMgr.get(className);
if (sd != null && sd instanceof RDBMSStoreData) {
ct = (DatastoreClass) sd.getTable();
if (ct != null) {
// Class known about
return ct;
}
}
} finally {
schemaLock.readLock().unlock();
}
// Class not known so consider adding it to our list of supported classes.
// Currently we only consider PC classes
boolean toBeAdded = false;
if (clr != null) {
Class cls = clr.classForName(className);
ApiAdapter api = getApiAdapter();
if (cls != null && !cls.isInterface() && api.isPersistable(cls)) {
toBeAdded = true;
}
} else {
toBeAdded = true;
}
boolean classKnown = false;
if (toBeAdded) {
// Add the class to our supported list
manageClasses(clr, className);
// Retry
schemaLock.readLock().lock();
try {
StoreData sd = storeDataMgr.get(className);
if (sd != null && sd instanceof RDBMSStoreData) {
classKnown = true;
ct = (DatastoreClass) sd.getTable();
}
} finally {
schemaLock.readLock().unlock();
}
}
// Note : "subclass-table" inheritance strategies will return null from this method
if (!classKnown && ct == null) {
throw new NoTableManagedException(className);
}
return ct;
}
Aggregations