Search in sources :

Example 1 with EmbeddedIdMapper

use of org.hibernate.envers.internal.entities.mapper.id.EmbeddedIdMapper in project hibernate-orm by hibernate.

the class IdMetadataGenerator method addId.

@SuppressWarnings({ "unchecked" })
IdMappingData addId(PersistentClass pc, boolean audited) {
    // Xml mapping which will be used for relations
    final Element relIdMapping = new DefaultElement("properties");
    // Xml mapping which will be used for the primary key of the versions table
    final Element origIdMapping = new DefaultElement("composite-id");
    final Property idProp = pc.getIdentifierProperty();
    final Component idMapper = pc.getIdentifierMapper();
    // Checking if the id mapping is supported
    if (idMapper == null && idProp == null) {
        return null;
    }
    SimpleIdMapperBuilder mapper;
    if (idMapper != null) {
        // Multiple id
        final Class componentClass = ReflectionTools.loadClass(((Component) pc.getIdentifier()).getComponentClassName(), mainGenerator.getClassLoaderService());
        mapper = new MultipleIdMapper(componentClass, pc.getServiceRegistry());
        if (!addIdProperties(relIdMapping, (Iterator<Property>) idMapper.getPropertyIterator(), mapper, false, audited)) {
            return null;
        }
        // null mapper - the mapping where already added the first time, now we only want to generate the xml
        if (!addIdProperties(origIdMapping, (Iterator<Property>) idMapper.getPropertyIterator(), null, true, audited)) {
            return null;
        }
    } else if (idProp.isComposite()) {
        // Embedded id
        final Component idComponent = (Component) idProp.getValue();
        final Class embeddableClass = ReflectionTools.loadClass(idComponent.getComponentClassName(), mainGenerator.getClassLoaderService());
        mapper = new EmbeddedIdMapper(getIdPropertyData(idProp), embeddableClass, pc.getServiceRegistry());
        if (!addIdProperties(relIdMapping, (Iterator<Property>) idComponent.getPropertyIterator(), mapper, false, audited)) {
            return null;
        }
        // null mapper - the mapping where already added the first time, now we only want to generate the xml
        if (!addIdProperties(origIdMapping, (Iterator<Property>) idComponent.getPropertyIterator(), null, true, audited)) {
            return null;
        }
    } else {
        // Single id
        mapper = new SingleIdMapper(pc.getServiceRegistry());
        // Last but one parameter: ids are always insertable
        mainGenerator.getBasicMetadataGenerator().addBasic(relIdMapping, getIdPersistentPropertyAuditingData(idProp), idProp.getValue(), mapper, true, false);
        // null mapper - the mapping where already added the first time, now we only want to generate the xml
        mainGenerator.getBasicMetadataGenerator().addBasic(origIdMapping, getIdPersistentPropertyAuditingData(idProp), idProp.getValue(), null, true, true);
    }
    origIdMapping.addAttribute("name", mainGenerator.getVerEntCfg().getOriginalIdPropName());
    // Adding a relation to the revision entity (effectively: the "revision number" property)
    mainGenerator.addRevisionInfoRelation(origIdMapping);
    return new IdMappingData(mapper, origIdMapping, relIdMapping);
}
Also used : SingleIdMapper(org.hibernate.envers.internal.entities.mapper.id.SingleIdMapper) DefaultElement(org.dom4j.tree.DefaultElement) SimpleIdMapperBuilder(org.hibernate.envers.internal.entities.mapper.id.SimpleIdMapperBuilder) DefaultElement(org.dom4j.tree.DefaultElement) Element(org.dom4j.Element) Iterator(java.util.Iterator) PersistentClass(org.hibernate.mapping.PersistentClass) Component(org.hibernate.mapping.Component) EmbeddedIdMapper(org.hibernate.envers.internal.entities.mapper.id.EmbeddedIdMapper) Property(org.hibernate.mapping.Property) MultipleIdMapper(org.hibernate.envers.internal.entities.mapper.id.MultipleIdMapper) IdMappingData(org.hibernate.envers.internal.entities.IdMappingData)

Aggregations

Iterator (java.util.Iterator)1 Element (org.dom4j.Element)1 DefaultElement (org.dom4j.tree.DefaultElement)1 IdMappingData (org.hibernate.envers.internal.entities.IdMappingData)1 EmbeddedIdMapper (org.hibernate.envers.internal.entities.mapper.id.EmbeddedIdMapper)1 MultipleIdMapper (org.hibernate.envers.internal.entities.mapper.id.MultipleIdMapper)1 SimpleIdMapperBuilder (org.hibernate.envers.internal.entities.mapper.id.SimpleIdMapperBuilder)1 SingleIdMapper (org.hibernate.envers.internal.entities.mapper.id.SingleIdMapper)1 Component (org.hibernate.mapping.Component)1 PersistentClass (org.hibernate.mapping.PersistentClass)1 Property (org.hibernate.mapping.Property)1