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