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));
}
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);
}
}
}
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);
}
}
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;
}
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));
}
}
Aggregations