use of javax.jdo.metadata.ForeignKeyMetadata in project motech by motech.
the class EntityMetadataBuilderImpl method getOrCreateRelFkMetadata.
private ForeignKeyMetadata getOrCreateRelFkMetadata(MemberMetadata mmd, EntityDto entity, FieldDto field) {
ForeignKeyMetadata fkmd = getOrCreateFkMetadata(mmd);
fkmd.setName(KeyNames.foreignKeyName(entity.getName(), entity.getId(), field.getBasic().getName(), EntityType.STANDARD));
return fkmd;
}
use of javax.jdo.metadata.ForeignKeyMetadata in project datanucleus-api-jdo by datanucleus.
the class TypeMetadataImpl method getForeignKeys.
public ForeignKeyMetadata[] getForeignKeys() {
List<ForeignKeyMetaData> internalFKs = getInternal().getForeignKeyMetaData();
if (internalFKs == null) {
return null;
}
ForeignKeyMetadata[] fkmds = new ForeignKeyMetadataImpl[internalFKs.size()];
int i = 0;
for (ForeignKeyMetaData internalFKMD : internalFKs) {
ForeignKeyMetadataImpl fkmd = new ForeignKeyMetadataImpl(internalFKMD);
fkmds[i++] = fkmd;
fkmd.parent = this;
}
return fkmds;
}
use of javax.jdo.metadata.ForeignKeyMetadata in project motech by motech.
the class EntityMetadataBuilderTest method shouldAddOneToOneRelationshipMetadata.
@Test
public void shouldAddOneToOneRelationshipMetadata() throws NotFoundException, CannotCompileException {
final String relClassName = "org.motechproject.test.MyClass";
final String relFieldName = "myField";
FieldDto oneToOneField = fieldDto("oneToOneName", OneToOneRelationship.class);
oneToOneField.addMetadata(new MetadataDto(RELATED_CLASS, relClassName));
oneToOneField.addMetadata(new MetadataDto(RELATED_FIELD, relFieldName));
FieldMetadata fmd = mock(FieldMetadata.class);
when(fmd.getName()).thenReturn("oneToOneName");
ForeignKeyMetadata fkmd = mock(ForeignKeyMetadata.class);
when(entity.getName()).thenReturn(ENTITY_NAME);
when(entity.getId()).thenReturn(3L);
when(schemaHolder.getFields(entity)).thenReturn(singletonList(oneToOneField));
when(entity.getTableName()).thenReturn(TABLE_NAME);
when(jdoMetadata.newPackageMetadata(PACKAGE)).thenReturn(packageMetadata);
when(packageMetadata.newClassMetadata(ENTITY_NAME)).thenReturn(classMetadata);
when(classMetadata.newFieldMetadata("oneToOneName")).thenReturn(fmd);
when(fmd.newForeignKeyMetadata()).thenReturn(fkmd);
/* We simulate configuration for the bi-directional relationship (the related class has got
a field that links back to the main class) */
CtClass myClass = mock(CtClass.class);
CtClass relatedClass = mock(CtClass.class);
CtField myField = mock(CtField.class);
CtField relatedField = mock(CtField.class);
when(myClass.getName()).thenReturn(relClassName);
when(myClass.getDeclaredFields()).thenReturn(new CtField[] { myField });
when(myField.getType()).thenReturn(relatedClass);
when(myField.getName()).thenReturn(relFieldName);
when(relatedClass.getDeclaredFields()).thenReturn(new CtField[] { relatedField });
when(relatedClass.getName()).thenReturn(CLASS_NAME);
entityMetadataBuilder.addEntityMetadata(jdoMetadata, entity, Sample.class, schemaHolder);
verifyCommonClassMetadata();
verify(fmd).setDefaultFetchGroup(true);
verify(fmd).setPersistenceModifier(PersistenceModifier.PERSISTENT);
verify(fkmd).setName("fk_Sample_oneToOneName_3");
}
use of javax.jdo.metadata.ForeignKeyMetadata in project motech by motech.
the class EntityMetadataBuilderImpl method addManyToManyMetadata.
private void addManyToManyMetadata(FieldMetadata fmd, RelationshipHolder holder, EntityDto entity, FieldDto field, Class<?> definition) {
// When using Lists join metadata must be present at two sides of M:N relation
if (!holder.isOwningSide() || holder.isListManyToMany()) {
java.lang.reflect.Field fieldDefinition = FieldUtils.getDeclaredField(definition, field.getBasic().getName(), true);
Join join = fieldDefinition.getAnnotation(Join.class);
Persistent persistent = fieldDefinition.getAnnotation(Persistent.class);
Element element = fieldDefinition.getAnnotation(Element.class);
JoinMetadata jmd = null;
if (join == null) {
jmd = fmd.newJoinMetadata();
}
setTableNameMetadata(fmd, persistent, entity, field, holder, EntityType.STANDARD);
setElementMetadata(fmd, element, holder, entity);
if (join == null || StringUtils.isEmpty(join.column())) {
setJoinMetadata(jmd, fmd, ClassName.getSimpleName(entity.getClassName()).toUpperCase() + ID_SUFFIX);
ForeignKeyMetadata fkmd = getOrCreateFkMetadata(jmd);
fkmd.setName(KeyNames.foreignKeyName(entity.getName(), entity.getId(), field.getBasic().getName(), EntityType.STANDARD));
}
}
}
use of javax.jdo.metadata.ForeignKeyMetadata in project motech by motech.
the class EntityMetadataBuilderImpl method setComboboxMetadata.
private FieldMetadata setComboboxMetadata(ClassMetadata cmd, EntityDto entity, FieldDto field, Class<?> definition, EntityType entityType) {
ComboboxHolder holder = new ComboboxHolder(entity, field);
String fieldName = getNameForMetadata(field);
FieldMetadata fmd = cmd.newFieldMetadata(fieldName);
if (holder.isCollection()) {
addDefaultFetchGroupMetadata(fmd, definition);
fmd.setTable(ClassTableName.getTableName(cmd.getTable(), fieldName));
JoinMetadata jm = fmd.newJoinMetadata();
ForeignKeyMetadata fkmd = getOrCreateFkMetadata(jm);
fkmd.setName(KeyNames.cbForeignKeyName(entity.getName(), entity.getId(), fieldName, entityType));
jm.setDeleteAction(ForeignKeyAction.CASCADE);
jm.newColumnMetadata().setName(fieldName + "_OID");
}
return fmd;
}
Aggregations