use of org.eclipse.persistence.mappings.OneToOneMapping in project cuba by cuba-platform.
the class JoinCriteriaMappingProcessor method process.
@Override
public void process(MappingProcessorContext context) {
DatabaseMapping mapping = context.getMapping();
Expression expression = AppBeans.getAll(JoinExpressionProvider.class).values().stream().map(provider -> provider.getJoinCriteriaExpression(mapping)).filter(Objects::nonNull).reduce(Expression::and).orElse(null);
// Applying additional join criteria, e.g. for soft delete or multitenancy -> move to mapping processor
if (mapping.isOneToManyMapping() || mapping.isOneToOneMapping()) {
// Apply expression to mappings
if (mapping.isOneToManyMapping()) {
((OneToManyMapping) mapping).setAdditionalJoinCriteria(expression);
} else if (mapping.isOneToOneMapping()) {
((OneToOneMapping) mapping).setAdditionalJoinCriteria(expression);
}
}
}
use of org.eclipse.persistence.mappings.OneToOneMapping in project jmix by jmix-framework.
the class SoftDeleteProcessor method process.
@Override
public void process(MappingProcessorContext context) {
DatabaseMapping mapping = context.getMapping();
MetaClass metaClass = metadata.getClass(mapping.getDescriptor().getJavaClass());
MetaProperty metaProperty = metaClass.getProperty(mapping.getAttributeName());
if (mapping.isOneToOneMapping()) {
OneToOneMapping oneToOneMapping = (OneToOneMapping) mapping;
if (metadataTools.isSoftDeletable(oneToOneMapping.getReferenceClass())) {
if (mapping.isManyToOneMapping()) {
oneToOneMapping.setSoftDeletionForBatch(false);
oneToOneMapping.setSoftDeletionForValueHolder(false);
} else {
OneToOne oneToOne = metaProperty.getAnnotatedElement().getAnnotation(OneToOne.class);
if (oneToOne != null) {
if (Strings.isNullOrEmpty(oneToOne.mappedBy())) {
oneToOneMapping.setSoftDeletionForBatch(false);
oneToOneMapping.setSoftDeletionForValueHolder(false);
}
}
}
}
}
}
use of org.eclipse.persistence.mappings.OneToOneMapping in project eclipselink by eclipse-ee4j.
the class MappingAccessor method processEntityMapKeyClass.
/**
* INTERNAL:
* Process the map key to be an entity class.
*/
protected OneToOneMapping processEntityMapKeyClass(MappedKeyMapAccessor mappedKeyMapAccessor) {
String mapKeyClassName = mappedKeyMapAccessor.getMapKeyClass().getName();
// Create the one to one map key mapping.
OneToOneMapping keyMapping = new OneToOneMapping();
keyMapping.setReferenceClassName(mapKeyClassName);
keyMapping.dontUseIndirection();
keyMapping.setDescriptor(getDescriptor().getClassDescriptor());
// Process the map key join columns.
EntityAccessor mapKeyAccessor = getProject().getEntityAccessor(mapKeyClassName);
MetadataDescriptor mapKeyClassDescriptor = mapKeyAccessor.getDescriptor();
// If the fk field (name) is not specified, it defaults to the
// concatenation of the following: the name of the referencing
// relationship property or field of the referencing entity or
// embeddable; "_"; "KEY"
String defaultFKFieldName = getAttributeName() + DEFAULT_MAP_KEY_COLUMN_SUFFIX;
// Get the join columns (directly or through an association override),
// init them and validate.
List<JoinColumnMetadata> joinColumns = getJoinColumns(mappedKeyMapAccessor.getMapKeyJoinColumns(), mapKeyClassDescriptor);
// Get the foreign key (directly or through an association override) and
// make sure it is initialized for processing.
ForeignKeyMetadata foreignKey = getForeignKey(mappedKeyMapAccessor.getMapKeyForeignKey(), mapKeyClassDescriptor);
// Now process the foreign key relationship metadata.
processForeignKeyRelationship(keyMapping, joinColumns, foreignKey, mapKeyClassDescriptor, defaultFKFieldName, getDefaultTableForEntityMapKey());
return keyMapping;
}
use of org.eclipse.persistence.mappings.OneToOneMapping in project eclipselink by eclipse-ee4j.
the class CMP3Policy method createPrimaryKeyFromId.
/**
* INTERNAL:
* Use the key to create a EclipseLink primary key.
* If the key is simple (direct mapped) then just add it to a vector,
* otherwise must go through the inefficient process of copying the key into the bean
* and extracting the key from the bean.
*/
@Override
public Object createPrimaryKeyFromId(Object key, AbstractSession session) {
// If the descriptor primary key is mapped through direct-to-field mappings,
// then no elaborate conversion is required.
// If key is compound, add each value to the vector.
KeyElementAccessor[] pkElementArray = this.getKeyClassFields();
Object[] primaryKey = null;
if (getDescriptor().getCacheKeyType() != CacheKeyType.ID_VALUE) {
primaryKey = new Object[pkElementArray.length];
}
for (int index = 0; index < pkElementArray.length; index++) {
DatabaseMapping mapping = pkElementArray[index].getMapping();
Object fieldValue = null;
if (mapping.isAbstractColumnMapping()) {
if (pkElementArray[index].isNestedAccessor()) {
// We have nested aggregate(s) in the embedded id pkclass.
DatabaseField keyField = pkElementArray[index].getDatabaseField();
Object keyToUse = key;
DatabaseMapping keyMapping = getDescriptor().getObjectBuilder().getMappingForField(keyField);
if (keyMapping.isAggregateMapping()) {
keyMapping = keyMapping.getReferenceDescriptor().getObjectBuilder().getMappingForField(keyField);
// Keep driving down the nested aggregates ...
while (keyMapping.isAggregateMapping()) {
keyToUse = keyMapping.getRealAttributeValueFromObject(keyToUse, session);
keyMapping = keyMapping.getReferenceDescriptor().getObjectBuilder().getMappingForField(keyField);
}
fieldValue = ((AbstractColumnMapping) mapping).getFieldValue(pkElementArray[index].getValue(keyToUse, session), session);
} else {
// This should never hit but just in case ... better to get a proper exception rather than a NPE etc.
fieldValue = ((AbstractColumnMapping) mapping).getFieldValue(pkElementArray[index].getValue(keyToUse, session), session);
}
} else {
fieldValue = ((AbstractColumnMapping) mapping).getFieldValue(pkElementArray[index].getValue(key, session), session);
}
} else {
fieldValue = pkElementArray[index].getValue(key, session);
if ((fieldValue != null) && (pkClass != null) && (mapping.isOneToOneMapping())) {
OneToOneMapping refmapping = (OneToOneMapping) mapping;
DatabaseField targetKey = refmapping.getSourceToTargetKeyFields().get(pkElementArray[index].getDatabaseField());
CMPPolicy refPolicy = refmapping.getReferenceDescriptor().getCMPPolicy();
if (refPolicy.isCMP3Policy()) {
Class<Object> aPKClass = refPolicy.getPKClass();
if ((aPKClass != null) && (aPKClass != fieldValue.getClass()) && (!aPKClass.isAssignableFrom(fieldValue.getClass()))) {
throw new IllegalArgumentException(ExceptionLocalization.buildMessage("invalid_pk_class", new Object[] { aPKClass, fieldValue.getClass() }));
}
fieldValue = ((CMP3Policy) refPolicy).getPkValueFromKeyForField(fieldValue, targetKey, session);
}
}
}
if (getDescriptor().getCacheKeyType() == CacheKeyType.ID_VALUE) {
return fieldValue;
}
primaryKey[index] = fieldValue;
}
return new CacheId(primaryKey);
}
use of org.eclipse.persistence.mappings.OneToOneMapping in project eclipselink by eclipse-ee4j.
the class CMP3Policy method getPkValueFromKeyForField.
/**
* INTERNAL:
* Pull the value for the field from the key.
*
* @param key Object the primary key to use to get the value for the field
* @param field DatabaseField the field to find a value for
* @return Object
*/
public Object getPkValueFromKeyForField(Object key, DatabaseField field, AbstractSession session) {
Object fieldValue = null;
KeyElementAccessor accessor = this.fieldToAccessorMap.get(field);
DatabaseMapping mapping = accessor.getMapping();
if (mapping.isAbstractColumnMapping()) {
fieldValue = ((AbstractColumnMapping) mapping).getFieldValue(accessor.getValue(key, session), session);
} else {
fieldValue = accessor.getValue(key, session);
if (mapping.isOneToOneMapping()) {
OneToOneMapping refmapping = (OneToOneMapping) mapping;
DatabaseField targetKey = refmapping.getSourceToTargetKeyFields().get(accessor.getDatabaseField());
CMPPolicy refPolicy = refmapping.getReferenceDescriptor().getCMPPolicy();
if (refPolicy.isCMP3Policy()) {
Class<Object> pkClass = refPolicy.getPKClass();
if ((pkClass != null) && (pkClass != fieldValue.getClass()) && (!pkClass.isAssignableFrom(fieldValue.getClass()))) {
throw new IllegalArgumentException(ExceptionLocalization.buildMessage("invalid_pk_class", new Object[] { refPolicy.getPKClass(), fieldValue.getClass() }));
}
fieldValue = ((CMP3Policy) refPolicy).getPkValueFromKeyForField(fieldValue, targetKey, session);
}
}
}
return fieldValue;
}
Aggregations