use of org.datanucleus.metadata.JoinMetaData in project tests by datanucleus.
the class AnnotationTest method testCollectionJoinTable.
/**
* Test for use of collection with join table
*/
public void testCollectionJoinTable() {
ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Manager.class.getName(), clr);
String prefix = cmd1.getFullClassName() + " : ";
AbstractMemberMetaData fmd = cmd1.getMetaDataForMember("subordinates");
assertNotNull(prefix + "doesnt have required field", fmd);
CollectionMetaData colmd = fmd.getCollection();
assertNotNull(prefix + "CollectionMetaData is null!", colmd);
assertEquals(prefix + "CollectionMetaData elementType is incorrect", Employee.class.getName(), colmd.getElementType());
JoinMetaData joinmd = fmd.getJoinMetaData();
assertNotNull(prefix + "field \"subordinates\" has no join information!", joinmd);
assertEquals(prefix + "field \"subordinates\" is stored in wrong table", "MANAGER_EMPLOYEES", fmd.getTable());
assertEquals(prefix + "field \"subordinates\" join table has incorrect column", "MANAGER_ID", joinmd.getColumnName());
}
use of org.datanucleus.metadata.JoinMetaData 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.JoinMetaData in project datanucleus-rdbms by datanucleus.
the class SQLStatementHelper method getSQLTableForMappingOfTable.
/**
* Method to return the SQLTable where the specified mapping (in the same table group as the provided
* SQLTable) is defined. If the statement doesn't currently join to the required table then a join will
* be added. If the required table is a superclass table then the join will be INNER. If the required
* table is a secondary table then the join will be defined by the meta-data for the secondary table.
* If this table group is NOT the candidate table group then LEFT OUTER JOIN will be used.
* @param stmt The statement
* @param sqlTbl SQLTable to start from for the supplied mapping (may be in super-table, or secondary-table of this)
* @param mapping The mapping
* @return The SQLTable for this mapping (may have been added to the statement during this method)
*/
public static SQLTable getSQLTableForMappingOfTable(SQLStatement stmt, SQLTable sqlTbl, JavaTypeMapping mapping) {
Table table = sqlTbl.getTable();
if (table instanceof SecondaryDatastoreClass || table instanceof JoinTable) {
// Secondary/join tables have no inheritance so ought to be correct
if (mapping.getTable() != null) {
// Check there is no better table already present in the TableGroup for this mapping
// This can happen when we do a select of a join table and the element table is in the
// same table group, so hence already is present
SQLTable mappingSqlTbl = stmt.getTable(mapping.getTable(), sqlTbl.getGroupName());
if (mappingSqlTbl != null) {
return mappingSqlTbl;
}
}
return sqlTbl;
}
DatastoreClass sourceTbl = (DatastoreClass) sqlTbl.getTable();
DatastoreClass mappingTbl = null;
if (mapping.getTable() != null) {
mappingTbl = (DatastoreClass) mapping.getTable();
} else {
mappingTbl = sourceTbl.getBaseDatastoreClassWithMember(mapping.getMemberMetaData());
}
if (mappingTbl == sourceTbl) {
return sqlTbl;
}
// Try to find this datastore table in the same table group
SQLTable mappingSqlTbl = stmt.getTable(mappingTbl, sqlTbl.getGroupName());
if (mappingSqlTbl == null) {
boolean forceLeftOuter = false;
SQLTableGroup tableGrp = stmt.getTableGroup(sqlTbl.getGroupName());
if (tableGrp.getJoinType() == JoinType.LEFT_OUTER_JOIN) {
// This group isn't the candidate group, and we joined to the candidate group using
// a left outer join originally, so use the same type for this table
forceLeftOuter = true;
}
if (mappingTbl instanceof SecondaryDatastoreClass) {
// Secondary table, so add inner/outer based on metadata
boolean innerJoin = true;
JoinMetaData joinmd = ((SecondaryDatastoreClass) mappingTbl).getJoinMetaData();
if (joinmd != null && joinmd.isOuter() && !forceLeftOuter) {
innerJoin = false;
}
if (innerJoin && !forceLeftOuter) {
// Add join from {sourceTbl}.ID to {secondaryTbl}.ID
mappingSqlTbl = stmt.join(JoinType.INNER_JOIN, sqlTbl, sqlTbl.getTable().getIdMapping(), mappingTbl, null, mappingTbl.getIdMapping(), null, sqlTbl.getGroupName());
} else {
// Add join from {sourceTbl}.ID to {secondaryTbl}.ID
mappingSqlTbl = stmt.join(JoinType.LEFT_OUTER_JOIN, sqlTbl, sqlTbl.getTable().getIdMapping(), mappingTbl, null, mappingTbl.getIdMapping(), null, sqlTbl.getGroupName());
}
} else {
if (forceLeftOuter) {
// Add join from {sourceTbl}.ID to {superclassTbl}.ID
mappingSqlTbl = stmt.join(JoinType.LEFT_OUTER_JOIN, sqlTbl, sqlTbl.getTable().getIdMapping(), mappingTbl, null, mappingTbl.getIdMapping(), null, sqlTbl.getGroupName());
} else {
// Add join from {sourceTbl}.ID to {superclassTbl}.ID
mappingSqlTbl = stmt.join(JoinType.INNER_JOIN, sqlTbl, sqlTbl.getTable().getIdMapping(), mappingTbl, null, mappingTbl.getIdMapping(), null, sqlTbl.getGroupName());
}
}
}
return mappingSqlTbl;
}
use of org.datanucleus.metadata.JoinMetaData in project datanucleus-api-jdo by datanucleus.
the class MemberMetadataImpl method newJoinMetadata.
public JoinMetadata newJoinMetadata() {
JoinMetaData internalJoinmd = getInternal().newJoinMetaData();
JoinMetadataImpl joinmd = new JoinMetadataImpl(internalJoinmd);
joinmd.parent = this;
return joinmd;
}
use of org.datanucleus.metadata.JoinMetaData in project datanucleus-api-jdo by datanucleus.
the class MemberMetadataImpl method getJoinMetadata.
public JoinMetadata getJoinMetadata() {
JoinMetaData internalJoinmd = getInternal().getJoinMetaData();
if (internalJoinmd == null) {
return null;
}
JoinMetadataImpl joinmd = new JoinMetadataImpl(internalJoinmd);
joinmd.parent = this;
return joinmd;
}
Aggregations