Search in sources :

Example 11 with Value

use of org.hibernate.mapping.Value in project hibernate-orm by hibernate.

the class FieldAccessedNestedEmbeddableMetadataTest method testEnumTypeInterpretation.

@Test
@FailureExpected(jiraKey = "HHH-9089")
public void testEnumTypeInterpretation() {
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    try {
        final Metadata metadata = new MetadataSources(ssr).addAnnotatedClass(Customer.class).buildMetadata();
        PersistentClass classMetadata = metadata.getEntityBinding(Customer.class.getName());
        Property investmentsProperty = classMetadata.getProperty("investments");
        Collection investmentsValue = (Collection) investmentsProperty.getValue();
        Component investmentMetadata = (Component) investmentsValue.getElement();
        Value descriptionValue = investmentMetadata.getProperty("description").getValue();
        assertEquals(1, descriptionValue.getColumnSpan());
        Column selectable = (Column) descriptionValue.getColumnIterator().next();
        assertEquals(500, selectable.getLength());
        Component amountMetadata = (Component) investmentMetadata.getProperty("amount").getValue();
        SimpleValue currencyMetadata = (SimpleValue) amountMetadata.getProperty("currency").getValue();
        CustomType currencyType = (CustomType) currencyMetadata.getType();
        int[] currencySqlTypes = currencyType.sqlTypes(metadata);
        assertEquals(1, currencySqlTypes.length);
        assertJdbcTypeCode(Types.VARCHAR, currencySqlTypes[0]);
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : CustomType(org.hibernate.type.CustomType) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) SimpleValue(org.hibernate.mapping.SimpleValue) Column(org.hibernate.mapping.Column) SimpleValue(org.hibernate.mapping.SimpleValue) Value(org.hibernate.mapping.Value) Collection(org.hibernate.mapping.Collection) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test) FailureExpected(org.hibernate.testing.FailureExpected)

Example 12 with Value

use of org.hibernate.mapping.Value in project hibernate-orm by hibernate.

the class ResultsetMappingSecondPass method getSubPropertyIterator.

private Iterator getSubPropertyIterator(PersistentClass pc, String reducedName) {
    Value value = pc.getRecursiveProperty(reducedName).getValue();
    Iterator parentPropIter;
    if (value instanceof Component) {
        Component comp = (Component) value;
        parentPropIter = comp.getPropertyIterator();
    } else if (value instanceof ToOne) {
        ToOne toOne = (ToOne) value;
        PersistentClass referencedPc = context.getMetadataCollector().getEntityBinding(toOne.getReferencedEntityName());
        if (toOne.getReferencedPropertyName() != null) {
            try {
                parentPropIter = ((Component) referencedPc.getRecursiveProperty(toOne.getReferencedPropertyName()).getValue()).getPropertyIterator();
            } catch (ClassCastException e) {
                throw new MappingException("dotted notation reference neither a component nor a many/one to one", e);
            }
        } else {
            try {
                if (referencedPc.getIdentifierMapper() == null) {
                    parentPropIter = ((Component) referencedPc.getIdentifierProperty().getValue()).getPropertyIterator();
                } else {
                    parentPropIter = referencedPc.getIdentifierMapper().getPropertyIterator();
                }
            } catch (ClassCastException e) {
                throw new MappingException("dotted notation reference neither a component nor a many/one to one", e);
            }
        }
    } else {
        throw new MappingException("dotted notation reference neither a component nor a many/one to one");
    }
    return parentPropIter;
}
Also used : Value(org.hibernate.mapping.Value) Iterator(java.util.Iterator) ToOne(org.hibernate.mapping.ToOne) Component(org.hibernate.mapping.Component) PersistentClass(org.hibernate.mapping.PersistentClass) MappingException(org.hibernate.MappingException)

Example 13 with Value

use of org.hibernate.mapping.Value in project hibernate-orm by hibernate.

the class CollectionMetadataGenerator method addOneToManyAttached.

@SuppressWarnings({ "unchecked" })
private void addOneToManyAttached(boolean fakeOneToManyBidirectional) {
    LOG.debugf("Adding audit mapping for property %s.%s: one-to-many collection, using a join column on the referenced entity", referencingEntityName, propertyName);
    // check whether the property has an @IndexColumn or @OrderColumn because its part of an
    // IndexedCollection mapping type.
    final boolean indexed = (propertyValue instanceof IndexedCollection) && ((IndexedCollection) propertyValue).getIndex() != null;
    final String mappedBy = getMappedBy(propertyValue);
    final IdMappingData referencedIdMapping = mainGenerator.getReferencedIdMappingData(referencingEntityName, referencedEntityName, propertyAuditingData, false);
    final IdMappingData referencingIdMapping = referencingEntityConfiguration.getIdMappingData();
    // Generating the id mappers data for the referencing side of the relation.
    final MiddleIdData referencingIdData = createMiddleIdData(referencingIdMapping, mappedBy + "_", referencingEntityName);
    // And for the referenced side. The prefixed mapper won't be used (as this collection isn't persisted
    // in a join table, so the prefix value is arbitrary).
    final MiddleIdData referencedIdData = createMiddleIdData(referencedIdMapping, null, referencedEntityName);
    // Generating the element mapping.
    final MiddleComponentData elementComponentData = new MiddleComponentData(new MiddleRelatedComponentMapper(referencedIdData), 0);
    // Generating the index mapping, if an index exists. It can only exists in case a javax.persistence.MapKey
    // annotation is present on the entity. So the middleEntityXml will be not be used. The queryGeneratorBuilder
    // will only be checked for nullnes.
    MiddleComponentData indexComponentData = addIndex(null, null);
    // Generating the query generator - it should read directly from the related entity.
    final RelationQueryGenerator queryGenerator = new OneAuditEntityQueryGenerator(mainGenerator.getGlobalCfg(), mainGenerator.getVerEntCfg(), mainGenerator.getAuditStrategy(), referencingIdData, referencedEntityName, referencedIdData, isEmbeddableElementType(), mappedBy, isMappedByKey(propertyValue, mappedBy));
    // Creating common mapper data.
    final CommonCollectionMapperData commonCollectionMapperData = new CommonCollectionMapperData(mainGenerator.getVerEntCfg(), referencedEntityName, propertyAuditingData.getPropertyData(), referencingIdData, queryGenerator);
    PropertyMapper fakeBidirectionalRelationMapper;
    PropertyMapper fakeBidirectionalRelationIndexMapper;
    if (fakeOneToManyBidirectional || indexed) {
        // In case of a fake many-to-one bidirectional relation, we have to generate a mapper which maps
        // the mapped-by property name to the id of the related entity (which is the owner of the collection).
        final String auditMappedBy;
        if (fakeOneToManyBidirectional) {
            auditMappedBy = propertyAuditingData.getAuditMappedBy();
        } else {
            auditMappedBy = propertyValue.getMappedByProperty();
        }
        // Creating a prefixed relation mapper.
        final IdMapper relMapper = referencingIdMapping.getIdMapper().prefixMappedProperties(MappingTools.createToOneRelationPrefix(auditMappedBy));
        fakeBidirectionalRelationMapper = new ToOneIdMapper(relMapper, // when constructing the PropertyData.
        new PropertyData(auditMappedBy, null, null, null), referencingEntityName, false);
        final String positionMappedBy;
        if (fakeOneToManyBidirectional) {
            positionMappedBy = propertyAuditingData.getPositionMappedBy();
        } else if (indexed) {
            final Value indexValue = ((IndexedCollection) propertyValue).getIndex();
            positionMappedBy = indexValue.getColumnIterator().next().getText();
        } else {
            positionMappedBy = null;
        }
        // Checking if there's an index defined. If so, adding a mapper for it.
        if (positionMappedBy != null) {
            fakeBidirectionalRelationIndexMapper = new SinglePropertyMapper(new PropertyData(positionMappedBy, null, null, null));
            // Also, overwriting the index component data to properly read the index.
            indexComponentData = new MiddleComponentData(new MiddleStraightComponentMapper(positionMappedBy), 0);
        } else {
            fakeBidirectionalRelationIndexMapper = null;
        }
    } else {
        fakeBidirectionalRelationMapper = null;
        fakeBidirectionalRelationIndexMapper = null;
    }
    // Checking the type of the collection and adding an appropriate mapper.
    addMapper(commonCollectionMapperData, elementComponentData, indexComponentData);
    // Storing information about this relation.
    referencingEntityConfiguration.addToManyNotOwningRelation(propertyName, mappedBy, referencedEntityName, referencingIdData.getPrefixedMapper(), fakeBidirectionalRelationMapper, fakeBidirectionalRelationIndexMapper, indexed);
}
Also used : PropertyData(org.hibernate.envers.internal.entities.PropertyData) CommonCollectionMapperData(org.hibernate.envers.internal.entities.mapper.relation.CommonCollectionMapperData) MiddleRelatedComponentMapper(org.hibernate.envers.internal.entities.mapper.relation.component.MiddleRelatedComponentMapper) IdMapper(org.hibernate.envers.internal.entities.mapper.id.IdMapper) ToOneIdMapper(org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper) SinglePropertyMapper(org.hibernate.envers.internal.entities.mapper.SinglePropertyMapper) MultiPropertyMapper(org.hibernate.envers.internal.entities.mapper.MultiPropertyMapper) SinglePropertyMapper(org.hibernate.envers.internal.entities.mapper.SinglePropertyMapper) PropertyMapper(org.hibernate.envers.internal.entities.mapper.PropertyMapper) MiddleIdData(org.hibernate.envers.internal.entities.mapper.relation.MiddleIdData) RelationQueryGenerator(org.hibernate.envers.internal.entities.mapper.relation.query.RelationQueryGenerator) Value(org.hibernate.mapping.Value) MiddleComponentData(org.hibernate.envers.internal.entities.mapper.relation.MiddleComponentData) ToOneIdMapper(org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper) OneAuditEntityQueryGenerator(org.hibernate.envers.internal.entities.mapper.relation.query.OneAuditEntityQueryGenerator) IndexedCollection(org.hibernate.mapping.IndexedCollection) MiddleStraightComponentMapper(org.hibernate.envers.internal.entities.mapper.relation.component.MiddleStraightComponentMapper) IdMappingData(org.hibernate.envers.internal.entities.IdMappingData)

Aggregations

Value (org.hibernate.mapping.Value)13 Component (org.hibernate.mapping.Component)8 Property (org.hibernate.mapping.Property)7 PersistentClass (org.hibernate.mapping.PersistentClass)6 SimpleValue (org.hibernate.mapping.SimpleValue)5 Iterator (java.util.Iterator)4 Collection (org.hibernate.mapping.Collection)4 Column (org.hibernate.mapping.Column)4 ToOne (org.hibernate.mapping.ToOne)4 AnnotationException (org.hibernate.AnnotationException)3 AssertionFailure (org.hibernate.AssertionFailure)3 Ejb3JoinColumn (org.hibernate.cfg.Ejb3JoinColumn)3 DependantValue (org.hibernate.mapping.DependantValue)3 ManyToOne (org.hibernate.mapping.ManyToOne)3 OneToMany (org.hibernate.mapping.OneToMany)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 XProperty (org.hibernate.annotations.common.reflection.XProperty)2 Metadata (org.hibernate.boot.Metadata)2 MetadataSources (org.hibernate.boot.MetadataSources)2