Search in sources :

Example 1 with PropertyData

use of org.hibernate.envers.internal.entities.PropertyData in project hibernate-orm by hibernate.

the class ToOneRelationMetadataGenerator method addToOne.

@SuppressWarnings({ "unchecked" })
void addToOne(Element parent, PropertyAuditingData propertyAuditingData, Value value, CompositeMapperBuilder mapper, String entityName, boolean insertable) {
    final String referencedEntityName = ((ToOne) value).getReferencedEntityName();
    final IdMappingData idMapping = mainGenerator.getReferencedIdMappingData(entityName, referencedEntityName, propertyAuditingData, true);
    final String lastPropertyPrefix = MappingTools.createToOneRelationPrefix(propertyAuditingData.getName());
    // Generating the id mapper for the relation
    final IdMapper relMapper = idMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix);
    // Storing information about this relation
    mainGenerator.getEntitiesConfigurations().get(entityName).addToOneRelation(propertyAuditingData.getName(), referencedEntityName, relMapper, insertable, MappingTools.ignoreNotFound(value));
    // If the property isn't insertable, checking if this is not a "fake" bidirectional many-to-one relationship,
    // that is, when the one side owns the relation (and is a collection), and the many side is non insertable.
    // When that's the case and the user specified to store this relation without a middle table (using
    // @AuditMappedBy), we have to make the property insertable for the purposes of Envers. In case of changes to
    // the entity that didn't involve the relation, it's value will then be stored properly. In case of changes
    // to the entity that did involve the relation, it's the responsibility of the collection side to store the
    // proper data.
    boolean nonInsertableFake;
    if (!insertable && propertyAuditingData.isForceInsertable()) {
        nonInsertableFake = true;
        insertable = true;
    } else {
        nonInsertableFake = false;
    }
    // Adding an element to the mapping corresponding to the references entity id's
    final Element properties = (Element) idMapping.getXmlRelationMapping().clone();
    properties.addAttribute("name", propertyAuditingData.getName());
    MetadataTools.prefixNamesInPropertyElement(properties, lastPropertyPrefix, MetadataTools.getColumnNameIterator(value.getColumnIterator()), false, insertable);
    // Extracting related id properties from properties tag
    for (Object o : properties.content()) {
        final Element element = (Element) o;
        element.setParent(null);
        parent.add(element);
    }
    // Adding mapper for the id
    final PropertyData propertyData = propertyAuditingData.getPropertyData();
    mapper.addComposite(propertyData, new ToOneIdMapper(relMapper, propertyData, referencedEntityName, nonInsertableFake));
}
Also used : PropertyData(org.hibernate.envers.internal.entities.PropertyData) Element(org.dom4j.Element) OneToOne(org.hibernate.mapping.OneToOne) ToOne(org.hibernate.mapping.ToOne) IdMapper(org.hibernate.envers.internal.entities.mapper.id.IdMapper) ToOneIdMapper(org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper) ToOneIdMapper(org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper) IdMappingData(org.hibernate.envers.internal.entities.IdMappingData)

Example 2 with PropertyData

use of org.hibernate.envers.internal.entities.PropertyData in project hibernate-orm by hibernate.

the class ComponentPropertyMapper method mapToEntityFromMap.

@Override
public void mapToEntityFromMap(EnversService enversService, Object obj, Map data, Object primaryKey, AuditReaderImplementor versionsReader, Number revision) {
    if (data == null || obj == null) {
        return;
    }
    if (propertyData.getBeanName() == null) {
        // If properties are not encapsulated in a component but placed directly in a class
        // (e.g. by applying <properties> tag).
        delegate.mapToEntityFromMap(enversService, obj, data, primaryKey, versionsReader, revision);
        return;
    }
    final Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyData, enversService.getServiceRegistry());
    // If all properties are null and single, then the component has to be null also.
    boolean allNullAndSingle = true;
    for (Map.Entry<PropertyData, PropertyMapper> property : delegate.getProperties().entrySet()) {
        if (data.get(property.getKey().getName()) != null || !(property.getValue() instanceof SinglePropertyMapper)) {
            allNullAndSingle = false;
            break;
        }
    }
    if (allNullAndSingle) {
        // single property, but default value need not be null, so we'll set it to null anyway
        setter.set(obj, null, null);
    } else {
        // set the component
        try {
            final Object subObj = ReflectHelper.getDefaultConstructor(componentClass).newInstance();
            setter.set(obj, subObj, null);
            delegate.mapToEntityFromMap(enversService, subObj, data, primaryKey, versionsReader, revision);
        } catch (Exception e) {
            throw new AuditException(e);
        }
    }
}
Also used : PropertyData(org.hibernate.envers.internal.entities.PropertyData) Setter(org.hibernate.property.access.spi.Setter) AuditException(org.hibernate.envers.exception.AuditException) HashMap(java.util.HashMap) Map(java.util.Map) AuditException(org.hibernate.envers.exception.AuditException)

Example 3 with PropertyData

use of org.hibernate.envers.internal.entities.PropertyData in project hibernate-orm by hibernate.

the class ComponentPropertyMapper method mapModifiedFlagsToMapForCollectionChange.

@Override
public void mapModifiedFlagsToMapForCollectionChange(String collectionPropertyName, Map<String, Object> data) {
    if (propertyData.isUsingModifiedFlag()) {
        boolean hasModifiedCollection = false;
        for (PropertyData propData : delegate.getProperties().keySet()) {
            if (collectionPropertyName.equals(propData.getName())) {
                hasModifiedCollection = true;
                break;
            }
        }
        data.put(propertyData.getModifiedFlagPropertyName(), hasModifiedCollection);
    }
}
Also used : PropertyData(org.hibernate.envers.internal.entities.PropertyData)

Example 4 with PropertyData

use of org.hibernate.envers.internal.entities.PropertyData in project hibernate-orm by hibernate.

the class MultiPropertyMapper method mapToMapFromEntity.

@Override
public boolean mapToMapFromEntity(SessionImplementor session, Map<String, Object> data, Object newObj, Object oldObj) {
    boolean ret = false;
    for (Map.Entry<PropertyData, PropertyMapper> entry : properties.entrySet()) {
        final PropertyData propertyData = entry.getKey();
        final PropertyMapper propertyMapper = entry.getValue();
        // synthetic properties are not part of the entity model; therefore they should be ignored.
        if (propertyData.isSynthetic()) {
            continue;
        }
        Getter getter;
        if (newObj != null) {
            getter = ReflectionTools.getGetter(newObj.getClass(), propertyData, session.getFactory().getServiceRegistry());
        } else if (oldObj != null) {
            getter = ReflectionTools.getGetter(oldObj.getClass(), propertyData, session.getFactory().getServiceRegistry());
        } else {
            return false;
        }
        ret |= propertyMapper.mapToMapFromEntity(session, data, newObj == null ? null : getter.get(newObj), oldObj == null ? null : getter.get(oldObj));
    }
    return ret;
}
Also used : PropertyData(org.hibernate.envers.internal.entities.PropertyData) Getter(org.hibernate.property.access.spi.Getter) Map(java.util.Map)

Example 5 with PropertyData

use of org.hibernate.envers.internal.entities.PropertyData in project hibernate-orm by hibernate.

the class MultiPropertyMapper method mapModifiedFlagsToMapFromEntity.

@Override
public void mapModifiedFlagsToMapFromEntity(SessionImplementor session, Map<String, Object> data, Object newObj, Object oldObj) {
    for (Map.Entry<PropertyData, PropertyMapper> entry : properties.entrySet()) {
        final PropertyData propertyData = entry.getKey();
        final PropertyMapper propertyMapper = entry.getValue();
        // synthetic properties are not part of the entity model; therefore they should be ignored.
        if (propertyData.isSynthetic()) {
            continue;
        }
        Getter getter;
        if (newObj != null) {
            getter = ReflectionTools.getGetter(newObj.getClass(), propertyData, session.getFactory().getServiceRegistry());
        } else if (oldObj != null) {
            getter = ReflectionTools.getGetter(oldObj.getClass(), propertyData, session.getFactory().getServiceRegistry());
        } else {
            return;
        }
        propertyMapper.mapModifiedFlagsToMapFromEntity(session, data, newObj == null ? null : getter.get(newObj), oldObj == null ? null : getter.get(oldObj));
    }
}
Also used : PropertyData(org.hibernate.envers.internal.entities.PropertyData) Getter(org.hibernate.property.access.spi.Getter) Map(java.util.Map)

Aggregations

PropertyData (org.hibernate.envers.internal.entities.PropertyData)16 Map (java.util.Map)6 ToOneIdMapper (org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper)5 IdMappingData (org.hibernate.envers.internal.entities.IdMappingData)4 IdMapper (org.hibernate.envers.internal.entities.mapper.id.IdMapper)4 Element (org.dom4j.Element)3 OneToOne (org.hibernate.mapping.OneToOne)3 MappingException (org.hibernate.MappingException)2 MultiPropertyMapper (org.hibernate.envers.internal.entities.mapper.MultiPropertyMapper)2 PropertyMapper (org.hibernate.envers.internal.entities.mapper.PropertyMapper)2 ToOne (org.hibernate.mapping.ToOne)2 Getter (org.hibernate.property.access.spi.Getter)2 BasicType (org.hibernate.type.BasicType)2 Type (org.hibernate.type.Type)2 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 Column (javax.persistence.Column)1 XClass (org.hibernate.annotations.common.reflection.XClass)1 XProperty (org.hibernate.annotations.common.reflection.XProperty)1