use of org.datanucleus.metadata.PrimaryKeyMetaData in project datanucleus-api-jdo by datanucleus.
the class JoinMetadataImpl method newPrimaryKeyMetadata.
/* (non-Javadoc)
* @see javax.jdo.metadata.JoinMetadata#newPrimaryKeyMetadata()
*/
public PrimaryKeyMetadata newPrimaryKeyMetadata() {
PrimaryKeyMetaData internalPkmd = getInternal().newPrimaryKeyMetaData();
PrimaryKeyMetadataImpl pkmd = new PrimaryKeyMetadataImpl(internalPkmd);
pkmd.parent = this;
return pkmd;
}
use of org.datanucleus.metadata.PrimaryKeyMetaData in project datanucleus-api-jdo by datanucleus.
the class JoinMetadataImpl method getPrimaryKeyMetadata.
/* (non-Javadoc)
* @see javax.jdo.metadata.JoinMetadata#getPrimaryKeyMetadata()
*/
public PrimaryKeyMetadata getPrimaryKeyMetadata() {
PrimaryKeyMetaData internalPkmd = getInternal().getPrimaryKeyMetaData();
if (internalPkmd == null) {
return null;
}
PrimaryKeyMetadataImpl pkmd = new PrimaryKeyMetadataImpl(internalPkmd);
pkmd.parent = this;
return pkmd;
}
use of org.datanucleus.metadata.PrimaryKeyMetaData in project tests by datanucleus.
the class AnnotationTest method testPrimaryKeyColumns.
/**
* Test for use of annotations to define PK columns, overriding those of the superclass
*/
public void testPrimaryKeyColumns() {
ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Manager.class.getName(), clr);
String prefix = cmd1.getFullClassName() + " : ";
PrimaryKeyMetaData pkmd = cmd1.getPrimaryKeyMetaData();
assertNotNull(prefix + "has null primary-key metadata!", pkmd);
assertNotNull(prefix + "has no primary-key columns!", pkmd.getColumnMetaData());
assertEquals(prefix + "has incorrect no of PK columns", 2, pkmd.getColumnMetaData().length);
ColumnMetaData[] colmds = pkmd.getColumnMetaData();
assertEquals("Name of first col of pk is incorrect", "MGR_ID", colmds[0].getName());
assertEquals("Target of first col of pk is incorrect", "PERSON_ID", colmds[0].getTarget());
assertEquals("Name of second col of pk is incorrect", "MGR_GLOBAL_ID", colmds[1].getName());
assertEquals("Target of second col of pk is incorrect", "PERSON_GLOB_ID", colmds[1].getTarget());
}
use of org.datanucleus.metadata.PrimaryKeyMetaData in project datanucleus-rdbms by datanucleus.
the class ClassTable method getPrimaryKey.
/**
* Accessor for the primary key for this table. Overrides the method in TableImpl
* to add on any specification of PK name in the metadata.
* @return The primary key.
*/
public PrimaryKey getPrimaryKey() {
PrimaryKey pk = super.getPrimaryKey();
PrimaryKeyMetaData pkmd = cmd.getPrimaryKeyMetaData();
if (pkmd != null && pkmd.getName() != null) {
pk.setName(pkmd.getName());
}
return pk;
}
use of org.datanucleus.metadata.PrimaryKeyMetaData in project datanucleus-rdbms by datanucleus.
the class CollectionTable method initialize.
/**
* 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 <join>
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 < elementMapping.getNumberOfDatastoreMappings(); i++) {
Column elementCol = elementMapping.getDatastoreMapping(i).getColumn();
elementCol.setNullable(true);
}
}
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[ELEMENT]", elementMapping);
}
}
PrimaryKeyMetaData pkmd = (mmd.getJoinMetaData() != null ? mmd.getJoinMetaData().getPrimaryKeyMetaData() : null);
boolean pkColsSpecified = (pkmd != null ? pkmd.getColumnMetaData() != null : false);
boolean pkRequired = requiresPrimaryKey();
// Add order mapping if required
boolean orderRequired = false;
if (mmd.getOrderMetaData() != null) {
if (mmd.getOrderMetaData().isIndexedList()) {
// Indexed Collection with <order>, 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 <order>, 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.getDatastoreMapping(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());
}
// JDO2 spec [18.5] order column is assumed to be "int"
orderMapping = storeMgr.getMappingManager().getMapping(int.class);
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 < ownerMapping.getNumberOfDatastoreMappings(); i++) {
ownerMapping.getDatastoreMapping(i).getColumn().setPrimaryKey();
}
if (orderRequired) {
// Order column specified so owner+order are the PK
orderMapping.getDatastoreMapping(0).getColumn().setPrimaryKey();
} else {
// No order column specified so owner+element are the PK
for (int i = 0; i < elementMapping.getNumberOfDatastoreMappings(); i++) {
elementMapping.getDatastoreMapping(i).getColumn().setPrimaryKey();
}
}
}
}
if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("057023", this));
}
storeMgr.registerTableInitialized(this);
state = TABLE_STATE_INITIALIZED;
}
Aggregations