org.datanucleus.store.rdbms.table.CollectionTable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datanucleus-rdbms Show documentation
Show all versions of datanucleus-rdbms Show documentation
Plugin for DataNucleus providing persistence to RDBMS datastores.
/**********************************************************************
Copyright (c) 2005 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Contributors:
...
**********************************************************************/
package org.datanucleus.store.rdbms.table;
import java.util.List;
import java.util.Set;
import org.datanucleus.ClassLoaderResolver;
import org.datanucleus.exceptions.NucleusUserException;
import org.datanucleus.metadata.AbstractClassMetaData;
import org.datanucleus.metadata.AbstractMemberMetaData;
import org.datanucleus.metadata.ColumnMetaData;
import org.datanucleus.metadata.ElementMetaData;
import org.datanucleus.metadata.FieldRole;
import org.datanucleus.metadata.IdentityType;
import org.datanucleus.metadata.PrimaryKeyMetaData;
import org.datanucleus.metadata.RelationType;
import org.datanucleus.store.rdbms.identifier.DatastoreIdentifier;
import org.datanucleus.store.rdbms.mapping.java.PersistableMapping;
import org.datanucleus.store.rdbms.mapping.java.ReferenceMapping;
import org.datanucleus.store.rdbms.RDBMSStoreManager;
import org.datanucleus.util.ClassUtils;
import org.datanucleus.util.Localiser;
import org.datanucleus.util.NucleusLogger;
/**
* Representation of a join table for a Collection. A Collection covers a wide range of possibilities
* in terms of whether it allows duplicates or not, whether it allows nulls or not, whether it supports
* ordering via indexes, whether it supports ordering via a SELECT criteria, etc. Consequently the
* join table can vary depending on the required capabilities.
* JoinTable Mappings
*
* The join table consists of the following mappings :-
*
* - ownerMapping linking back to the owning class with the Collection.
* - elementMapping either being an FK link to the element table or being an
* embedded/serialised element stored wholly in this table.
* - orderMapping which may be null, or otherwise stores an index for the elements.
* This is either to provide uniqueness or ordering in a List (and part of the PK).
*
* Note that with an M-N relation there will be 2 instances of the CollectionTable - one represents the relation
* from owner to element, and the other for the relation from element to owner.
*/
public class CollectionTable extends ElementContainerTable implements DatastoreElementContainer
{
/**
* Constructor.
* @param ownerTable Table of the owner of this member
* @param tableName Identifier name of the table
* @param mmd MetaData for the member of the owner
* @param storeMgr The Store Manager managing these tables.
*/
public CollectionTable(Table ownerTable, DatastoreIdentifier tableName, AbstractMemberMetaData mmd, RDBMSStoreManager storeMgr)
{
super(ownerTable, tableName, mmd, storeMgr);
}
/**
* Method to initialise the table definition.
* @param clr The ClassLoaderResolver
*/
public void initialize(ClassLoaderResolver clr)
{
super.initialize(clr);
// Add column(s) for element
boolean elementPC = (mmd.hasCollection() && mmd.getCollection().elementIsPersistent());
Class elementClass = clr.classForName(getElementType());
if (isSerialisedElement() || isEmbeddedElementPC() || (isEmbeddedElement() && !elementPC) || ClassUtils.isReferenceType(elementClass))
{
// Element = PC(embedded), PC(serialised), Non-PC(serialised), Non-PC(embedded), Reference
// Join table has : ownerMapping (PK), elementMapping, orderMapping (PK)
elementMapping = storeMgr.getMappingManager().getMapping(this, mmd, clr, FieldRole.ROLE_COLLECTION_ELEMENT);
if (NucleusLogger.DATASTORE.isDebugEnabled())
{
logMapping(mmd.getFullFieldName()+".[ELEMENT]", elementMapping);
}
}
else
{
// Element = PC
// Join table has : ownerMapping (PK), elementMapping, orderMapping (optional)
ColumnMetaData[] elemColmd = null;
AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
ElementMetaData elemmd = mmd.getElementMetaData();
if (elemmd != null && elemmd.getColumnMetaData() != null && elemmd.getColumnMetaData().length > 0)
{
// Column mappings defined at this side (1-N, M-N)
elemColmd = elemmd.getColumnMetaData();
}
else if (relatedMmds != null && relatedMmds[0].getJoinMetaData() != null &&
relatedMmds[0].getJoinMetaData().getColumnMetaData() != null &&
relatedMmds[0].getJoinMetaData().getColumnMetaData().length > 0)
{
// Column mappings defined at other side (M-N) on
elemColmd = relatedMmds[0].getJoinMetaData().getColumnMetaData();
}
elementMapping = ColumnCreator.createColumnsForJoinTables(elementClass, mmd, elemColmd, storeMgr, this, false, false, FieldRole.ROLE_COLLECTION_ELEMENT, clr, null);
RelationType relationType = mmd.getRelationType(clr);
if (Boolean.TRUE.equals(mmd.getContainer().allowNulls()) && relationType != RelationType.MANY_TO_MANY_BI)
{
// 1-N : Make all element col(s) nullable so we can store null elements
for (int i=0;i, so add index mapping
orderRequired = true;
RelationType relType = mmd.getRelationType(clr);
if (relType == RelationType.MANY_TO_MANY_BI)
{
// Don't support M-N using indexed List
throw new NucleusUserException(Localiser.msg("020002", mmd.getFullFieldName())).setFatal();
}
}
}
else if (List.class.isAssignableFrom(mmd.getType()))
{
// Indexed List with no , so has index mapping
orderRequired = true;
}
else if (pkRequired && !pkColsSpecified)
{
// PK is required so maybe need to add an index to form the PK
if (isEmbeddedElementPC())
{
if (mmd.getCollection().getElementClassMetaData(clr).getIdentityType() != IdentityType.APPLICATION)
{
// Embedded PC with datastore id so we need an index to form the PK
orderRequired = true;
}
}
else if (isSerialisedElement())
{
// Serialised element, so need an index to form the PK
orderRequired = true;
}
else if (elementMapping instanceof ReferenceMapping)
{
// ReferenceMapping, so have order if more than 1 implementation
ReferenceMapping refMapping = (ReferenceMapping)elementMapping;
if (refMapping.getJavaTypeMapping().length > 1)
{
orderRequired = true;
}
}
else if (!(elementMapping instanceof PersistableMapping))
{
// Non-PC, so depends if the element column can be used as part of a PK
// TODO This assumes the elementMapping has a single column but what if it is Color with 4 cols?
Column elementCol = elementMapping.getColumnMapping(0).getColumn();
if (!storeMgr.getDatastoreAdapter().isValidPrimaryKeyType(elementCol.getJdbcType()))
{
// Not possible to use this Non-PC type as part of the PK
orderRequired = true;
}
}
}
if (orderRequired)
{
// Order (index) column is required (integer based)
ColumnMetaData orderColmd = null;
if (mmd.getOrderMetaData() != null &&
mmd.getOrderMetaData().getColumnMetaData() != null &&
mmd.getOrderMetaData().getColumnMetaData().length > 0)
{
// Specified "order" column info
orderColmd = mmd.getOrderMetaData().getColumnMetaData()[0];
if (orderColmd.getName() == null)
{
// No column name so use default
orderColmd = new ColumnMetaData(orderColmd);
DatastoreIdentifier id = storeMgr.getIdentifierFactory().newIndexFieldIdentifier(mmd);
orderColmd.setName(id.getName());
}
}
else
{
// No column name so use default
DatastoreIdentifier id = storeMgr.getIdentifierFactory().newIndexFieldIdentifier(mmd);
orderColmd = new ColumnMetaData();
orderColmd.setName(id.getName());
}
orderMapping = storeMgr.getMappingManager().getMapping(int.class); // JDO2 spec [18.5] order column is assumed to be "int"
ColumnCreator.createIndexColumn(orderMapping, storeMgr, clr, this, orderColmd, pkRequired && !pkColsSpecified);
if (NucleusLogger.DATASTORE.isDebugEnabled())
{
logMapping(mmd.getFullFieldName()+".[ORDER]", orderMapping);
}
}
// Define primary key of the join table (if any)
if (pkRequired)
{
if (pkColsSpecified)
{
// Apply the users PK specification
applyUserPrimaryKeySpecification(pkmd);
}
else
{
// Define PK
for (int i=0;i 1)
{
// Cannot apply PK when we have multiple interface implementations
return false;
}
if (Set.class.isAssignableFrom(mmd.getType()))
{
// No dups
}
else if (mmd.getOrderMetaData() != null && !mmd.getOrderMetaData().isIndexedList())
{
// Dups allowed in a List, but if we have an indexed list then we can have a PK
return false;
}
return super.requiresPrimaryKey();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy