Search in sources :

Example 11 with ComponentType

use of org.hibernate.type.ComponentType in project jbosstools-hibernate by jbosstools.

the class TypeFacadeTest method testIsComponentType.

@Test
public void testIsComponentType() {
    IType typeFacade = null;
    ClassType classType = new ClassType();
    typeFacade = FACADE_FACTORY.createType(classType);
    Assert.assertFalse(typeFacade.isComponentType());
    MetadataBuildingOptions mdbo = new MetadataBuilderImpl.MetadataBuildingOptionsImpl(new StandardServiceRegistryBuilder().build());
    MetadataImplementor mdi = (MetadataImplementor) new MetadataBuilderImpl(new MetadataSources()).build();
    ComponentType componentType = new ComponentType(null, new ComponentMetamodel(new Component(mdi, new RootClass(null)), mdbo));
    typeFacade = FACADE_FACTORY.createType(componentType);
    Assert.assertTrue(typeFacade.isComponentType());
}
Also used : RootClass(org.hibernate.mapping.RootClass) ComponentType(org.hibernate.type.ComponentType) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) ComponentMetamodel(org.hibernate.tuple.component.ComponentMetamodel) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) ClassType(org.hibernate.type.ClassType) Component(org.hibernate.mapping.Component) MetadataBuilderImpl(org.hibernate.boot.internal.MetadataBuilderImpl) IType(org.jboss.tools.hibernate.runtime.spi.IType) MetadataBuildingOptions(org.hibernate.boot.spi.MetadataBuildingOptions) Test(org.junit.Test)

Example 12 with ComponentType

use of org.hibernate.type.ComponentType in project hibernate-orm by hibernate.

the class CollectionMetadataGenerator method addValueToMiddleTable.

/**
 * @param value Value, which should be mapped to the middle-table, either as a relation to another entity,
 * or as a simple value.
 * @param xmlMapping If not <code>null</code>, xml mapping for this value is added to this element.
 * @param queryGeneratorBuilder In case <code>value</code> is a relation to another entity, information about it
 * should be added to the given.
 * @param prefix Prefix for proeprty names of related entities identifiers.
 * @param joinColumns Names of columns to use in the xml mapping, if this array isn't null and has any elements.
 *
 * @return Data for mapping this component.
 */
@SuppressWarnings({ "unchecked" })
private MiddleComponentData addValueToMiddleTable(Value value, Element xmlMapping, QueryGeneratorBuilder queryGeneratorBuilder, String prefix, JoinColumn[] joinColumns, boolean key) {
    final Type type = value.getType();
    if (type instanceof ManyToOneType) {
        final String prefixRelated = prefix + "_";
        final String referencedEntityName = MappingTools.getReferencedEntityName(value);
        final IdMappingData referencedIdMapping = mainGenerator.getReferencedIdMappingData(referencingEntityName, referencedEntityName, propertyAuditingData, true);
        // relation isn't inverse (so when <code>xmlMapping</code> is not null).
        if (xmlMapping != null) {
            addRelatedToXmlMapping(xmlMapping, prefixRelated, joinColumns != null && joinColumns.length > 0 ? MetadataTools.getColumnNameIterator(joinColumns) : MetadataTools.getColumnNameIterator(value.getColumnIterator()), referencedIdMapping);
        }
        // Storing the id data of the referenced entity: original mapper, prefixed mapper and entity name.
        final MiddleIdData referencedIdData = createMiddleIdData(referencedIdMapping, prefixRelated, referencedEntityName);
        // And adding it to the generator builder.
        queryGeneratorBuilder.addRelation(referencedIdData);
        return new MiddleComponentData(new MiddleRelatedComponentMapper(referencedIdData), queryGeneratorBuilder.getCurrentIndex());
    } else if (type instanceof ComponentType) {
        // Collection of embeddable elements.
        final Component component = (Component) value;
        final Class componentClass = ReflectionTools.loadClass(component.getComponentClassName(), mainGenerator.getClassLoaderService());
        final MiddleEmbeddableComponentMapper componentMapper = new MiddleEmbeddableComponentMapper(new MultiPropertyMapper(), componentClass);
        final Element parentXmlMapping = xmlMapping.getParent();
        final ComponentAuditingData auditData = new ComponentAuditingData();
        final ReflectionManager reflectionManager = mainGenerator.getMetadata().getMetadataBuildingOptions().getReflectionManager();
        new ComponentAuditedPropertiesReader(ModificationStore.FULL, new AuditedPropertiesReader.ComponentPropertiesSource(reflectionManager, component), auditData, mainGenerator.getGlobalCfg(), reflectionManager, "").read();
        // Emulating first pass.
        for (String auditedPropertyName : auditData.getPropertyNames()) {
            final PropertyAuditingData nestedAuditingData = auditData.getPropertyAuditingData(auditedPropertyName);
            mainGenerator.addValue(parentXmlMapping, component.getProperty(auditedPropertyName).getValue(), componentMapper, prefix, xmlMappingData, nestedAuditingData, true, true, true);
        }
        // Emulating second pass so that the relations can be mapped too.
        for (String auditedPropertyName : auditData.getPropertyNames()) {
            final PropertyAuditingData nestedAuditingData = auditData.getPropertyAuditingData(auditedPropertyName);
            mainGenerator.addValue(parentXmlMapping, component.getProperty(auditedPropertyName).getValue(), componentMapper, referencingEntityName, xmlMappingData, nestedAuditingData, true, false, true);
        }
        // Embeddable properties may contain null values, so cannot be stored within composite primary key.
        if (propertyValue.isSet()) {
            final String setOrdinalPropertyName = mainGenerator.getVerEntCfg().getEmbeddableSetOrdinalPropertyName();
            final Element ordinalProperty = MetadataTools.addProperty(xmlMapping, setOrdinalPropertyName, "integer", true, true);
            MetadataTools.addColumn(ordinalProperty, setOrdinalPropertyName, null, null, null, null, null, null, false);
        }
        return new MiddleComponentData(componentMapper, 0);
    } else {
        // Last but one parameter: collection components are always insertable
        final boolean mapped = mainGenerator.getBasicMetadataGenerator().addBasic(key ? xmlMapping : xmlMapping.getParent(), new PropertyAuditingData(prefix, "field", ModificationStore.FULL, RelationTargetAuditMode.AUDITED, null, null, false), value, null, true, key);
        if (mapped && key) {
            // Simple values are always stored in the first item of the array returned by the query generator.
            return new MiddleComponentData(new MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), prefix), 0);
        } else if (mapped && !key) {
            // when mapped but not part of the key, its stored as a dummy mapper??
            return new MiddleComponentData(new MiddleMapElementNotKeyComponentMapper(mainGenerator.getVerEntCfg(), prefix), 0);
        } else {
            mainGenerator.throwUnsupportedTypeException(type, referencingEntityName, propertyName);
            // Impossible to get here.
            throw new AssertionError();
        }
    }
}
Also used : AuditedPropertiesReader(org.hibernate.envers.configuration.internal.metadata.reader.AuditedPropertiesReader) ComponentAuditedPropertiesReader(org.hibernate.envers.configuration.internal.metadata.reader.ComponentAuditedPropertiesReader) ComponentType(org.hibernate.type.ComponentType) ManyToOneType(org.hibernate.type.ManyToOneType) ReflectionManager(org.hibernate.annotations.common.reflection.ReflectionManager) MiddleSimpleComponentMapper(org.hibernate.envers.internal.entities.mapper.relation.component.MiddleSimpleComponentMapper) MiddleRelatedComponentMapper(org.hibernate.envers.internal.entities.mapper.relation.component.MiddleRelatedComponentMapper) Element(org.dom4j.Element) MultiPropertyMapper(org.hibernate.envers.internal.entities.mapper.MultiPropertyMapper) PropertyAuditingData(org.hibernate.envers.configuration.internal.metadata.reader.PropertyAuditingData) MiddleMapElementNotKeyComponentMapper(org.hibernate.envers.internal.entities.mapper.relation.component.MiddleMapElementNotKeyComponentMapper) ListType(org.hibernate.type.ListType) ManyToOneType(org.hibernate.type.ManyToOneType) MaterializedNClobType(org.hibernate.type.MaterializedNClobType) SetType(org.hibernate.type.SetType) MaterializedClobType(org.hibernate.type.MaterializedClobType) MapType(org.hibernate.type.MapType) ComponentType(org.hibernate.type.ComponentType) SortedSetType(org.hibernate.type.SortedSetType) SortedMapType(org.hibernate.type.SortedMapType) BagType(org.hibernate.type.BagType) Type(org.hibernate.type.Type) ComponentAuditingData(org.hibernate.envers.configuration.internal.metadata.reader.ComponentAuditingData) MiddleIdData(org.hibernate.envers.internal.entities.mapper.relation.MiddleIdData) ComponentAuditedPropertiesReader(org.hibernate.envers.configuration.internal.metadata.reader.ComponentAuditedPropertiesReader) MiddleEmbeddableComponentMapper(org.hibernate.envers.internal.entities.mapper.relation.component.MiddleEmbeddableComponentMapper) PersistentClass(org.hibernate.mapping.PersistentClass) MiddleComponentData(org.hibernate.envers.internal.entities.mapper.relation.MiddleComponentData) Component(org.hibernate.mapping.Component) IdMappingData(org.hibernate.envers.internal.entities.IdMappingData)

Example 13 with ComponentType

use of org.hibernate.type.ComponentType in project ysoserial by frohoff.

the class Hibernate1 method makeCaller.

static Object makeCaller(Object tpl, Object getters) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchFieldException, Exception, ClassNotFoundException {
    PojoComponentTuplizer tup = Reflections.createWithoutConstructor(PojoComponentTuplizer.class);
    Reflections.getField(AbstractComponentTuplizer.class, "getters").set(tup, getters);
    ComponentType t = Reflections.createWithConstructor(ComponentType.class, AbstractType.class, new Class[0], new Object[0]);
    Reflections.setFieldValue(t, "componentTuplizer", tup);
    Reflections.setFieldValue(t, "propertySpan", 1);
    Reflections.setFieldValue(t, "propertyTypes", new Type[] { t });
    TypedValue v1 = new TypedValue(t, null);
    Reflections.setFieldValue(v1, "value", tpl);
    Reflections.setFieldValue(v1, "type", t);
    TypedValue v2 = new TypedValue(t, null);
    Reflections.setFieldValue(v2, "value", tpl);
    Reflections.setFieldValue(v2, "type", t);
    return Gadgets.makeMap(v1, v2);
}
Also used : ComponentType(org.hibernate.type.ComponentType) AbstractComponentTuplizer(org.hibernate.tuple.component.AbstractComponentTuplizer) PojoComponentTuplizer(org.hibernate.tuple.component.PojoComponentTuplizer) TypedValue(org.hibernate.engine.spi.TypedValue)

Example 14 with ComponentType

use of org.hibernate.type.ComponentType in project midpoint by Evolveum.

the class MidpointPersisterUtil method killUnwantedAssociationValues.

private static void killUnwantedAssociationValues(String[] propertyNames, Type[] propertyTypes, Object[] values, int depth) {
    if (values == null) {
        return;
    }
    for (int i = 0; i < propertyTypes.length; i++) {
        String name = propertyNames[i];
        Type type = propertyTypes[i];
        Object value = values[i];
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("{}- killUnwantedAssociationValues processing #{}: {} (type={}, value={})", StringUtils.repeat("  ", depth), i, name, type, value);
        }
        if (type instanceof ComponentType) {
            ComponentType componentType = (ComponentType) type;
            killUnwantedAssociationValues(componentType.getPropertyNames(), componentType.getSubtypes(), (Object[]) value, depth + 1);
        } else if (type instanceof ManyToOneType) {
            if (ASSOCIATION_TO_REMOVE.equals(name)) {
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("{}- killUnwantedAssociationValues KILLED #{}: {} (type={}, value={})", StringUtils.repeat("  ", depth), i, name, type, value);
                }
                values[i] = null;
            }
        }
    }
}
Also used : ManyToOneType(org.hibernate.type.ManyToOneType) ComponentType(org.hibernate.type.ComponentType) Type(org.hibernate.type.Type) ComponentType(org.hibernate.type.ComponentType) ManyToOneType(org.hibernate.type.ManyToOneType)

Example 15 with ComponentType

use of org.hibernate.type.ComponentType in project hibernate-orm by hibernate.

the class ASTParserLoadingTest method testComponentQueries.

@Test
public void testComponentQueries() {
    Session s = openSession();
    s.beginTransaction();
    Type[] types = s.createQuery("select h.name from Human h").getReturnTypes();
    assertEquals(1, types.length);
    assertTrue(types[0] instanceof ComponentType);
    // Test the ability to perform comparisons between component values
    s.createQuery("from Human h where h.name = h.name").list();
    s.createQuery("from Human h where h.name = :name").setParameter("name", new Name()).list();
    s.createQuery("from Human where name = :name").setParameter("name", new Name()).list();
    s.createQuery("from Human h where :name = h.name").setParameter("name", new Name()).list();
    s.createQuery("from Human h where :name <> h.name").setParameter("name", new Name()).list();
    // Test the ability to perform comparisons between a component and an explicit row-value
    s.createQuery("from Human h where h.name = ('John', 'X', 'Doe')").list();
    s.createQuery("from Human h where ('John', 'X', 'Doe') = h.name").list();
    s.createQuery("from Human h where ('John', 'X', 'Doe') <> h.name").list();
    s.createQuery("from Human h order by h.name").list();
    s.getTransaction().commit();
    s.close();
}
Also used : DiscriminatorType(org.hibernate.persister.entity.DiscriminatorType) ManyToOneType(org.hibernate.type.ManyToOneType) ComponentType(org.hibernate.type.ComponentType) Type(org.hibernate.type.Type) ComponentType(org.hibernate.type.ComponentType) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

ComponentType (org.hibernate.type.ComponentType)22 Component (org.hibernate.mapping.Component)9 Type (org.hibernate.type.Type)9 Test (org.junit.Test)9 RootClass (org.hibernate.mapping.RootClass)8 ComponentMetamodel (org.hibernate.tuple.component.ComponentMetamodel)8 ClassType (org.hibernate.type.ClassType)8 IType (org.jboss.tools.hibernate.runtime.spi.IType)8 ManyToOneType (org.hibernate.type.ManyToOneType)5 MetadataSources (org.hibernate.boot.MetadataSources)4 MetadataBuilderImpl (org.hibernate.boot.internal.MetadataBuilderImpl)4 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)4 MetadataBuildingOptions (org.hibernate.boot.spi.MetadataBuildingOptions)4 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)4 CollectionType (org.hibernate.type.CollectionType)3 ArrayList (java.util.ArrayList)2 Calendar (java.util.Calendar)2 Date (java.util.Date)2 Iterator (java.util.Iterator)2 OneToOneType (org.hibernate.type.OneToOneType)2