use of org.eclipse.persistence.mappings.OneToManyMapping in project eclipselink by eclipse-ee4j.
the class DynamicTypeBuilder method addOneToManyMapping.
/**
* Add a {@link OneToManyMapping} to the {@link #entityType} being built or
* extended. This mapping is created using standard foreign keys from the
* source table(s) to the target table(s) and transparent indirection (
* {@link IndirectList}).
*
* @param name
* attribute name to use in the dynamic entity. Also the property
* name used to access the state of the entity
* @param fkFieldNames
* the FK field names specified in the same order to match the PK
* field names of the target class
*
* @return the newly created, configured mappin. It will be initialized if
* the descriptor is already initialized.
*/
public OneToManyMapping addOneToManyMapping(String name, DynamicType refType, String... fkFieldNames) {
if (fkFieldNames == null) {
throw new IllegalArgumentException("Invalid FK field names: 'null' for target: " + refType);
}
if (getType().getDescriptor().getPrimaryKeyFields().size() != fkFieldNames.length) {
throw new IllegalArgumentException("Invalid FK field names: " + Arrays.asList(fkFieldNames) + " for target: " + refType);
}
OneToManyMapping mapping = new OneToManyMapping();
mapping.setAttributeName(name);
mapping.setReferenceClass(refType.getJavaClass());
for (int index = 0; index < fkFieldNames.length; index++) {
String targetField = getType().getDescriptor().getPrimaryKeyFields().get(index).getName();
mapping.addTargetForeignKeyFieldName(fkFieldNames[index], targetField);
}
mapping.useTransparentList();
return (OneToManyMapping) addMapping(mapping);
}
use of org.eclipse.persistence.mappings.OneToManyMapping in project eclipselink by eclipse-ee4j.
the class ConcurrentPerson method descriptor.
public static RelationalDescriptor descriptor() {
RelationalDescriptor descriptor = new RelationalDescriptor();
/* First define the class, table and descriptor properties. */
descriptor.setJavaClass(ConcurrentPerson.class);
descriptor.setTableName("CONCURRENT_EMP");
descriptor.setPrimaryKeyFieldName("ID");
descriptor.setSequenceNumberName("SEQ");
descriptor.setSequenceNumberFieldName("ID");
/* Next define the attribute mappings. */
descriptor.addDirectMapping("id", "ID");
DirectToFieldMapping nameMapping = new DirectToFieldMapping();
nameMapping.setAttributeName("name");
nameMapping.setFieldName("NAME");
nameMapping.setGetMethodName("getName");
nameMapping.setSetMethodName("setName");
descriptor.addMapping(nameMapping);
OneToOneMapping addressMapping = new OneToOneMapping();
addressMapping.setAttributeName("address");
addressMapping.setReferenceClass(ConcurrentAddress.class);
addressMapping.setGetMethodName("getAddress");
addressMapping.setSetMethodName("setAddress");
addressMapping.dontUseIndirection();
addressMapping.addForeignKeyFieldName("CONCURRENT_EMP.ADDR_ID", "CONCURRENT_ADDRESS.ADDRESS_ID");
descriptor.addMapping(addressMapping);
TransformationMapping trans = new TransformationMapping();
trans.setIsReadOnly(true);
trans.dontUseIndirection();
trans.setAttributeName("luckyNumber");
trans.setAttributeTransformation("calculateLuckyNumber");
descriptor.addMapping(trans);
OneToOneMapping hobbyMapping = new OneToOneMapping();
hobbyMapping.setAttributeName("hobby");
hobbyMapping.setReferenceClass(ConcurrentProject.class);
hobbyMapping.setGetMethodName("getHobby");
hobbyMapping.setSetMethodName("setHobby");
hobbyMapping.dontUseIndirection();
hobbyMapping.addForeignKeyFieldName("CONCURRENT_EMP.PROJ_ID", "CONCURRENT_PROJECT.ID");
descriptor.addMapping(hobbyMapping);
OneToManyMapping phoneNumbersMapping = new OneToManyMapping();
phoneNumbersMapping.setAttributeName("phoneNumbers");
phoneNumbersMapping.setReferenceClass(ConcurrentPhoneNumber.class);
phoneNumbersMapping.dontUseIndirection();
phoneNumbersMapping.addTargetForeignKeyFieldName("CONCURRENT_PHONE.EMP_ID", "CONCURRENT_EMP.ID");
descriptor.addMapping(phoneNumbersMapping);
return descriptor;
}
use of org.eclipse.persistence.mappings.OneToManyMapping in project eclipselink by eclipse-ee4j.
the class SetMethodParameterTypeNotValidTest method descriptor.
public RelationalDescriptor descriptor() {
RelationalDescriptor descriptor = new RelationalDescriptor();
descriptor.setJavaClass(org.eclipse.persistence.testing.tests.validation.EmployeeWithProblems.class);
descriptor.addTableName("EMPLOYEE");
descriptor.addPrimaryKeyFieldName("EMPLOYEE.EMP_ID");
// Descriptor properties.
descriptor.useFullIdentityMap();
descriptor.setIdentityMapSize(100);
descriptor.useRemoteFullIdentityMap();
descriptor.setRemoteIdentityMapSize(100);
descriptor.setSequenceNumberFieldName("EMP_ID");
descriptor.setSequenceNumberName("EMP_SEQ");
DirectToFieldMapping idMapping = new DirectToFieldMapping();
idMapping.setAttributeName("id");
idMapping.setFieldName("EMPLOYEE.EMP_ID");
descriptor.addMapping(idMapping);
// the following mapping causes the correct error to occure.
OneToManyMapping managedEmployeesMapping = new OneToManyMapping();
managedEmployeesMapping.setAttributeName("managedEmployeesWithProblems");
managedEmployeesMapping.setReferenceClass(org.eclipse.persistence.testing.tests.validation.EmployeeWithProblems.class);
managedEmployeesMapping.dontUseIndirection();
// the following causes the correct error to occure.
managedEmployeesMapping.setSetMethodName("setManagedEmployeesWithProblems");
managedEmployeesMapping.addTargetForeignKeyFieldName("EMPLOYEE.MANAGER_ID", "EMPLOYEE.EMP_ID");
descriptor.addMapping(managedEmployeesMapping);
return descriptor;
}
use of org.eclipse.persistence.mappings.OneToManyMapping in project eclipselink by eclipse-ee4j.
the class MappingProject method buildEmployeeDescriptor.
protected void buildEmployeeDescriptor() {
RelationalDescriptor descriptor = new RelationalDescriptor();
// SECTION: DESCRIPTOR
descriptor.setJavaClass(Employee.class);
Vector vector = new Vector();
vector.addElement("MAP_EMP");
descriptor.setTableNames(vector);
descriptor.addPrimaryKeyFieldName("MAP_EMP.FNAME");
descriptor.addPrimaryKeyFieldName("MAP_EMP.LNAME");
descriptor.addPrimaryKeyFieldName("MAP_EMP.SEX");
// SECTION: COPY POLICY
descriptor.createCopyPolicy("constructor");
descriptor.useSoftIdentityMap();
// SECTION: INSTANTIATION POLICY
descriptor.createInstantiationPolicy("constructor");
// SECTION: DIRECTCOLLECTIONMAPPING
DirectCollectionMapping directcollectionmapping = new DirectCollectionMapping();
directcollectionmapping.setAttributeName("policies");
directcollectionmapping.setIsReadOnly(false);
directcollectionmapping.setUsesIndirection(true);
directcollectionmapping.setIsPrivateOwned(false);
directcollectionmapping.setDirectFieldName("MAP_POL.POLICY");
directcollectionmapping.setReferenceTableName("MAP_POL");
directcollectionmapping.addReferenceKeyFieldName("MAP_POL.LNAME", "MAP_EMP.LNAME");
directcollectionmapping.addReferenceKeyFieldName("MAP_POL.FNAME", "MAP_EMP.FNAME");
descriptor.addMapping(directcollectionmapping);
// SECTION: DIRECTTOFIELDMAPPING
DirectToFieldMapping directtofieldmapping = new DirectToFieldMapping();
directtofieldmapping.setAttributeName("firstName");
directtofieldmapping.setIsReadOnly(false);
directtofieldmapping.setFieldName("MAP_EMP.FNAME");
descriptor.addMapping(directtofieldmapping);
// SECTION: DIRECTTOFIELDMAPPING
DirectToFieldMapping directtofieldmapping1 = new DirectToFieldMapping();
directtofieldmapping1.setAttributeName("lastName");
directtofieldmapping1.setIsReadOnly(false);
directtofieldmapping1.setFieldName("MAP_EMP.LNAME");
descriptor.addMapping(directtofieldmapping1);
// SECTION: ONETOONEMAPPING
OneToOneMapping onetoonemapping1 = new OneToOneMapping();
onetoonemapping1.setAttributeName("cubicle");
onetoonemapping1.setIsReadOnly(false);
onetoonemapping1.setUsesIndirection(false);
onetoonemapping1.setReferenceClass(Cubicle.class);
onetoonemapping1.setIsPrivateOwned(true);
onetoonemapping1.addForeignKeyFieldName("MAP_EMP.C_ID", "MAP_CUB.C_ID");
descriptor.addMapping(onetoonemapping1);
// SECTION: MANYTOMANYMAPPING
ManyToManyMapping manytomanymapping = new ManyToManyMapping();
manytomanymapping.setAttributeName("phoneNumbers");
manytomanymapping.setIsReadOnly(false);
manytomanymapping.setUsesIndirection(true);
manytomanymapping.setReferenceClass(Phone.class);
manytomanymapping.setIsPrivateOwned(true);
manytomanymapping.setRelationTableName("MAP_EMPH");
manytomanymapping.addSourceRelationKeyFieldName("MAP_EMPH.LNAME", "MAP_EMP.LNAME");
manytomanymapping.addSourceRelationKeyFieldName("MAP_EMPH.FNAME", "MAP_EMP.FNAME");
manytomanymapping.addTargetRelationKeyFieldName("MAP_EMPH.P_ID", "MAP_PHO.P_ID");
descriptor.addMapping(manytomanymapping);
// SECTION: MANYTOMANYMAPPING
ManyToManyMapping manytomanymapping1 = new ManyToManyMapping();
manytomanymapping1.setAttributeName("shipments");
manytomanymapping1.setIsReadOnly(false);
manytomanymapping1.setUsesIndirection(false);
manytomanymapping1.setReferenceClass(Shipment.class);
manytomanymapping1.setIsPrivateOwned(false);
manytomanymapping1.setRelationTableName("MAP_EMSP");
manytomanymapping1.addSourceRelationKeyFieldName("MAP_EMSP.EMP_LNAME", "MAP_EMP.LNAME");
manytomanymapping1.addSourceRelationKeyFieldName("MAP_EMSP.EMP_FNAME", "MAP_EMP.FNAME");
manytomanymapping1.addTargetRelationKeyFieldName("MAP_EMSP.SP_TSMIL", "MAP_SHIP.SP_TSMIL");
manytomanymapping1.addTargetRelationKeyFieldName("MAP_EMSP.SP_TS", "MAP_SHIP.SP_TS");
descriptor.addMapping(manytomanymapping1);
// SECTION: OBJECTTYPEMAPPING
DirectToFieldMapping objecttypemapping = new DirectToFieldMapping();
ObjectTypeConverter objecttypeconverter = new ObjectTypeConverter();
objecttypemapping.setAttributeName("sex");
objecttypemapping.setIsReadOnly(false);
objecttypemapping.setFieldName("MAP_EMP.SEX");
objecttypeconverter.setDefaultAttributeValue("female");
objecttypeconverter.addConversionValue("F", "female");
objecttypeconverter.addConversionValue("M", "male");
objecttypemapping.setConverter(objecttypeconverter);
descriptor.addMapping(objecttypemapping);
// SECTION: ONETOMANYMAPPING
OneToManyMapping onetomanymapping = new OneToManyMapping();
onetomanymapping.setAttributeName("managedEmployees");
onetomanymapping.setIsReadOnly(false);
onetomanymapping.setUsesIndirection(false);
onetomanymapping.setGetMethodName("getManagedEmployeesForTOPLink");
onetomanymapping.setSetMethodName("setManagedEmployeesFromTOPLink");
onetomanymapping.setReferenceClass(Employee.class);
onetomanymapping.setIsPrivateOwned(false);
onetomanymapping.addTargetForeignKeyFieldName("MAP_EMP.M_LNAME", "MAP_EMP.LNAME");
onetomanymapping.addTargetForeignKeyFieldName("MAP_EMP.M_FNAME", "MAP_EMP.FNAME");
descriptor.addMapping(onetomanymapping);
// SECTION: ONETOONEMAPPING
OneToOneMapping onetoonemapping = new OneToOneMapping();
onetoonemapping.setAttributeName("computer");
onetoonemapping.setIsReadOnly(false);
onetoonemapping.setUsesIndirection(false);
onetoonemapping.setGetMethodName("getComputer");
onetoonemapping.setSetMethodName("setComputer");
onetoonemapping.setReferenceClass(Hardware.class);
onetoonemapping.setIsPrivateOwned(true);
onetoonemapping.addTargetForeignKeyFieldName("MAP_HRW.EMP_FNAME", "MAP_EMP.FNAME");
onetoonemapping.addTargetForeignKeyFieldName("MAP_HRW.EMP_LNAME", "MAP_EMP.LNAME");
descriptor.addMapping(onetoonemapping);
// SECTION: ONETOONEMAPPING
OneToOneMapping onetoonemapping2 = new OneToOneMapping();
onetoonemapping2.setAttributeName("manager");
onetoonemapping2.setIsReadOnly(false);
onetoonemapping2.setUsesIndirection(false);
onetoonemapping2.setGetMethodName("getManager");
onetoonemapping2.setSetMethodName("setManager");
onetoonemapping2.setReferenceClass(Employee.class);
onetoonemapping2.setIsPrivateOwned(false);
onetoonemapping2.addForeignKeyFieldName("MAP_EMP.M_FNAME", "MAP_EMP.FNAME");
onetoonemapping2.addForeignKeyFieldName("MAP_EMP.M_LNAME", "MAP_EMP.LNAME");
descriptor.addMapping(onetoonemapping2);
// SECTION: SERIALIZEDMAPPING
DirectToFieldMapping serializedmapping = new DirectToFieldMapping();
SerializedObjectConverter serializedconverter = new SerializedObjectConverter();
serializedmapping.setAttributeName("jobDescription");
serializedmapping.setIsReadOnly(false);
serializedmapping.setGetMethodName("getJobDescription");
serializedmapping.setSetMethodName("setJobDescription");
serializedmapping.setFieldName("MAP_EMP.JDESC");
serializedmapping.setConverter(serializedconverter);
descriptor.addMapping(serializedmapping);
// SECTION: TRANSFORMATIONMAPPING
TransformationMapping transformationmapping = new TransformationMapping();
transformationmapping.setAttributeName("dateAndTimeOfBirth");
transformationmapping.setIsReadOnly(false);
transformationmapping.setUsesIndirection(false);
transformationmapping.setAttributeTransformation("setDateAndTime");
transformationmapping.addFieldTransformation("MAP_EMP.BDAY", "getDate");
transformationmapping.addFieldTransformation("MAP_EMP.BTIME", "getTime");
descriptor.addMapping(transformationmapping);
// SECTION: TRANSFORMATIONMAPPING
TransformationMapping transformationmapping1 = new TransformationMapping();
transformationmapping1.setAttributeName("designation");
transformationmapping1.setIsReadOnly(false);
transformationmapping1.setUsesIndirection(true);
transformationmapping1.setGetMethodName("getDesignation");
transformationmapping1.setSetMethodName("setDesignation");
transformationmapping1.setAttributeTransformation("getRankFromRow");
transformationmapping1.addFieldTransformation("MAP_EMP.ERANK", "getRankFromObject");
descriptor.addMapping(transformationmapping1);
// SECTION: TYPECONVERSIONMAPPING
DirectToFieldMapping typeconversionmapping = new DirectToFieldMapping();
TypeConversionConverter typeconversionconverter = new TypeConversionConverter();
typeconversionmapping.setAttributeName("joiningDate");
typeconversionmapping.setIsReadOnly(false);
typeconversionmapping.setFieldName("MAP_EMP.JDAY");
typeconversionconverter.setObjectClass(java.util.Date.class);
typeconversionconverter.setDataClass(java.sql.Date.class);
typeconversionmapping.setConverter(typeconversionconverter);
descriptor.addMapping(typeconversionmapping);
addDescriptor(descriptor);
}
use of org.eclipse.persistence.mappings.OneToManyMapping in project eclipselink by eclipse-ee4j.
the class IsolatedEmployeeProject method buildEmployeeDescriptor.
public RelationalDescriptor buildEmployeeDescriptor() {
RelationalDescriptor descriptor = new RelationalDescriptor();
descriptor.setJavaClass(org.eclipse.persistence.testing.tests.isolatedsession.IsolatedEmployee.class);
descriptor.addTableName("ISOLATED_EMPLOYEE");
descriptor.addTableName("ISOLATED_SALARY");
descriptor.addPrimaryKeyFieldName("ISOLATED_EMPLOYEE.EMP_ID");
// Descriptor properties.
descriptor.setSequenceNumberFieldName("EMP_ID");
descriptor.setSequenceNumberName("EMP_SEQ");
VersionLockingPolicy lockingPolicy = new VersionLockingPolicy();
lockingPolicy.setWriteLockFieldName("VERSION");
descriptor.setOptimisticLockingPolicy(lockingPolicy);
descriptor.setCacheIsolation(CacheIsolationType.ISOLATED);
// Tests depend on uow merge.
descriptor.setUnitOfWorkCacheIsolationLevel(RelationalDescriptor.ISOLATE_NEW_DATA_AFTER_TRANSACTION);
// Query manager.
descriptor.getQueryManager().checkCacheForDoesExist();
// Event manager.
// Mappings.
DirectToFieldMapping firstNameMapping = new DirectToFieldMapping();
firstNameMapping.setAttributeName("firstName");
firstNameMapping.setFieldName("ISOLATED_EMPLOYEE.F_NAME");
firstNameMapping.setNullValue("");
descriptor.addMapping(firstNameMapping);
DirectToFieldMapping idMapping = new DirectToFieldMapping();
idMapping.setAttributeName("id");
idMapping.setFieldName("ISOLATED_EMPLOYEE.EMP_ID");
descriptor.addMapping(idMapping);
DirectToFieldMapping lastNameMapping = new DirectToFieldMapping();
lastNameMapping.setAttributeName("lastName");
lastNameMapping.setFieldName("ISOLATED_EMPLOYEE.L_NAME");
lastNameMapping.setNullValue("");
descriptor.addMapping(lastNameMapping);
DirectToFieldMapping salaryMapping = new DirectToFieldMapping();
salaryMapping.setAttributeName("salary");
salaryMapping.setFieldName("ISOLATED_SALARY.SALARY");
descriptor.addMapping(salaryMapping);
DirectToFieldMapping genderMapping = new DirectToFieldMapping();
ObjectTypeConverter genderConverter = new ObjectTypeConverter();
genderMapping.setAttributeName("gender");
genderMapping.setFieldName("ISOLATED_EMPLOYEE.GENDER");
genderConverter.addConversionValue("M", "Male");
genderConverter.addConversionValue("F", "Female");
genderMapping.setConverter(genderConverter);
descriptor.addMapping(genderMapping);
TransformationMapping normalHoursMapping = new TransformationMapping();
normalHoursMapping.setAttributeName("normalHours");
normalHoursMapping.setAttributeTransformation("buildNormalHours");
normalHoursMapping.addFieldTransformation("ISOLATED_EMPLOYEE.START_TIME", "getStartTime");
normalHoursMapping.addFieldTransformation("ISOLATED_EMPLOYEE.END_TIME", "getEndTime");
descriptor.addMapping(normalHoursMapping);
AggregateObjectMapping periodMapping = new AggregateObjectMapping();
periodMapping.setAttributeName("period");
periodMapping.setReferenceClass(org.eclipse.persistence.testing.tests.isolatedsession.IsolatedEmploymentPeriod.class);
periodMapping.setIsNullAllowed(true);
descriptor.addMapping(periodMapping);
DirectCollectionMapping responsibilitiesListMapping = new DirectCollectionMapping();
responsibilitiesListMapping.setAttributeName("responsibilitiesList");
responsibilitiesListMapping.useBasicIndirection();
responsibilitiesListMapping.setReferenceTableName("ISOLATED_RESPONS");
responsibilitiesListMapping.setDirectFieldName("ISOLATED_RESPONS.DESCRIP");
responsibilitiesListMapping.addReferenceKeyFieldName("ISOLATED_RESPONS.EMP_ID", "ISOLATED_EMPLOYEE.EMP_ID");
descriptor.addMapping(responsibilitiesListMapping);
OneToOneMapping addressMapping = new OneToOneMapping();
addressMapping.setAttributeName("address");
addressMapping.setReferenceClass(org.eclipse.persistence.testing.tests.isolatedsession.IsolatedAddress.class);
addressMapping.useBasicIndirection();
addressMapping.privateOwnedRelationship();
addressMapping.addForeignKeyFieldName("ISOLATED_EMPLOYEE.ADDR_ID", "ISOLATED_ADDRESS.ADDRESS_ID");
descriptor.addMapping(addressMapping);
OneToOneMapping managerMapping = new OneToOneMapping();
managerMapping.setAttributeName("manager");
managerMapping.setReferenceClass(IsolatedEmployee.class);
managerMapping.useBasicIndirection();
managerMapping.addForeignKeyFieldName("ISOLATED_EMPLOYEE.MANAGER_ID", "ISOLATED_EMPLOYEE.EMP_ID");
descriptor.addMapping(managerMapping);
OneToManyMapping managedEmployeesMapping = new OneToManyMapping();
managedEmployeesMapping.setAttributeName("managedEmployees");
managedEmployeesMapping.setReferenceClass(IsolatedEmployee.class);
managedEmployeesMapping.useBasicIndirection();
managedEmployeesMapping.addTargetForeignKeyFieldName("ISOLATED_EMPLOYEE.MANAGER_ID", "ISOLATED_EMPLOYEE.EMP_ID");
descriptor.addMapping(managedEmployeesMapping);
OneToManyMapping phoneNumbersMapping = new OneToManyMapping();
phoneNumbersMapping.setAttributeName("phoneNumbers");
phoneNumbersMapping.setReferenceClass(IsolatedPhoneNumber.class);
phoneNumbersMapping.useBasicIndirection();
phoneNumbersMapping.privateOwnedRelationship();
phoneNumbersMapping.addTargetForeignKeyFieldName("ISOLATED_PHONE.EMP_ID", "ISOLATED_EMPLOYEE.EMP_ID");
descriptor.addMapping(phoneNumbersMapping);
return descriptor;
}
Aggregations