use of org.datanucleus.metadata.ClassMetaData in project datanucleus-rdbms by datanucleus.
the class ClassTable method manageMembers.
/**
* Goes through all specified members for the specified class and adds a mapping for each.
* Ignores primary-key fields which are added elsewhere.
* @param theCmd ClassMetaData for the class to be managed
* @param clr The ClassLoaderResolver
* @param mmds the fields/properties to manage
*/
private void manageMembers(AbstractClassMetaData theCmd, ClassLoaderResolver clr, AbstractMemberMetaData[] mmds) {
// Go through the fields for this class and add columns for them
for (int fieldNumber = 0; fieldNumber < mmds.length; fieldNumber++) {
// Primary key fields are added by the initialisePK method
AbstractMemberMetaData mmd = mmds[fieldNumber];
if (!mmd.isPrimaryKey()) {
if (managesMember(mmd.getFullFieldName())) {
if (!mmd.getClassName(true).equals(theCmd.getFullClassName())) {
// Field already managed by this table so maybe we are overriding a superclass
JavaTypeMapping fieldMapping = getMappingForMemberName(mmd.getFullFieldName());
ColumnMetaData[] colmds = mmd.getColumnMetaData();
if (colmds != null && colmds.length > 0) {
// Apply this set of ColumnMetaData to the existing mapping
int colnum = 0;
IdentifierFactory idFactory = getStoreManager().getIdentifierFactory();
for (int i = 0; i < fieldMapping.getNumberOfDatastoreMappings(); i++) {
Column col = fieldMapping.getDatastoreMapping(i).getColumn();
col.setIdentifier(idFactory.newColumnIdentifier(colmds[colnum].getName()));
col.setColumnMetaData(colmds[colnum]);
colnum++;
if (colnum == colmds.length) {
// Reached end of specified metadata
break;
}
}
// TODO Change this to reflect that we have updated the previous mapping
logMapping(mmd.getFullFieldName(), fieldMapping);
}
}
} else {
// Manage the field if not already managed (may already exist if overriding a superclass field)
if (mmd.getPersistenceModifier() == FieldPersistenceModifier.PERSISTENT) {
boolean isPrimary = true;
if (mmd.getTable() != null && mmd.getJoinMetaData() == null) {
// Field has a table specified and is not a 1-N with join table
// so is mapped to a secondary table
isPrimary = false;
}
if (isPrimary) {
// Add the field to this table
JavaTypeMapping fieldMapping = storeMgr.getMappingManager().getMapping(this, mmd, clr, FieldRole.ROLE_FIELD);
if (theCmd != cmd && theCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUPERCLASS_TABLE && fieldMapping.getNumberOfDatastoreMappings() > 0) {
// Field is for a subclass and so column(s) has to either allow nulls, or have default
int numCols = fieldMapping.getNumberOfDatastoreMappings();
for (int colNum = 0; colNum < numCols; colNum++) {
Column col = fieldMapping.getDatastoreMapping(colNum).getColumn();
if (col.getDefaultValue() == null && !col.isNullable()) {
// Column needs to be nullable
NucleusLogger.DATASTORE_SCHEMA.debug("Member " + mmd.getFullFieldName() + " uses superclass-table yet the field is not marked as nullable " + " nor does it have a default value, so setting the column as nullable");
col.setNullable(true);
}
}
}
addMemberMapping(fieldMapping);
} else {
// Add the field to the appropriate secondary table
if (secondaryTables == null) {
secondaryTables = new HashMap();
}
SecondaryTable secTable = secondaryTables.get(mmd.getTable());
if (secTable == null) {
// Secondary table doesnt exist yet so create it to users specifications.
List<JoinMetaData> joinmds = theCmd.getJoinMetaData();
JoinMetaData theJoinMD = null;
if (joinmds != null) {
for (JoinMetaData joinmd : joinmds) {
if (joinmd.getTable().equalsIgnoreCase(mmd.getTable()) && (joinmd.getCatalog() == null || (joinmd.getCatalog() != null && joinmd.getCatalog().equalsIgnoreCase(mmd.getCatalog()))) && (joinmd.getSchema() == null || (joinmd.getSchema() != null && joinmd.getSchema().equalsIgnoreCase(mmd.getSchema())))) {
theJoinMD = joinmd;
break;
}
}
}
DatastoreIdentifier secTableIdentifier = storeMgr.getIdentifierFactory().newTableIdentifier(mmd.getTable());
// Use specified catalog, else take catalog of the owning table
String catalogName = mmd.getCatalog();
if (catalogName == null) {
catalogName = getCatalogName();
}
// Use specified schema, else take schema of the owning table
String schemaName = mmd.getSchema();
if (schemaName == null) {
schemaName = getSchemaName();
}
secTableIdentifier.setCatalogName(catalogName);
secTableIdentifier.setSchemaName(schemaName);
secTable = new SecondaryTable(secTableIdentifier, storeMgr, this, theJoinMD, clr);
secTable.preInitialize(clr);
secTable.initialize(clr);
secTable.postInitialize(clr);
secondaryTables.put(mmd.getTable(), secTable);
}
secTable.addMemberMapping(storeMgr.getMappingManager().getMapping(secTable, mmd, clr, FieldRole.ROLE_FIELD));
}
} else if (mmd.getPersistenceModifier() != FieldPersistenceModifier.TRANSACTIONAL) {
throw new NucleusException(Localiser.msg("057006", mmd.getName())).setFatal();
}
// Calculate if we need a FK adding due to a 1-N (FK) relationship
boolean needsFKToContainerOwner = false;
RelationType relationType = mmd.getRelationType(clr);
if (relationType == RelationType.ONE_TO_MANY_BI) {
AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
if (mmd.getJoinMetaData() == null && relatedMmds[0].getJoinMetaData() == null) {
needsFKToContainerOwner = true;
}
} else if (relationType == RelationType.ONE_TO_MANY_UNI && !mmd.isSingleCollection()) {
if (mmd.getJoinMetaData() == null) {
needsFKToContainerOwner = true;
}
}
if (needsFKToContainerOwner) {
// 1-N uni/bidirectional using FK, so update the element side with a FK
if ((mmd.getCollection() != null && !SCOUtils.collectionHasSerialisedElements(mmd)) || (mmd.getArray() != null && !SCOUtils.arrayIsStoredInSingleColumn(mmd, storeMgr.getMetaDataManager()))) {
// 1-N ForeignKey collection/array, so add FK to element table
AbstractClassMetaData elementCmd = null;
if (mmd.hasCollection()) {
// Collection
elementCmd = storeMgr.getMetaDataManager().getMetaDataForClass(mmd.getCollection().getElementType(), clr);
} else {
// Array
elementCmd = storeMgr.getMetaDataManager().getMetaDataForClass(mmd.getType().getComponentType(), clr);
}
if (elementCmd == null) {
String[] implClassNames = storeMgr.getMetaDataManager().getClassesImplementingInterface(mmd.getCollection().getElementType(), clr);
if (implClassNames != null && implClassNames.length > 0) {
// Collection/array of interface type so apply callback to all implementation types
AbstractClassMetaData[] elementCmds = new AbstractClassMetaData[implClassNames.length];
for (int i = 0; i < implClassNames.length; i++) {
elementCmds[i] = storeMgr.getMetaDataManager().getMetaDataForClass(implClassNames[i], clr);
}
// Run callbacks for each of the element classes.
for (int i = 0; i < elementCmds.length; i++) {
storeMgr.addSchemaCallback(elementCmds[i].getFullClassName(), mmd);
DatastoreClass dc = storeMgr.getDatastoreClass(elementCmds[i].getFullClassName(), clr);
if (dc == null) {
throw new NucleusException("Unable to add foreign-key to " + elementCmds[i].getFullClassName() + " to " + this + " since element has no table!");
}
if (dc instanceof ClassTable) {
ClassTable ct = (ClassTable) dc;
if (ct.isInitialized()) {
// if the target table is already initialized, run the callbacks
ct.runCallBacks(clr);
}
} else {
NucleusLogger.DATASTORE_SCHEMA.info("Table " + toString() + " has to manage member " + mmd.getFullFieldName() + " yet the related element uses a VIEW so not remotely adding element FK owner column; assumed to be part of the VIEW definition");
}
}
} else {
// Elements that are reference types or non-PC will come through here
if (mmd.hasCollection()) {
NucleusLogger.METADATA.warn(Localiser.msg("057016", theCmd.getFullClassName(), mmd.getCollection().getElementType()));
} else {
NucleusLogger.METADATA.warn(Localiser.msg("057014", theCmd.getFullClassName(), mmd.getType().getComponentType().getName()));
}
}
} else {
AbstractClassMetaData[] elementCmds = null;
// TODO : Cater for interface elements, and get the metadata for the implementation classes here
if (elementCmd.getBaseAbstractClassMetaData().getInheritanceMetaData().getStrategy() == InheritanceStrategy.COMPLETE_TABLE) {
// COMPLETE-TABLE inheritance in element, so really need FK in each!
Collection<String> elementSubclassNames = storeMgr.getSubClassesForClass(elementCmd.getFullClassName(), true, clr);
elementCmds = new ClassMetaData[elementSubclassNames != null ? 1 + elementSubclassNames.size() : 1];
int elemNo = 0;
elementCmds[elemNo++] = elementCmd;
if (elementSubclassNames != null) {
for (String elementSubclassName : elementSubclassNames) {
AbstractClassMetaData elemSubCmd = storeMgr.getMetaDataManager().getMetaDataForClass(elementSubclassName, clr);
elementCmds[elemNo++] = elemSubCmd;
}
}
} else if (elementCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE) {
elementCmds = storeMgr.getClassesManagingTableForClass(elementCmd, clr);
} else {
elementCmds = new ClassMetaData[1];
elementCmds[0] = elementCmd;
}
ElementMetaData elemmd = mmd.getElementMetaData();
if (elemmd != null && !StringUtils.isWhitespace(elemmd.getTable())) {
DatastoreIdentifier requiredTableId = storeMgr.getIdentifierFactory().newTableIdentifier(elemmd.getTable());
DatastoreClass requiredTable = storeMgr.getDatastoreClass(requiredTableId);
if (requiredTable != null) {
// TODO Respect specification of table in ElementMetaData rather than just defaulting to table of element type
// Note that this will need updates to FKListStore, FKSetStore etc to look for the table
NucleusLogger.GENERAL.warn("Member=" + mmd.getFullFieldName() + " has 1-N FK with required table=" + requiredTable + " : we don't currently support specification of the element table, and always take the default table for the element type");
/*for (int i=0;i<elementCmds.length;i++)
{
AbstractClassMetaData theElementCmd = elementCmds[i];
while (theElementCmd != null)
{
if (requiredTable.managesClass(theElementCmd.getFullClassName()))
{
if (theElementCmd != elementCmds[i])
{
elementCmds = new ClassMetaData[1];
elementCmds[0] = theElementCmd;
break;
}
}
theElementCmd = theElementCmd.getSuperAbstractClassMetaData();
}
}*/
} else {
NucleusLogger.DATASTORE_SCHEMA.warn("Member " + mmd.getFullFieldName() + " specified element FK in table=" + elemmd.getTable() + " but table not known. Ignoring.");
}
}
// Run callbacks for each of the element classes
for (int i = 0; i < elementCmds.length; i++) {
storeMgr.addSchemaCallback(elementCmds[i].getFullClassName(), mmd);
DatastoreClass dc = storeMgr.getDatastoreClass(elementCmds[i].getFullClassName(), clr);
if (// If dc is null then we assume the (possible) element is abstract so no FK needed
dc != null) {
if (dc instanceof ClassTable) {
ClassTable ct = (ClassTable) dc;
if (ct.isInitialized()) {
// if the target table is already initialized, run the callbacks
ct.runCallBacks(clr);
}
} else {
NucleusLogger.DATASTORE_SCHEMA.info("Table " + toString() + " has to manage member " + mmd.getFullFieldName() + " yet the related element uses a VIEW so not remotely adding element FK owner column; assumed to be part of the VIEW definition");
}
}
}
}
} else if (mmd.getMap() != null && !SCOUtils.mapHasSerialisedKeysAndValues(mmd)) {
// 1-N ForeignKey map, so add FK to value table
if (mmd.getKeyMetaData() != null && mmd.getKeyMetaData().getMappedBy() != null) {
// Key is stored in the value table so add the FK to the value table
AbstractClassMetaData valueCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(mmd.getMap().getValueType(), clr);
if (valueCmd == null) {
// Interface elements will come through here and java.lang.String and others as well
NucleusLogger.METADATA.warn(Localiser.msg("057018", theCmd.getFullClassName(), mmd.getMap().getValueType()));
} else {
AbstractClassMetaData[] valueCmds = null;
// TODO : Cater for interface values, and get the metadata for the implementation classes here
if (valueCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE) {
valueCmds = storeMgr.getClassesManagingTableForClass(valueCmd, clr);
} else {
valueCmds = new ClassMetaData[1];
valueCmds[0] = valueCmd;
}
// Run callbacks for each of the value classes.
for (int i = 0; i < valueCmds.length; i++) {
storeMgr.addSchemaCallback(valueCmds[i].getFullClassName(), mmd);
DatastoreClass dc = storeMgr.getDatastoreClass(valueCmds[i].getFullClassName(), clr);
if (dc instanceof ClassTable) {
ClassTable ct = (ClassTable) dc;
if (ct.isInitialized()) {
// if the target table is already initialized, run the callbacks
ct.runCallBacks(clr);
}
} else {
NucleusLogger.DATASTORE_SCHEMA.info("Table " + toString() + " has to manage member " + mmd.getFullFieldName() + " yet the related value uses a VIEW so not remotely adding value owner FK column; assumed to be part of the VIEW definition");
}
}
}
} else if (mmd.getValueMetaData() != null && mmd.getValueMetaData().getMappedBy() != null) {
// Value is stored in the key table so add the FK to the key table
AbstractClassMetaData keyCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(mmd.getMap().getKeyType(), clr);
if (keyCmd == null) {
// Interface elements will come through here and java.lang.String and others as well
NucleusLogger.METADATA.warn(Localiser.msg("057019", theCmd.getFullClassName(), mmd.getMap().getKeyType()));
} else {
AbstractClassMetaData[] keyCmds = null;
// TODO : Cater for interface keys, and get the metadata for the implementation classes here
if (keyCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE) {
keyCmds = storeMgr.getClassesManagingTableForClass(keyCmd, clr);
} else {
keyCmds = new ClassMetaData[1];
keyCmds[0] = keyCmd;
}
// Run callbacks for each of the key classes.
for (int i = 0; i < keyCmds.length; i++) {
storeMgr.addSchemaCallback(keyCmds[i].getFullClassName(), mmd);
DatastoreClass dc = storeMgr.getDatastoreClass(keyCmds[i].getFullClassName(), clr);
if (dc instanceof ClassTable) {
ClassTable ct = (ClassTable) dc;
if (ct.isInitialized()) {
// if the target table is already initialized, run the callbacks
ct.runCallBacks(clr);
}
} else {
NucleusLogger.DATASTORE_SCHEMA.info("Table " + toString() + " has to manage member " + mmd.getFullFieldName() + " yet the related key uses a VIEW so not remotely adding key FK owner column; assumed to be part of the VIEW definition");
}
}
}
}
}
}
}
}
}
}
use of org.datanucleus.metadata.ClassMetaData in project datanucleus-rdbms by datanucleus.
the class PersistableMapping method prepareDatastoreMapping.
/**
* Method to prepare the PC mapping and add its associated datastore mappings.
* @param clr The ClassLoaderResolver
*/
protected void prepareDatastoreMapping(ClassLoaderResolver clr) {
if (roleForMember == FieldRole.ROLE_COLLECTION_ELEMENT) {
// TODO Handle creation of columns in join table for collection of PCs
} else if (roleForMember == FieldRole.ROLE_ARRAY_ELEMENT) {
// TODO Handle creation of columns in join table for array of PCs
} else if (roleForMember == FieldRole.ROLE_MAP_KEY) {
// TODO Handle creation of columns in join table for map of PCs as keys
} else if (roleForMember == FieldRole.ROLE_MAP_VALUE) {
// TODO Handle creation of columns in join table for map of PCs as values
} else {
// Either one end of a 1-1 relation, or the N end of a N-1
AbstractClassMetaData refCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(mmd.getType(), clr);
JavaTypeMapping referenceMapping = null;
if (refCmd == null) {
// User stupidity
throw new NucleusUserException("You have a field " + mmd.getFullFieldName() + " that has type " + mmd.getTypeName() + " but this type has no known metadata. Your mapping is incorrect");
}
if (refCmd.getInheritanceMetaData() != null && refCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE) {
// Find the actual tables storing the other end (can be multiple subclasses)
AbstractClassMetaData[] cmds = storeMgr.getClassesManagingTableForClass(refCmd, clr);
if (cmds != null && cmds.length > 0) {
if (cmds.length > 1) {
// TODO Only log this when it is really necessary. In some situations it is fine
NucleusLogger.PERSISTENCE.warn("Field " + mmd.getFullFieldName() + " represents either a 1-1 relation, " + "or a N-1 relation where the other end uses \"subclass-table\" inheritance strategy and more " + "than 1 subclasses with a table. This is not fully supported");
}
} else {
// TODO Throw an exception ?
return;
}
// TODO We need a mapping for each of the possible subclass tables
referenceMapping = storeMgr.getDatastoreClass(cmds[0].getFullClassName(), clr).getIdMapping();
} else if (refCmd.getInheritanceMetaData() != null && refCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.COMPLETE_TABLE) {
// Find the other side of the relation
DatastoreClass refTable = null;
if (refCmd instanceof ClassMetaData && !((ClassMetaData) refCmd).isAbstract()) {
refTable = storeMgr.getDatastoreClass(refCmd.getFullClassName(), clr);
} else {
Collection<String> refSubclasses = storeMgr.getSubClassesForClass(refCmd.getFullClassName(), true, clr);
if (refSubclasses != null && !refSubclasses.isEmpty()) {
// if only 1 subclass then use that
String refSubclassName = refSubclasses.iterator().next();
refTable = storeMgr.getDatastoreClass(refSubclassName, clr);
if (refSubclasses.size() > 1) {
NucleusLogger.DATASTORE_SCHEMA.info("Field " + mmd.getFullFieldName() + " is a 1-1/N-1 relation and the other side had multiple possible classes " + "to which to create a foreign-key. Using first possible (" + refSubclassName + ")");
}
}
}
if (refTable != null) {
referenceMapping = refTable.getIdMapping();
} else {
throw new NucleusUserException("Field " + mmd.getFullFieldName() + " represents either a 1-1 relation, " + "or a N-1 relation where the other end uses \"complete-table\" inheritance strategy and either no table was found, or multiple possible tables!");
}
} else {
// Default is to use the ID of the related object
// TODO Add option to use a natural-id in the other class. Find the mapping using the targetColumnName
referenceMapping = storeMgr.getDatastoreClass(mmd.getType().getName(), clr).getIdMapping();
}
// Generate a mapping from the columns of the referenced object to this mapping's ColumnMetaData
CorrespondentColumnsMapper correspondentColumnsMapping = new CorrespondentColumnsMapper(mmd, table, referenceMapping, true);
// Find any related field where this is part of a bidirectional relation
RelationType relationType = mmd.getRelationType(clr);
boolean createDatastoreMappings = true;
if (relationType == RelationType.MANY_TO_ONE_BI) {
AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
// TODO Cater for more than 1 related field
createDatastoreMappings = (relatedMmds[0].getJoinMetaData() == null);
} else if (// TODO If join table then don't need this
relationType == RelationType.ONE_TO_ONE_BI) {
// Put the FK at the end without "mapped-by"
createDatastoreMappings = (mmd.getMappedBy() == null);
}
if (mmd.getJoinMetaData() != null && (relationType == RelationType.MANY_TO_ONE_UNI || relationType == RelationType.ONE_TO_ONE_UNI || relationType == RelationType.ONE_TO_ONE_BI)) {
if (relationType == RelationType.ONE_TO_ONE_UNI || relationType == RelationType.ONE_TO_ONE_BI) {
throw new NucleusUserException("We do not currently support 1-1 relations via join table : " + mmd.getFullFieldName());
}
// create join table
storeMgr.newJoinTable(table, mmd, clr);
} else {
// Loop through the datastore fields in the referenced class and create a datastore field for each
for (int i = 0; i < referenceMapping.getNumberOfDatastoreMappings(); i++) {
DatastoreMapping refDatastoreMapping = referenceMapping.getDatastoreMapping(i);
JavaTypeMapping mapping = storeMgr.getMappingManager().getMapping(refDatastoreMapping.getJavaTypeMapping().getJavaType());
this.addJavaTypeMapping(mapping);
// Create physical datastore columns where we require a FK link to the related table.
if (createDatastoreMappings) {
// Find the Column MetaData that maps to the referenced datastore field
ColumnMetaData colmd = correspondentColumnsMapping.getColumnMetaDataByIdentifier(refDatastoreMapping.getColumn().getIdentifier());
if (colmd == null) {
throw new NucleusUserException(Localiser.msg("041038", refDatastoreMapping.getColumn().getIdentifier(), toString())).setFatal();
}
// Create a Datastore field to equate to the referenced classes datastore field
MappingManager mmgr = storeMgr.getMappingManager();
Column col = mmgr.createColumn(mmd, table, mapping, colmd, refDatastoreMapping.getColumn(), clr);
// Add its datastore mapping
DatastoreMapping datastoreMapping = mmgr.createDatastoreMapping(mapping, col, refDatastoreMapping.getJavaTypeMapping().getJavaTypeForDatastoreMapping(i));
this.addDatastoreMapping(datastoreMapping);
} else {
mapping.setReferenceMapping(referenceMapping);
}
}
}
}
}
use of org.datanucleus.metadata.ClassMetaData in project datanucleus-api-jdo by datanucleus.
the class PackageMetadataImpl method newClassMetadata.
/* (non-Javadoc)
* @see javax.jdo.metadata.PackageMetadata#newClassMetadata(java.lang.Class)
*/
public ClassMetadata newClassMetadata(Class cls) {
if (cls.isInterface()) {
throw new JDOUserException("Canot create new class metadata for " + cls.getName() + " since it is an interface!");
}
ClassMetaData internalCmd = getInternal().newClassMetadata(ClassUtils.getClassNameForClass(cls));
ClassMetadataImpl cmd = new ClassMetadataImpl(internalCmd);
cmd.parent = this;
return cmd;
}
use of org.datanucleus.metadata.ClassMetaData in project datanucleus-api-jdo by datanucleus.
the class PackageMetadataImpl method newClassMetadata.
/* (non-Javadoc)
* @see javax.jdo.metadata.PackageMetadata#newClassMetadata(java.lang.String)
*/
public ClassMetadata newClassMetadata(String name) {
ClassMetaData internalCmd = getInternal().newClassMetadata(name);
ClassMetadataImpl cmd = new ClassMetadataImpl(internalCmd);
cmd.parent = this;
return cmd;
}
use of org.datanucleus.metadata.ClassMetaData in project datanucleus-api-jdo by datanucleus.
the class JDOPersistenceManagerFactory method getMetadata.
/**
* Method to return the (class) metadata object for the specified class, if there is
* metadata defined for that class.
* @param className Name of the class that we want metadata for
* @return The metadata
*/
public javax.jdo.metadata.TypeMetadata getMetadata(String className) {
MetaDataManager mmgr = nucleusContext.getMetaDataManager();
AbstractClassMetaData acmd = mmgr.getMetaDataForClass(className, nucleusContext.getClassLoaderResolver(null));
if (acmd == null) {
return null;
}
if (acmd instanceof ClassMetaData) {
return new ClassMetadataImpl((ClassMetaData) acmd);
}
return new InterfaceMetadataImpl((InterfaceMetaData) acmd);
}
Aggregations