Search in sources :

Example 1 with ToOne

use of org.hibernate.mapping.ToOne 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));
}
Also used : PropertyData(org.hibernate.envers.internal.entities.PropertyData) Element(org.dom4j.Element) OneToOne(org.hibernate.mapping.OneToOne) ToOne(org.hibernate.mapping.ToOne) IdMapper(org.hibernate.envers.internal.entities.mapper.id.IdMapper) ToOneIdMapper(org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper) ToOneIdMapper(org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper) IdMappingData(org.hibernate.envers.internal.entities.IdMappingData)

Example 2 with ToOne

use of org.hibernate.mapping.ToOne in project jbosstools-hibernate by jbosstools.

the class ValueFacadeTest method testIsToOne.

@Test
public void testIsToOne() {
    valueTarget = new SimpleValue();
    valueFacade = FACADE_FACTORY.createValue(valueTarget);
    Assert.assertFalse(valueFacade.isToOne());
    ToOne toOne = new OneToOne(null, new RootClass());
    valueFacade = FACADE_FACTORY.createValue(toOne);
    Assert.assertTrue(valueFacade.isToOne());
}
Also used : RootClass(org.hibernate.mapping.RootClass) OneToOne(org.hibernate.mapping.OneToOne) ToOne(org.hibernate.mapping.ToOne) ManyToOne(org.hibernate.mapping.ManyToOne) OneToOne(org.hibernate.mapping.OneToOne) SimpleValue(org.hibernate.mapping.SimpleValue) Test(org.junit.Test)

Example 3 with ToOne

use of org.hibernate.mapping.ToOne in project jbosstools-hibernate by jbosstools.

the class ValueFacadeTest method testIsToOne.

@Test
public void testIsToOne() {
    valueTarget = new SimpleValue(null);
    valueFacade = FACADE_FACTORY.createValue(valueTarget);
    Assert.assertFalse(valueFacade.isToOne());
    ToOne toOne = new OneToOne(null, null, new RootClass());
    valueFacade = FACADE_FACTORY.createValue(toOne);
    Assert.assertTrue(valueFacade.isToOne());
}
Also used : RootClass(org.hibernate.mapping.RootClass) OneToOne(org.hibernate.mapping.OneToOne) ToOne(org.hibernate.mapping.ToOne) ManyToOne(org.hibernate.mapping.ManyToOne) OneToOne(org.hibernate.mapping.OneToOne) SimpleValue(org.hibernate.mapping.SimpleValue) Test(org.junit.Test)

Example 4 with ToOne

use of org.hibernate.mapping.ToOne in project jbosstools-hibernate by jbosstools.

the class ValueFacadeTest method testIsToOne.

@Test
public void testIsToOne() {
    valueTarget = new SimpleValue(null);
    valueFacade = FACADE_FACTORY.createValue(valueTarget);
    Assert.assertFalse(valueFacade.isToOne());
    ToOne toOne = new OneToOne(null, null, new RootClass());
    valueFacade = FACADE_FACTORY.createValue(toOne);
    Assert.assertTrue(valueFacade.isToOne());
}
Also used : RootClass(org.hibernate.mapping.RootClass) OneToOne(org.hibernate.mapping.OneToOne) ToOne(org.hibernate.mapping.ToOne) ManyToOne(org.hibernate.mapping.ManyToOne) OneToOne(org.hibernate.mapping.OneToOne) SimpleValue(org.hibernate.mapping.SimpleValue) Test(org.junit.Test)

Example 5 with ToOne

use of org.hibernate.mapping.ToOne in project jbosstools-hibernate by jbosstools.

the class ValueFacadeTest method testIsToOne.

@Test
public void testIsToOne() {
    valueTarget = new SimpleValue(null);
    valueFacade = FACADE_FACTORY.createValue(valueTarget);
    Assert.assertFalse(valueFacade.isToOne());
    ToOne toOne = new OneToOne(null, null, new RootClass(null));
    valueFacade = FACADE_FACTORY.createValue(toOne);
    Assert.assertTrue(valueFacade.isToOne());
}
Also used : RootClass(org.hibernate.mapping.RootClass) OneToOne(org.hibernate.mapping.OneToOne) ToOne(org.hibernate.mapping.ToOne) ManyToOne(org.hibernate.mapping.ManyToOne) OneToOne(org.hibernate.mapping.OneToOne) SimpleValue(org.hibernate.mapping.SimpleValue) Test(org.junit.Test)

Aggregations

ToOne (org.hibernate.mapping.ToOne)16 OneToOne (org.hibernate.mapping.OneToOne)10 SimpleValue (org.hibernate.mapping.SimpleValue)10 ManyToOne (org.hibernate.mapping.ManyToOne)9 RootClass (org.hibernate.mapping.RootClass)8 Test (org.junit.Test)8 Iterator (java.util.Iterator)5 Property (org.hibernate.mapping.Property)5 Component (org.hibernate.mapping.Component)4 PersistentClass (org.hibernate.mapping.PersistentClass)4 Value (org.hibernate.mapping.Value)4 AnnotationException (org.hibernate.AnnotationException)3 AssertionFailure (org.hibernate.AssertionFailure)3 IdMapper (org.hibernate.envers.internal.entities.mapper.id.IdMapper)3 XProperty (org.hibernate.annotations.common.reflection.XProperty)2 Ejb3JoinColumn (org.hibernate.cfg.Ejb3JoinColumn)2 IdMappingData (org.hibernate.envers.internal.entities.IdMappingData)2 PropertyData (org.hibernate.envers.internal.entities.PropertyData)2 ToOneIdMapper (org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper)2 Collection (org.hibernate.mapping.Collection)2