Search in sources :

Example 86 with MappingException

use of org.hibernate.MappingException in project hibernate-orm by hibernate.

the class AuditMetadataGenerator method getReferencedIdMappingData.

/**
	 * Reads the id mapping data of a referenced entity.
	 *
	 * @param entityName Name of the entity which is the source of the relation.
	 * @param referencedEntityName Name of the entity which is the target of the relation.
	 * @param propertyAuditingData Auditing data of the property that is the source of the relation.
	 * @param allowNotAuditedTarget Are not-audited target entities allowed.
	 *
	 * @return The id mapping data of the related entity.
	 *
	 * @throws MappingException If a relation from an audited to a non-audited entity is detected, which is not
	 * mapped using {@link RelationTargetAuditMode#NOT_AUDITED}.
	 */
IdMappingData getReferencedIdMappingData(String entityName, String referencedEntityName, PropertyAuditingData propertyAuditingData, boolean allowNotAuditedTarget) {
    EntityConfiguration configuration = getEntitiesConfigurations().get(referencedEntityName);
    if (configuration == null) {
        final RelationTargetAuditMode relationTargetAuditMode = propertyAuditingData.getRelationTargetAuditMode();
        configuration = getNotAuditedEntitiesConfigurations().get(referencedEntityName);
        if (configuration == null || !allowNotAuditedTarget || !RelationTargetAuditMode.NOT_AUDITED.equals(relationTargetAuditMode)) {
            throw new MappingException("An audited relation from " + entityName + "." + propertyAuditingData.getName() + " to a not audited entity " + referencedEntityName + "!" + (allowNotAuditedTarget ? " Such mapping is possible, but has to be explicitly defined using @Audited(targetAuditMode = NOT_AUDITED)." : ""));
        }
    }
    return configuration.getIdMappingData();
}
Also used : RelationTargetAuditMode(org.hibernate.envers.RelationTargetAuditMode) EntityConfiguration(org.hibernate.envers.internal.entities.EntityConfiguration) MappingException(org.hibernate.MappingException)

Example 87 with MappingException

use of org.hibernate.MappingException in project hibernate-orm by hibernate.

the class AuditMetadataGenerator method generateInheritanceMappingData.

private Triple<Element, ExtendedPropertyMapper, String> generateInheritanceMappingData(PersistentClass pc, EntityXmlMappingData xmlMappingData, AuditTableData auditTableData, String inheritanceMappingType) {
    final String extendsEntityName = verEntCfg.getAuditEntityName(pc.getSuperclass().getEntityName());
    final Element classMapping = MetadataTools.createSubclassEntity(xmlMappingData.getMainXmlMapping(), inheritanceMappingType, auditTableData, extendsEntityName, pc.getDiscriminatorValue(), pc.isAbstract());
    // The id and revision type is already mapped in the parent
    // Getting the property mapper of the parent - when mapping properties, they need to be included
    final String parentEntityName = pc.getSuperclass().getEntityName();
    final EntityConfiguration parentConfiguration = entitiesConfigurations.get(parentEntityName);
    if (parentConfiguration == null) {
        throw new MappingException("Entity '" + pc.getEntityName() + "' is audited, but its superclass: '" + parentEntityName + "' is not.");
    }
    final ExtendedPropertyMapper parentPropertyMapper = parentConfiguration.getPropertyMapper();
    final ExtendedPropertyMapper propertyMapper = new SubclassPropertyMapper(new MultiPropertyMapper(), parentPropertyMapper);
    return Triple.make(classMapping, propertyMapper, parentEntityName);
}
Also used : Element(org.dom4j.Element) EntityConfiguration(org.hibernate.envers.internal.entities.EntityConfiguration) SubclassPropertyMapper(org.hibernate.envers.internal.entities.mapper.SubclassPropertyMapper) MultiPropertyMapper(org.hibernate.envers.internal.entities.mapper.MultiPropertyMapper) ExtendedPropertyMapper(org.hibernate.envers.internal.entities.mapper.ExtendedPropertyMapper) MappingException(org.hibernate.MappingException)

Example 88 with MappingException

use of org.hibernate.MappingException in project hibernate-orm by hibernate.

the class IdMetadataGenerator method addIdProperties.

@SuppressWarnings({ "unchecked" })
private boolean addIdProperties(Element parent, Iterator<Property> properties, SimpleMapperBuilder mapper, boolean key, boolean audited) {
    while (properties.hasNext()) {
        final Property property = properties.next();
        final Type propertyType = property.getType();
        if (!"_identifierMapper".equals(property.getName())) {
            boolean added = false;
            if (propertyType instanceof ManyToOneType) {
                added = mainGenerator.getBasicMetadataGenerator().addManyToOne(parent, getIdPersistentPropertyAuditingData(property), property.getValue(), mapper);
            } else {
                // Last but one parameter: ids are always insertable
                added = mainGenerator.getBasicMetadataGenerator().addBasic(parent, getIdPersistentPropertyAuditingData(property), property.getValue(), mapper, true, key);
            }
            if (!added) {
                // target relation mode not audited.
                if (audited) {
                    throw new MappingException("Type not supported: " + propertyType.getClass().getName());
                } else {
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : ManyToOneType(org.hibernate.type.ManyToOneType) Type(org.hibernate.type.Type) ManyToOneType(org.hibernate.type.ManyToOneType) Property(org.hibernate.mapping.Property) MappingException(org.hibernate.MappingException)

Example 89 with MappingException

use of org.hibernate.MappingException in project hibernate-orm by hibernate.

the class ToOneRelationMetadataGenerator method addOneToOneNotOwning.

@SuppressWarnings({ "unchecked" })
void addOneToOneNotOwning(PropertyAuditingData propertyAuditingData, Value value, CompositeMapperBuilder mapper, String entityName) {
    final OneToOne propertyValue = (OneToOne) value;
    final String owningReferencePropertyName = propertyValue.getReferencedPropertyName();
    final EntityConfiguration configuration = mainGenerator.getEntitiesConfigurations().get(entityName);
    if (configuration == null) {
        throw new MappingException("An audited relation to a non-audited entity " + entityName + "!");
    }
    final IdMappingData ownedIdMapping = configuration.getIdMappingData();
    if (ownedIdMapping == null) {
        throw new MappingException("An audited relation to a non-audited entity " + entityName + "!");
    }
    final String lastPropertyPrefix = MappingTools.createToOneRelationPrefix(owningReferencePropertyName);
    final String referencedEntityName = propertyValue.getReferencedEntityName();
    // Generating the id mapper for the relation
    final IdMapper ownedIdMapper = ownedIdMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix);
    // Storing information about this relation
    mainGenerator.getEntitiesConfigurations().get(entityName).addToOneNotOwningRelation(propertyAuditingData.getName(), owningReferencePropertyName, referencedEntityName, ownedIdMapper, MappingTools.ignoreNotFound(value));
    // Adding mapper for the id
    final PropertyData propertyData = propertyAuditingData.getPropertyData();
    mapper.addComposite(propertyData, new OneToOneNotOwningMapper(entityName, referencedEntityName, owningReferencePropertyName, propertyData, mainGenerator.getServiceRegistry()));
}
Also used : OneToOne(org.hibernate.mapping.OneToOne) PropertyData(org.hibernate.envers.internal.entities.PropertyData) OneToOneNotOwningMapper(org.hibernate.envers.internal.entities.mapper.relation.OneToOneNotOwningMapper) EntityConfiguration(org.hibernate.envers.internal.entities.EntityConfiguration) IdMapper(org.hibernate.envers.internal.entities.mapper.id.IdMapper) ToOneIdMapper(org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper) MappingException(org.hibernate.MappingException) IdMappingData(org.hibernate.envers.internal.entities.IdMappingData)

Example 90 with MappingException

use of org.hibernate.MappingException in project hibernate-orm by hibernate.

the class AnnotationsMetadataReader method getAuditData.

public ClassAuditingData getAuditData() {
    if (pc.getClassName() == null) {
        return auditData;
    }
    try {
        final XClass xclass = reflectionManager.classForName(pc.getClassName());
        final ModificationStore defaultStore = getDefaultAudited(xclass);
        if (defaultStore != null) {
            auditData.setDefaultAudited(true);
        }
        new AuditedPropertiesReader(defaultStore, new PersistentClassPropertiesSource(xclass), auditData, globalCfg, reflectionManager, "").read();
        addAuditTable(xclass);
        addAuditSecondaryTables(xclass);
    } catch (ClassLoadingException e) {
        throw new MappingException(e);
    }
    return auditData;
}
Also used : ClassLoadingException(org.hibernate.annotations.common.reflection.ClassLoadingException) ModificationStore(org.hibernate.envers.ModificationStore) XClass(org.hibernate.annotations.common.reflection.XClass) MappingException(org.hibernate.MappingException)

Aggregations

MappingException (org.hibernate.MappingException)94 PersistentClass (org.hibernate.mapping.PersistentClass)17 HibernateException (org.hibernate.HibernateException)12 Iterator (java.util.Iterator)11 Test (org.junit.Test)11 AnnotationException (org.hibernate.AnnotationException)10 QueryException (org.hibernate.QueryException)10 Type (org.hibernate.type.Type)10 Property (org.hibernate.mapping.Property)9 HashMap (java.util.HashMap)8 XClass (org.hibernate.annotations.common.reflection.XClass)8 DuplicateMappingException (org.hibernate.DuplicateMappingException)6 Configuration (org.hibernate.cfg.Configuration)6 UnknownSqlResultSetMappingException (org.hibernate.procedure.UnknownSqlResultSetMappingException)6 ServiceRegistry (org.hibernate.service.ServiceRegistry)6 Map (java.util.Map)5 AssociationType (org.hibernate.type.AssociationType)5 HashSet (java.util.HashSet)4 ClassLoadingException (org.hibernate.annotations.common.reflection.ClassLoadingException)4 MetadataSources (org.hibernate.boot.MetadataSources)4