public void AddOneToOneNotOwning(PropertyAuditingData propertyAuditingData, OneToOne value, ICompositeMapperBuilder mapper, string entityName) { var owningReferencePropertyName = referencePropertyName(value, entityName); var configuration = mainGenerator.EntitiesConfigurations[entityName]; if (configuration == null) { throw new MappingException("An audited relation to a non-audited entity " + entityName + "!"); } var ownedIdMapping = configuration.IdMappingData; if (ownedIdMapping == null) { throw new MappingException("An audited relation to a non-audited entity " + entityName + "!"); } var lastPropertyPrefix = MappingTools.CreateToOneRelationPrefix(owningReferencePropertyName); var referencedEntityName = value.ReferencedEntityName; // Generating the id mapper for the relation var ownedIdMapper = ownedIdMapping.IdMapper.PrefixMappedProperties(lastPropertyPrefix); // Storing information about this relation mainGenerator.EntitiesConfigurations[entityName].AddToOneNotOwningRelation( propertyAuditingData.Name, owningReferencePropertyName, referencedEntityName, ownedIdMapper); // Adding mapper for the id var propertyData = propertyAuditingData.GetPropertyData(); mapper.AddComposite(propertyData, new OneToOneNotOwningMapper(owningReferencePropertyName, referencedEntityName, propertyData)); }
public void AddComponent(XmlElement parent, PropertyAuditingData propertyAuditingData, IValue value, ICompositeMapperBuilder mapper, string entityName, EntityXmlMappingData xmlMappingData, bool firstPass, bool insertable) { var propComponent = (Component) value; var componentMapper = mapper.AddComponent(propertyAuditingData.GetPropertyData(), propComponent.ComponentClassName); // The property auditing data must be for a component. var componentAuditingData = (ComponentAuditingData) propertyAuditingData; // Adding all properties of the component foreach (var property in propComponent.PropertyIterator) { var componentPropertyAuditingData = componentAuditingData.GetPropertyAuditingData(property.Name); // Checking if that property is audited if (componentPropertyAuditingData != null) { mainGenerator.AddValue(parent, property.Value, componentMapper, entityName, xmlMappingData, componentPropertyAuditingData, property.IsInsertable && insertable, firstPass); } } }
//@SuppressWarnings({"unchecked"}) public void AddComponent(XmlElement parent, PropertyAuditingData propertyAuditingData, IValue value, ICompositeMapperBuilder mapper, String entityName, EntityXmlMappingData xmlMappingData, bool firstPass) { Component prop_component = (Component) value; ICompositeMapperBuilder componentMapper = mapper.AddComponent(propertyAuditingData.getPropertyData(), prop_component.ComponentClassName); // The property auditing data must be for a component. ComponentAuditingData componentAuditingData = (ComponentAuditingData) propertyAuditingData; // Adding all properties of the component IEnumerator<Property> properties = (IEnumerator<Property>) prop_component.PropertyIterator.GetEnumerator(); while (properties.MoveNext()) { Property property = properties.Current; PropertyAuditingData componentPropertyAuditingData = componentAuditingData.getPropertyAuditingData(property.Name); // Checking if that property is audited if (componentPropertyAuditingData != null) { mainGenerator.AddValue(parent, property.Value, componentMapper, entityName, xmlMappingData, componentPropertyAuditingData, property.IsInsertable, firstPass); } } }
//@SuppressWarnings({"unchecked"}) public void AddOneToOneNotOwning(PropertyAuditingData propertyAuditingData, IValue value, ICompositeMapperBuilder mapper, String entityName) { OneToOne propertyValue = (OneToOne)value; String owningReferencePropertyName = propertyValue.ReferencedPropertyName; // mappedBy EntityConfiguration configuration = mainGenerator.EntitiesConfigurations[entityName]; if (configuration == null) { throw new MappingException("An audited relation to a non-audited entity " + entityName + "!"); } IdMappingData ownedIdMapping = configuration.IdMappingData; if (ownedIdMapping == null) { throw new MappingException("An audited relation to a non-audited entity " + entityName + "!"); } String lastPropertyPrefix = MappingTools.createToOneRelationPrefix(owningReferencePropertyName); String referencedEntityName = propertyValue.ReferencedEntityName; // Generating the id mapper for the relation IIdMapper ownedIdMapper = ownedIdMapping.IdMapper.PrefixMappedProperties(lastPropertyPrefix); // Storing information about this relation mainGenerator.EntitiesConfigurations[entityName].AddToOneNotOwningRelation( propertyAuditingData.Name, owningReferencePropertyName, referencedEntityName, ownedIdMapper); // Adding mapper for the id PropertyData propertyData = propertyAuditingData.getPropertyData(); mapper.AddComposite(propertyData, new OneToOneNotOwningMapper(owningReferencePropertyName, referencedEntityName, propertyData)); }
public void AddToOne(XmlElement parent, PropertyAuditingData propertyAuditingData, IValue value, ICompositeMapperBuilder mapper, string entityName, bool insertable, IEnumerable<string> fixedColumnNames) { var referencedEntityName = ((ToOne)value).ReferencedEntityName; var idMapping = mainGenerator.GetReferencedIdMappingData(entityName, referencedEntityName, propertyAuditingData, true); var lastPropertyPrefix = MappingTools.CreateToOneRelationPrefix(propertyAuditingData.Name); // Generating the id mapper for the relation var relMapper = idMapping.IdMapper.PrefixMappedProperties(lastPropertyPrefix); // Storing information about this relation mainGenerator.EntitiesConfigurations[entityName].AddToOneRelation( propertyAuditingData.Name, referencedEntityName, relMapper, insertable); // 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. bool nonInsertableFake; if (!insertable && propertyAuditingData.ForceInsertable) { nonInsertableFake = true; insertable = true; } else { nonInsertableFake = false; } // Adding an element to the mapping corresponding to the references entity id's // Use OwnerDocument.ImportNode() instead of XmlNode.Clone(); var properties = (XmlElement)parent.OwnerDocument.ImportNode(idMapping.XmlRelationMapping,true); properties.SetAttribute("name",propertyAuditingData.Name); MetadataTools.PrefixNamesInPropertyElement(properties, lastPropertyPrefix, fixedColumnNames == null ? MetadataTools.GetColumnNameEnumerator(value.ColumnIterator) : fixedColumnNames.GetEnumerator(), false, insertable); parent.AppendChild(properties); // Adding mapper for the id var propertyData = propertyAuditingData.GetPropertyData(); mapper.AddComposite(propertyData, new ToOneIdMapper(relMapper,propertyData,referencedEntityName,nonInsertableFake)); }
/** * @param mainGenerator Main generator, giving access to configuration and the basic mapper. * @param propertyValue Value of the collection, as mapped by Hibernate. * @param currentMapper Mapper, to which the appropriate {@link org.hibernate.envers.entities.mapper.PropertyMapper} * will be added. * @param referencingEntityName Name of the entity that owns this collection. * @param xmlMappingData In case this collection requires a middle table, additional mapping documents will * be created using this object. * @param propertyAuditingData Property auditing (meta-)data. Among other things, holds the name of the * property that references the collection in the referencing entity, the user data for middle (join) * table and the value of the <code>@MapKey</code> annotation, if there was one. */ public CollectionMetadataGenerator(AuditMetadataGenerator mainGenerator, Mapping.Collection propertyValue, ICompositeMapperBuilder currentMapper, String referencingEntityName, EntityXmlMappingData xmlMappingData, PropertyAuditingData propertyAuditingData) { this.mainGenerator = mainGenerator; this.propertyValue = propertyValue; this.currentMapper = currentMapper; this.referencingEntityName = referencingEntityName; this.xmlMappingData = xmlMappingData; this.propertyAuditingData = propertyAuditingData; this.propertyName = propertyAuditingData.Name; referencingEntityConfiguration = mainGenerator.EntitiesConfigurations[referencingEntityName]; if (referencingEntityConfiguration == null) { throw new MappingException("Unable to read auditing configuration for " + referencingEntityName + "!"); } referencedEntityName = MappingTools.getReferencedEntityName(propertyValue.Element); }
//@SuppressWarnings({"unchecked"}) private void AddJoins(PersistentClass pc, ICompositeMapperBuilder currentMapper, ClassAuditingData auditingData, String entityName, EntityXmlMappingData xmlMappingData, bool firstPass) { IEnumerator<Join> joins = pc.JoinIterator.GetEnumerator(); while (joins.MoveNext()) { Join join = joins.Current; XmlElement joinElement = entitiesJoins[entityName][join]; if (joinElement != null) { AddProperties(joinElement, (IEnumerator<Property>)join.PropertyIterator.GetEnumerator(), currentMapper, auditingData, entityName, xmlMappingData, firstPass); } } }
//@SuppressWarnings({"unchecked"}) private void AddProperties(XmlElement parent, IEnumerator<Property> properties, ICompositeMapperBuilder currentMapper, ClassAuditingData auditingData, String entityName, EntityXmlMappingData xmlMappingData, bool firstPass) { while (properties.MoveNext()) { Property property = properties.Current; String propertyName = property.Name; PropertyAuditingData propertyAuditingData = auditingData.getPropertyAuditingData(propertyName); if (propertyAuditingData != null) { AddValue(parent, property.Value, currentMapper, entityName, xmlMappingData, propertyAuditingData, property.IsInsertable, firstPass); } } }
//@SuppressWarnings({"unchecked"}) public void AddValue(XmlElement parent, IValue value, ICompositeMapperBuilder currentMapper, String entityName, EntityXmlMappingData xmlMappingData, PropertyAuditingData propertyAuditingData, bool insertable, bool firstPass) { IType type = value.Type; // only first pass if (firstPass) { if (BasicMetadataGenerator.AddBasic(parent, propertyAuditingData, value, currentMapper, insertable, false)) { // The property was mapped by the basic generator. return; } } if (type is ComponentType) { // both passes componentMetadataGenerator.AddComponent(parent, propertyAuditingData, value, currentMapper, entityName, xmlMappingData, firstPass); } else if (type is ManyToOneType) { // only second pass if (!firstPass) { toOneRelationMetadataGenerator.AddToOne(parent, propertyAuditingData, value, currentMapper, entityName, insertable); } } else if (type is OneToOneType) { // only second pass if (!firstPass) { toOneRelationMetadataGenerator.AddOneToOneNotOwning(propertyAuditingData, value, currentMapper, entityName); } } else if (type is CollectionType) { // only second pass if (!firstPass) { CollectionMetadataGenerator collectionMetadataGenerator = new CollectionMetadataGenerator(this, (Mapping.Collection)value, currentMapper, entityName, xmlMappingData, propertyAuditingData); collectionMetadataGenerator.AddCollection(); } } else { if (firstPass) { // If we got here in the first pass, it means the basic mapper didn't map it, and none of the // above branches either. ThrowUnsupportedTypeException(type, entityName, propertyAuditingData.Name); } } }
public void AddToOne(XElement parent, PropertyAuditingData propertyAuditingData, IValue value, ICompositeMapperBuilder mapper, string entityName, bool insertable) { var referencedEntityName = ((ToOne)value).ReferencedEntityName; var idMapping = _mainGenerator.GetReferencedIdMappingData(entityName, referencedEntityName, propertyAuditingData, true); var lastPropertyPrefix = MappingTools.CreateToOneRelationPrefix(propertyAuditingData.Name); // Generating the id mapper for the relation var relMapper = idMapping.IdMapper.PrefixMappedProperties(lastPropertyPrefix); // Storing information about this relation _mainGenerator.EntitiesConfigurations[entityName].AddToOneRelation( propertyAuditingData.Name, 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. bool nonInsertableFake; if (!insertable && propertyAuditingData.ForceInsertable) { nonInsertableFake = true; insertable = true; } else { nonInsertableFake = false; } // Adding an element to the mapping corresponding to the references entity id's var properties = new XElement(idMapping.XmlRelationMapping); properties.Add(new XAttribute("name", propertyAuditingData.Name)); MetadataTools.PrefixNamesInPropertyElement(properties, lastPropertyPrefix, MetadataTools.GetColumnNameEnumerator(value.ColumnIterator), false, insertable); // Extracting related id properties from properties tag var firstJoin = firstJoinElement(parent); foreach (var element in properties.Elements()) { if (firstJoin == null) { parent.Add(element); } else { firstJoin.AddBeforeSelf(element); } } // Adding mapper for the id var propertyData = propertyAuditingData.GetPropertyData(); mapper.AddComposite(propertyData, new ToOneIdMapper(_mainGenerator.GlobalCfg.EnversProxyFactory, relMapper, propertyData, referencedEntityName, nonInsertableFake)); }
public void AddOneToOnePrimaryKeyJoinColumn(PropertyAuditingData propertyAuditingData, IValue value, ICompositeMapperBuilder mapper, string entityName, bool insertable) { var referencedEntityName = ((ToOne)value).ReferencedEntityName; var idMapping = _mainGenerator.GetReferencedIdMappingData(entityName, referencedEntityName, propertyAuditingData, true); var lastPropertyPrefix = MappingTools.CreateToOneRelationPrefix(propertyAuditingData.Name); // Generating the id mapper for the relation var relMapper = idMapping.IdMapper.PrefixMappedProperties(lastPropertyPrefix); // Storing information about this relation _mainGenerator.EntitiesConfigurations[entityName].AddToOneRelation(propertyAuditingData.Name, referencedEntityName, relMapper, insertable, MappingTools.IgnoreNotFound(value)); // Adding mapper for the id var propertyData = propertyAuditingData.GetPropertyData(); mapper.AddComposite(propertyData, new OneToOnePrimaryKeyJoinColumnMapper(entityName, referencedEntityName, propertyData)); }
private void addProperties(XElement parent, IEnumerable <Property> properties, ICompositeMapperBuilder currentMapper, ClassAuditingData auditingData, string entityName, EntityXmlMappingData xmlMappingData, bool firstPass) { foreach (var property in properties) { var propertyName = property.Name; var propertyAuditingData = auditingData.GetPropertyAuditingData(propertyName); if (propertyAuditingData != null) { AddValue(parent, property.Value, currentMapper, entityName, xmlMappingData, propertyAuditingData, property.IsInsertable, firstPass, true); } } }
public void AddOneToOneNotOwning(PropertyAuditingData propertyAuditingData, IValue value, ICompositeMapperBuilder mapper, string entityName) { var propertyValue = (OneToOne)value; var owningReferencePropertyName = propertyValue.ReferencedPropertyName; // mappedBy var configuration = _mainGenerator.EntitiesConfigurations[entityName]; if (configuration == null) { throw new MappingException("An audited relation to a non-audited entity " + entityName + "!"); } var ownedIdMapping = configuration.IdMappingData; if (ownedIdMapping == null) { throw new MappingException("An audited relation to a non-audited entity " + entityName + "!"); } var lastPropertyPrefix = MappingTools.CreateToOneRelationPrefix(owningReferencePropertyName); var referencedEntityName = propertyValue.ReferencedEntityName; // Generating the id mapper for the relation var ownedIdMapper = ownedIdMapping.IdMapper.PrefixMappedProperties(lastPropertyPrefix); // Storing information about this relation _mainGenerator.EntitiesConfigurations[entityName].AddToOneNotOwningRelation( propertyAuditingData.Name, owningReferencePropertyName, referencedEntityName, ownedIdMapper, MappingTools.IgnoreNotFound(value)); // Adding mapper for the id var propertyData = propertyAuditingData.GetPropertyData(); mapper.AddComposite(propertyData, new OneToOneNotOwningMapper(entityName, referencedEntityName, owningReferencePropertyName, propertyData)); }
private void AddProperties(XmlElement parent, IEnumerable<Property> properties, ICompositeMapperBuilder currentMapper, ClassAuditingData auditingData, string entityName, EntityXmlMappingData xmlMappingData, bool firstPass) { foreach (var property in properties) { var propertyName = property.Name; var propertyAuditingData = auditingData.GetPropertyAuditingData(propertyName); if (propertyAuditingData != null) { AddValue(parent, property.Value, currentMapper, entityName, xmlMappingData, propertyAuditingData, property.IsInsertable, firstPass); } } }
private void AddJoins(PersistentClass pc, ICompositeMapperBuilder currentMapper, ClassAuditingData auditingData, string entityName, EntityXmlMappingData xmlMappingData, bool firstPass) { foreach (var join in pc.JoinIterator) { var joinElement = entitiesJoins[entityName][join]; if (joinElement != null) { AddProperties(joinElement, join.PropertyIterator, currentMapper, auditingData, entityName, xmlMappingData, firstPass); } } }
public void AddValue(XmlElement parent, IValue value, ICompositeMapperBuilder currentMapper, string entityName, EntityXmlMappingData xmlMappingData, PropertyAuditingData propertyAuditingData, bool insertable, bool firstPass) { var type = value.Type; // only first pass if (firstPass) { if (BasicMetadataGenerator.AddBasic(parent, propertyAuditingData, value, currentMapper, insertable, false)) { // The property was mapped by the basic generator. return; } } if (type is ComponentType) { // both passes componentMetadataGenerator.AddComponent(parent, propertyAuditingData, value, currentMapper, entityName, xmlMappingData, firstPass, insertable); } else if (type is ManyToOneType) { // only second pass if (!firstPass) { toOneRelationMetadataGenerator.AddToOne(parent, propertyAuditingData, value, currentMapper, entityName, insertable, null); } } else if (type is OneToOneType) { // only second pass if (!firstPass) { var oneToOneType = (OneToOneType)type; var oneToOneValue = (OneToOne)value; if (oneToOneType.IsReferenceToPrimaryKey && oneToOneValue.IsConstrained) { //if pk onetoone is used, "value" has no corresponding columns var pkColumns = new List<string>(); foreach (Column column in Cfg.GetClassMapping(oneToOneValue.ReferencedEntityName).Identifier.ColumnIterator) { pkColumns.Add("Ref" + column.Name); } toOneRelationMetadataGenerator.AddToOne(parent, propertyAuditingData, value, currentMapper, entityName, insertable, pkColumns); } else { toOneRelationMetadataGenerator.AddOneToOneNotOwning(propertyAuditingData, oneToOneValue, currentMapper, entityName); } } } else if (type is CollectionType) { // only second pass if (!firstPass) { var collectionMetadataGenerator = new CollectionMetadataGenerator(this, (Mapping.Collection)value, currentMapper, entityName, xmlMappingData, propertyAuditingData); collectionMetadataGenerator.AddCollection(); } } else { if (firstPass) { // If we got here in the first pass, it means the basic mapper didn't map it, and none of the // above branches either. ThrowUnsupportedTypeException(type, entityName, propertyAuditingData.Name); } } }