use of org.datanucleus.metadata.ElementMetaData in project datanucleus-rdbms by datanucleus.
the class ReferenceMapping method prepareDatastoreMapping.
/**
* Convenience method to create the necessary columns to represent this reference in the datastore.
* With "per-implementation" mapping strategy will create columns for each of the possible implementations.
* With "identity"/"xcalia" will create a single column to store a reference to the implementation value.
* @param clr The ClassLoaderResolver
*/
protected void prepareDatastoreMapping(ClassLoaderResolver clr) {
if (mappingStrategy == PER_IMPLEMENTATION_MAPPING) {
// Mapping per reference implementation, so create columns for each possible implementation
if (roleForMember == FieldRole.ROLE_ARRAY_ELEMENT) {
// Creation of columns in join table for array of references
ColumnMetaData[] colmds = null;
ElementMetaData elemmd = mmd.getElementMetaData();
if (elemmd != null && elemmd.getColumnMetaData() != null && elemmd.getColumnMetaData().length > 0) {
// Column mappings defined at this side (1-N, M-N)
colmds = elemmd.getColumnMetaData();
}
createPerImplementationColumnsForReferenceField(false, false, false, false, roleForMember, colmds, clr);
} else if (roleForMember == FieldRole.ROLE_COLLECTION_ELEMENT) {
// Creation of columns in join table for collection of references
ColumnMetaData[] colmds = 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)
colmds = 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>
colmds = relatedMmds[0].getJoinMetaData().getColumnMetaData();
}
createPerImplementationColumnsForReferenceField(false, false, false, false, roleForMember, colmds, clr);
} else if (roleForMember == FieldRole.ROLE_MAP_KEY) {
// Creation of columns in join table for map of references as keys
ColumnMetaData[] colmds = null;
KeyMetaData keymd = mmd.getKeyMetaData();
if (keymd != null && keymd.getColumnMetaData() != null && keymd.getColumnMetaData().length > 0) {
// Column mappings defined at this side (1-N, M-N)
colmds = keymd.getColumnMetaData();
}
createPerImplementationColumnsForReferenceField(false, false, false, false, roleForMember, colmds, clr);
} else if (roleForMember == FieldRole.ROLE_MAP_VALUE) {
// Creation of columns in join table for map of references as values
ColumnMetaData[] colmds = null;
ValueMetaData valuemd = mmd.getValueMetaData();
if (valuemd != null && valuemd.getColumnMetaData() != null && valuemd.getColumnMetaData().length > 0) {
// Column mappings defined at this side (1-N, M-N)
colmds = valuemd.getColumnMetaData();
}
createPerImplementationColumnsForReferenceField(false, false, false, false, roleForMember, colmds, clr);
} else {
if (mmd.getMappedBy() == null) {
// Unidirectional 1-1
boolean embedded = (mmd.isEmbedded() || mmd.getEmbeddedMetaData() != null);
createPerImplementationColumnsForReferenceField(false, true, false, embedded, roleForMember, mmd.getColumnMetaData(), clr);
} else {
// Bidirectional 1-1/N-1
AbstractClassMetaData refCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForInterface(mmd.getType(), clr);
if (refCmd != null && refCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE) {
// TODO Is this block actually reachable ? Would we specify "inheritance" under "interface" elements?
// 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) {
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 currently");
}
} else {
// TODO Throw an exception ?
return;
}
// TODO We need a mapping for each of the possible subclass tables
/*JavaTypeMapping referenceMapping = */
storeMgr.getDatastoreClass(cmds[0].getFullClassName(), clr).getIdMapping();
} else {
String[] implTypes = MetaDataUtils.getInstance().getImplementationNamesForReferenceField(mmd, FieldRole.ROLE_FIELD, clr, storeMgr.getMetaDataManager());
for (int j = 0; j < implTypes.length; j++) {
JavaTypeMapping refMapping = storeMgr.getDatastoreClass(implTypes[j], clr).getIdMapping();
JavaTypeMapping mapping = storeMgr.getMappingManager().getMapping(clr.classForName(implTypes[j]));
mapping.setReferenceMapping(refMapping);
this.addJavaTypeMapping(mapping);
}
}
}
}
} else if (mappingStrategy == ID_MAPPING || mappingStrategy == XCALIA_MAPPING) {
// Single (String) column storing the identity of the related object
MappingManager mapMgr = storeMgr.getMappingManager();
JavaTypeMapping mapping = mapMgr.getMapping(String.class);
mapping.setMemberMetaData(mmd);
mapping.setTable(table);
mapping.setRoleForMember(roleForMember);
Column col = mapMgr.createColumn(mapping, String.class.getName(), 0);
mapMgr.createDatastoreMapping(mapping, mmd, 0, col);
this.addJavaTypeMapping(mapping);
}
}
use of org.datanucleus.metadata.ElementMetaData in project datanucleus-rdbms by datanucleus.
the class ArrayTable method initialize.
/**
* Method to initialise the table definition.
* @param clr The ClassLoaderResolver
*/
@Override
public void initialize(ClassLoaderResolver clr) {
super.initialize(clr);
// Add field(s) for element
boolean elementPC = (mmd.hasArray() && mmd.getArray().elementIsPersistent());
if (isSerialisedElementPC() || isEmbeddedElementPC() || (isEmbeddedElement() && !elementPC) || ClassUtils.isReferenceType(mmd.getType().getComponentType())) {
// Element = PC(embedded), PC(serialised), Non-PC(embedded), Reference
elementMapping = storeMgr.getMappingManager().getMapping(this, mmd, clr, FieldRole.ROLE_ARRAY_ELEMENT);
if (Boolean.TRUE.equals(mmd.getContainer().allowNulls())) {
// Make all element col(s) nullable so we can store null elements
for (int i = 0; i < elementMapping.getNumberOfColumnMappings(); i++) {
Column elementCol = elementMapping.getColumnMapping(i).getColumn();
elementCol.setNullable(true);
}
}
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[ELEMENT]", elementMapping);
}
} else {
// Element = PC
ColumnMetaData[] elemColmd = null;
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();
}
elementMapping = ColumnCreator.createColumnsForJoinTables(mmd.getType().getComponentType(), mmd, elemColmd, storeMgr, this, false, true, FieldRole.ROLE_ARRAY_ELEMENT, clr, null);
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[ELEMENT]", elementMapping);
}
}
boolean pkRequired = requiresPrimaryKey();
PrimaryKeyMetaData pkmd = (mmd.getJoinMetaData() != null ? mmd.getJoinMetaData().getPrimaryKeyMetaData() : null);
boolean pkColsSpecified = (pkmd != null ? pkmd.getColumnMetaData() != null : false);
// Add order mapping
ColumnMetaData colmd = null;
if (mmd.getOrderMetaData() != null && mmd.getOrderMetaData().getColumnMetaData() != null && mmd.getOrderMetaData().getColumnMetaData().length > 0) {
// Specified "order" column info
colmd = mmd.getOrderMetaData().getColumnMetaData()[0];
} else {
// No column name so use default
DatastoreIdentifier id = storeMgr.getIdentifierFactory().newIndexFieldIdentifier(mmd);
colmd = new ColumnMetaData();
colmd.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, colmd, 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.getNumberOfColumnMappings(); i++) {
ownerMapping.getColumnMapping(i).getColumn().setPrimaryKey();
}
}
}
if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("057023", this));
}
storeMgr.registerTableInitialized(this);
state = TABLE_STATE_INITIALIZED;
}
use of org.datanucleus.metadata.ElementMetaData in project tests by datanucleus.
the class AnnotationTest method testOneToManyBiFK.
/**
* Test of JPA 1-N bidir FK relation
*/
public void testOneToManyBiFK() {
NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
metaDataMgr.loadPersistenceUnit(pumd, null);
// owner side
ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Manager.class.getName(), clr);
AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("departments");
assertNotNull("Manager.departments is null!", fmd1);
assertEquals("Manager.departments mapped-by is incorrect", fmd1.getMappedBy(), "manager");
assertEquals("Manager.departments relationType is incorrect", fmd1.getRelationType(clr), RelationType.ONE_TO_MANY_BI);
ElementMetaData elemmd = fmd1.getElementMetaData();
assertNull("Manager.departments has join column info but shouldnt (specified on N side)", elemmd);
// non-owner side
ClassMetaData cmd2 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Department.class.getName(), clr);
AbstractMemberMetaData fmd2 = cmd2.getMetaDataForMember("manager");
assertNotNull("Department.manager is null!", fmd2);
assertEquals("Department.manager mapped-by is incorrect", fmd2.getMappedBy(), null);
assertEquals("Department.manager relationType is incorrect", fmd2.getRelationType(clr), RelationType.MANY_TO_ONE_BI);
ColumnMetaData[] colmds = fmd2.getColumnMetaData();
assertNotNull("Department.manager has no join column info", colmds);
assertEquals("Department.manager has incorrect number of joincolumns", colmds.length, 1);
assertEquals("Department.manager joincolumn name is wrong", "MGR_ID", colmds[0].getName());
}
use of org.datanucleus.metadata.ElementMetaData in project tests by datanucleus.
the class AnnotationTest method testOneToManyBiJoin.
/**
* Test of JPA 1-N unidir FK relation.
* Really is 1-N uni join since JPA doesnt support 1-N uni FK
*/
/*public void testOneToManyUniFK()
{
NucleusContext nucleusCtx = new NucleusContext(new PersistenceConfiguration(){});
nucleusCtx.setApi("JPA");
MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
ClassLoaderResolver clr = new ClassLoaderResolverImpl();
// owner side
ClassMetaData cmd1 = (ClassMetaData)metaDataMgr.getMetaDataForClass(Site.class.getName(), clr);
AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("offices");
assertNotNull("Site.offices is null!", fmd1);
assertEquals("Site.offices mapped-by is incorrect", fmd1.getMappedBy(), null);
assertEquals("Site.offices relationType is incorrect",
fmd1.getRelationType(clr), Relation.ONE_TO_MANY_UNI);
assertEquals("Site.offices jointable name is incorrect", fmd1.getTable(), null);
assertNotNull("Site.offices should have join but doesnt", fmd1.getJoinMetaData());
ElementMetaData elemmd = fmd1.getElementMetaData();
assertNotNull("Site.offices has no element column info but should", elemmd);
ColumnMetaData[] colmds = elemmd.getColumnMetaData();
assertNotNull("Site.offices has incorrect element columns", colmds);
assertEquals("Site.offices has incorrect number of element columns", colmds.length, 1);
assertEquals("Site.offices has incorrect element column name", colmds[0].getName(), "SITE_ID");
}*/
/**
* Test of JPA 1-N bidir join relation
*/
public void testOneToManyBiJoin() {
NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
metaDataMgr.loadPersistenceUnit(pumd, null);
// owner side
ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Manager.class.getName(), clr);
assertEquals("Manager has incorrect table name", cmd1.getTable(), "JPA_AN_MANAGER");
AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("subordinates");
assertNotNull("Manager.subordinates is null!", fmd1);
assertEquals("Manager.subordinates mapped-by is incorrect", fmd1.getMappedBy(), "manager");
assertEquals("Manager.subordinates relationType is incorrect", fmd1.getRelationType(clr), RelationType.ONE_TO_MANY_BI);
assertEquals("Manager.subordinates jointable name is incorrect", fmd1.getTable(), "JPA_AN_MGR_EMPLOYEES");
// non-owner side
ClassMetaData cmd2 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Employee.class.getName(), clr);
assertEquals("Employee has incorrect table name", cmd2.getTable(), "JPA_AN_EMPLOYEE");
AbstractMemberMetaData fmd2 = cmd2.getMetaDataForMember("manager");
assertNotNull("Employee.manager is null!", fmd2);
assertEquals("Employee.manager mapped-by is incorrect", fmd2.getMappedBy(), null);
assertEquals("Employee.manager relationType is incorrect", fmd2.getRelationType(clr), RelationType.MANY_TO_ONE_BI);
assertEquals("Employee.manager jointable name is incorrect", fmd2.getTable(), null);
// join-table
JoinMetaData joinmd = fmd1.getJoinMetaData();
assertNotNull("Manager.subordinates has no join table!", joinmd);
assertNotNull("Manager.subordinates has incorrect join columns", joinmd.getColumnMetaData());
assertEquals("Manager.subordinates has incorrect number of join columns", 1, joinmd.getColumnMetaData().length);
assertEquals("Manager.subordinates has incorrect owner join column name", "MGR_ID", joinmd.getColumnMetaData()[0].getName());
ElementMetaData elemmd = fmd1.getElementMetaData();
assertNotNull("Manager.subordinates has no element column info but should", elemmd);
assertNotNull("Manager.subordinates has incorrect element columns", elemmd.getColumnMetaData());
assertEquals("Manager.subordinates has incorrect number of element columns", 1, elemmd.getColumnMetaData().length);
assertEquals("Manager.subordinates has incorrect element join column name", "EMP_ID", elemmd.getColumnMetaData()[0].getName());
}
use of org.datanucleus.metadata.ElementMetaData in project tests by datanucleus.
the class XMLTest method testOneToManyUniJoin.
/**
* Test of JPA 1-N unidir JoinTable relation
*/
public void testOneToManyUniJoin() {
NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
metaDataMgr.loadPersistenceUnit(pumd, null);
// owner side
ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Department.class.getName(), clr);
AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("projects");
assertNotNull("Department.projects is null!", fmd1);
assertEquals("Department.projects mapped-by is incorrect", null, fmd1.getMappedBy());
assertEquals("Department.projects relationType is incorrect", RelationType.ONE_TO_MANY_UNI, fmd1.getRelationType(clr));
assertEquals("Department.projects jointable name is incorrect", "JPA_MD_DEPT_PROJECTS", fmd1.getTable());
JoinMetaData joinmd = fmd1.getJoinMetaData();
assertNotNull("Department.projects has no join table!", joinmd);
assertNotNull("Department.projects has incorrect join columns", joinmd.getColumnMetaData());
assertEquals("Department.projects has incorrect number of join columns", 1, joinmd.getColumnMetaData().length);
assertEquals("Department.projects has incorrect join column name", joinmd.getColumnMetaData()[0].getName(), "DEPT_ID");
ElementMetaData elemmd = fmd1.getElementMetaData();
assertNotNull("Department.projects has no element column info but should", elemmd);
ColumnMetaData[] colmds = elemmd.getColumnMetaData();
assertNotNull("Department.projects has incorrect element columns", colmds);
assertEquals("Department.projects has incorrect number of element columns", 1, colmds.length);
assertEquals("Department.projects has incorrect element column name", "PROJECT_ID", colmds[0].getName());
}
Aggregations