Search in sources :

Example 1 with CompositeType

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

the class Example method addComponentTypedValues.

protected void addComponentTypedValues(String path, Object component, CompositeType type, List<TypedValue> list, Criteria criteria, CriteriaQuery criteriaQuery) {
    if (component != null) {
        final String[] propertyNames = type.getPropertyNames();
        final Type[] subtypes = type.getSubtypes();
        final Object[] values = type.getPropertyValues(component, getEntityMode(criteria, criteriaQuery));
        for (int i = 0; i < propertyNames.length; i++) {
            final Object value = values[i];
            final Type subtype = subtypes[i];
            final String subpath = StringHelper.qualify(path, propertyNames[i]);
            if (isPropertyIncluded(value, subpath, subtype)) {
                if (subtype.isComponentType()) {
                    addComponentTypedValues(subpath, value, (CompositeType) subtype, list, criteria, criteriaQuery);
                } else {
                    addPropertyTypedValue(value, subtype, list);
                }
            }
        }
    }
}
Also used : CompositeType(org.hibernate.type.CompositeType) Type(org.hibernate.type.Type)

Example 2 with CompositeType

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

the class Example method appendComponentCondition.

protected void appendComponentCondition(String path, Object component, CompositeType type, Criteria criteria, CriteriaQuery criteriaQuery, StringBuilder buf) {
    if (component != null) {
        final String[] propertyNames = type.getPropertyNames();
        final Object[] values = type.getPropertyValues(component, getEntityMode(criteria, criteriaQuery));
        final Type[] subtypes = type.getSubtypes();
        for (int i = 0; i < propertyNames.length; i++) {
            final String subPath = StringHelper.qualify(path, propertyNames[i]);
            final Object value = values[i];
            if (isPropertyIncluded(value, subPath, subtypes[i])) {
                final Type subtype = subtypes[i];
                if (subtype.isComponentType()) {
                    appendComponentCondition(subPath, value, (CompositeType) subtype, criteria, criteriaQuery, buf);
                } else {
                    appendPropertyCondition(subPath, value, criteria, criteriaQuery, buf);
                }
            }
        }
    }
}
Also used : CompositeType(org.hibernate.type.CompositeType) Type(org.hibernate.type.Type)

Example 3 with CompositeType

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

the class Example method getTypedValues.

@Override
public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) {
    final EntityPersister meta = criteriaQuery.getFactory().getEntityPersister(criteriaQuery.getEntityName(criteria));
    final String[] propertyNames = meta.getPropertyNames();
    final Type[] propertyTypes = meta.getPropertyTypes();
    final Object[] values = meta.getPropertyValues(exampleEntity);
    final List<TypedValue> list = new ArrayList<TypedValue>();
    for (int i = 0; i < propertyNames.length; i++) {
        final Object value = values[i];
        final Type type = propertyTypes[i];
        final String name = propertyNames[i];
        final boolean isVersionProperty = i == meta.getVersionProperty();
        if (!isVersionProperty && isPropertyIncluded(value, name, type)) {
            if (propertyTypes[i].isComponentType()) {
                addComponentTypedValues(name, value, (CompositeType) type, list, criteria, criteriaQuery);
            } else {
                addPropertyTypedValue(value, type, list);
            }
        }
    }
    return list.toArray(new TypedValue[list.size()]);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) CompositeType(org.hibernate.type.CompositeType) Type(org.hibernate.type.Type) ArrayList(java.util.ArrayList) TypedValue(org.hibernate.engine.spi.TypedValue)

Example 4 with CompositeType

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

the class Example method toSqlString.

@Override
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) {
    final StringBuilder buf = new StringBuilder().append('(');
    final EntityPersister meta = criteriaQuery.getFactory().getEntityPersister(criteriaQuery.getEntityName(criteria));
    final String[] propertyNames = meta.getPropertyNames();
    final Type[] propertyTypes = meta.getPropertyTypes();
    final Object[] propertyValues = meta.getPropertyValues(exampleEntity);
    for (int i = 0; i < propertyNames.length; i++) {
        final Object propertyValue = propertyValues[i];
        final String propertyName = propertyNames[i];
        final boolean isVersionProperty = i == meta.getVersionProperty();
        if (!isVersionProperty && isPropertyIncluded(propertyValue, propertyName, propertyTypes[i])) {
            if (propertyTypes[i].isComponentType()) {
                appendComponentCondition(propertyName, propertyValue, (CompositeType) propertyTypes[i], criteria, criteriaQuery, buf);
            } else {
                appendPropertyCondition(propertyName, propertyValue, criteria, criteriaQuery, buf);
            }
        }
    }
    if (buf.length() == 1) {
        buf.append("1=1");
    }
    return buf.append(')').toString();
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) CompositeType(org.hibernate.type.CompositeType) Type(org.hibernate.type.Type)

Example 5 with CompositeType

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

the class Nullability method checkSubElementsNullability.

/**
	 * check sub elements-nullability. Returns property path that break
	 * nullability or null if none
	 *
	 * @param propertyType type to check
	 * @param value value to check
	 *
	 * @return property path
	 * @throws HibernateException error while getting subcomponent values
	 */
private String checkSubElementsNullability(Type propertyType, Object value) throws HibernateException {
    if (propertyType.isComponentType()) {
        return checkComponentNullability(value, (CompositeType) propertyType);
    }
    if (propertyType.isCollectionType()) {
        // persistent collections may have components
        final CollectionType collectionType = (CollectionType) propertyType;
        final Type collectionElementType = collectionType.getElementType(session.getFactory());
        if (collectionElementType.isComponentType()) {
            // check for all components values in the collection
            final CompositeType componentType = (CompositeType) collectionElementType;
            final Iterator itr = CascadingActions.getLoadedElementsIterator(session, collectionType, value);
            while (itr.hasNext()) {
                final Object compositeElement = itr.next();
                if (compositeElement != null) {
                    return checkComponentNullability(compositeElement, componentType);
                }
            }
        }
    }
    return null;
}
Also used : CompositeType(org.hibernate.type.CompositeType) CollectionType(org.hibernate.type.CollectionType) Type(org.hibernate.type.Type) CollectionType(org.hibernate.type.CollectionType) Iterator(java.util.Iterator) CompositeType(org.hibernate.type.CompositeType)

Aggregations

CompositeType (org.hibernate.type.CompositeType)32 Type (org.hibernate.type.Type)22 AssociationType (org.hibernate.type.AssociationType)12 EntityType (org.hibernate.type.EntityType)11 EntityPersister (org.hibernate.persister.entity.EntityPersister)7 JoinType (org.hibernate.sql.JoinType)6 CollectionType (org.hibernate.type.CollectionType)5 ArrayList (java.util.ArrayList)4 Iterator (java.util.Iterator)4 MappingException (org.hibernate.MappingException)3 QueryException (org.hibernate.QueryException)3 ExpandingCompositeQuerySpace (org.hibernate.loader.plan.build.spi.ExpandingCompositeQuerySpace)3 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)3 HashSet (java.util.HashSet)2 HibernateException (org.hibernate.HibernateException)2 TypedValue (org.hibernate.engine.spi.TypedValue)2 CompositePropertyMapping (org.hibernate.loader.plan.build.internal.spaces.CompositePropertyMapping)2 ExpandingEntityQuerySpace (org.hibernate.loader.plan.build.spi.ExpandingEntityQuerySpace)2 AssociationKey (org.hibernate.persister.walking.spi.AssociationKey)2 AttributeDefinition (org.hibernate.persister.walking.spi.AttributeDefinition)2