use of org.datanucleus.metadata.CollectionMetaData 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.CollectionMetaData in project datanucleus-rdbms by datanucleus.
the class AbstractListStore method remove.
/**
* Method to remove the specified element from the List.
* @param op ObjectProvider for the owner
* @param element The element to remove.
* @param size Current size of list if known. -1 if not known
* @param allowDependentField Whether to allow any cascade deletes caused by this removal
* @return Whether it was removed successfully.
*/
public boolean remove(ObjectProvider op, Object element, int size, boolean allowDependentField) {
if (!validateElementForReading(op, element)) {
return false;
}
Object elementToRemove = element;
ExecutionContext ec = op.getExecutionContext();
if (ec.getApiAdapter().isDetached(element)) {
// Element passed in is detached so find attached version (DON'T attach this object)
elementToRemove = ec.findObject(ec.getApiAdapter().getIdForObject(element), true, false, element.getClass().getName());
}
boolean modified = internalRemove(op, elementToRemove, size);
if (allowDependentField) {
CollectionMetaData collmd = ownerMemberMetaData.getCollection();
boolean dependent = collmd.isDependentElement();
if (ownerMemberMetaData.isCascadeRemoveOrphans()) {
dependent = true;
}
if (dependent && !collmd.isEmbeddedElement()) {
// Delete the element if it is dependent
op.getExecutionContext().deleteObjectInternal(elementToRemove);
}
}
return modified;
}
use of org.datanucleus.metadata.CollectionMetaData in project datanucleus-rdbms by datanucleus.
the class AbstractListStore method remove.
/**
* Method to remove an object at an index in the List.
* If the list is ordered, will remove the element completely since no index positions exist.
* @param op ObjectProvider
* @param index The location
* @param size Current size of the list (if known). -1 if not known
* @return The object that was removed
*/
public E remove(ObjectProvider op, int index, int size) {
E element = get(op, index);
if (indexedList) {
// Remove the element at this position
internalRemoveAt(op, index, size);
} else {
// Ordered list doesn't allow indexed removal so just remove the element
internalRemove(op, element, size);
}
// Dependent element
CollectionMetaData collmd = ownerMemberMetaData.getCollection();
boolean dependent = collmd.isDependentElement();
if (ownerMemberMetaData.isCascadeRemoveOrphans()) {
dependent = true;
}
if (dependent && !collmd.isEmbeddedElement()) {
if (!contains(op, element)) {
// Delete the element if it is dependent and doesn't have a duplicate entry in the list
op.getExecutionContext().deleteObjectInternal(element);
}
}
return element;
}
use of org.datanucleus.metadata.CollectionMetaData in project datanucleus-rdbms by datanucleus.
the class ElementContainerStore method clear.
/**
* Clear the association from owner to all elements.
* Provides cascade-delete when the elements being deleted are PC types.
* @param ownerOP ObjectProvider for the container.
*/
public void clear(ObjectProvider ownerOP) {
Collection dependentElements = null;
CollectionMetaData collmd = ownerMemberMetaData.getCollection();
boolean dependent = collmd.isDependentElement();
if (ownerMemberMetaData.isCascadeRemoveOrphans()) {
dependent = true;
}
if (dependent && !collmd.isEmbeddedElement() && !collmd.isSerializedElement()) {
// Retain the dependent elements that need deleting after clearing
dependentElements = new HashSet();
Iterator iter = iterator(ownerOP);
while (iter.hasNext()) {
dependentElements.add(iter.next());
}
}
boolean ownerSoftDelete = ownerOP.getClassMetaData().hasExtension(MetaData.EXTENSION_CLASS_SOFTDELETE);
boolean elementSoftDelete = (elementInfo != null ? elementInfo[0].cmd.hasExtension(MetaData.EXTENSION_CLASS_SOFTDELETE) : false);
if (ownerSoftDelete && elementSoftDelete) {
// Owner and elements are being soft deleted, so no need to touch join table entries
} else if (!dependent && ownerSoftDelete) {
// Not deleting the elements, but owner is being soft deleted, so leave join table entries
} else {
String clearStmt = getClearStmt();
try {
ExecutionContext ec = ownerOP.getExecutionContext();
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, clearStmt, false);
try {
int jdbcPosition = 1;
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(ownerOP, ec, ps, jdbcPosition, this);
if (relationDiscriminatorMapping != null) {
BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
}
sqlControl.executeStatementUpdate(ec, mconn, clearStmt, ps, true);
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
throw new NucleusDataStoreException(Localiser.msg("056013", clearStmt), e);
}
}
// Cascade-delete
if (dependentElements != null && dependentElements.size() > 0) {
Iterator iter = dependentElements.iterator();
while (iter.hasNext()) {
Object obj = iter.next();
if (ownerOP.getExecutionContext().getApiAdapter().isDeleted(obj)) {
// Element is tagged for deletion so will be deleted at flush(), and we dont need it immediately
} else {
ownerOP.getExecutionContext().deleteObjectInternal(obj);
}
}
}
}
use of org.datanucleus.metadata.CollectionMetaData in project datanucleus-rdbms by datanucleus.
the class SingleCollectionMapping method initialize.
@Override
public void initialize(AbstractMemberMetaData mmd, Table table, ClassLoaderResolver clr) {
CollectionMetaData collectionMetaData = mmd.getCollection();
String wrappedTypeName = collectionMetaData.getElementType();
wrappedTypeClass = clr.classForName(wrappedTypeName);
WrappedMemberMetaData wmmd = new WrappedMemberMetaData(mmd, wrappedTypeClass, clr);
// Get the actual mapping that handles the the wrapped type
wrappedMapping = table.getStoreManager().getMappingManager().getMapping(table, wmmd, clr, FieldRole.ROLE_FIELD);
super.initialize(mmd, table, clr);
}
Aggregations