Search in sources :

Example 1 with Type

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

the class SpatialProjections method extent.

/**
	 * Applies an extent projection to the specified geometry function
	 *
	 * <p>The extent of a set of {@code Geometry}s is the union of their bounding boxes.</p>
	 *
	 * @param propertyName The property to use for calculating the extent
	 *
	 * @return an extent-projection for the specified property.
	 */
public static Projection extent(final String propertyName) {
    return new SimpleProjection() {

        public Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
            return new Type[] { criteriaQuery.getType(criteria, propertyName) };
        }

        public String toSqlString(Criteria criteria, int position, CriteriaQuery criteriaQuery) throws HibernateException {
            final StringBuilder stbuf = new StringBuilder();
            final SessionFactoryImplementor factory = criteriaQuery.getFactory();
            final String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName);
            final Dialect dialect = factory.getDialect();
            if (dialect instanceof SpatialDialect) {
                final SpatialDialect seDialect = (SpatialDialect) dialect;
                stbuf.append(seDialect.getSpatialAggregateSQL(columns[0], SpatialAggregate.EXTENT));
                stbuf.append(" as y").append(position).append('_');
                return stbuf.toString();
            }
            return null;
        }
    };
}
Also used : Type(org.hibernate.type.Type) SpatialDialect(org.hibernate.spatial.SpatialDialect) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) CriteriaQuery(org.hibernate.criterion.CriteriaQuery) Dialect(org.hibernate.dialect.Dialect) SpatialDialect(org.hibernate.spatial.SpatialDialect) Criteria(org.hibernate.Criteria) SimpleProjection(org.hibernate.criterion.SimpleProjection)

Example 2 with Type

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

the class TypedValueSerializationTest method testTypedValueSerialization.

@Test
@TestForIssue(jiraKey = "HHH-9024")
public void testTypedValueSerialization() throws Exception {
    final Type mockType = mock(Type.class);
    final String value = "foo";
    final TypedValue typedValue = new TypedValue(mockType, value);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(typedValue);
    final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    final TypedValue typedValueClone = (TypedValue) ois.readObject();
    assertEquals(typedValue.hashCode(), typedValueClone.hashCode());
    assertEquals(typedValue.toString(), typedValueClone.toString());
    assertEquals(typedValue.getValue(), typedValueClone.getValue());
}
Also used : Type(org.hibernate.type.Type) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) TypedValue(org.hibernate.engine.spi.TypedValue) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 3 with Type

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

the class CriteriaTools method identifierPropertyNames.

/**
	 * @param sessionFactory Session factory.
	 * @param entityName Entity name.
	 *
	 * @return List of property names representing entity identifier.
	 */
private static List<String> identifierPropertyNames(SessionFactoryImplementor sessionFactory, String entityName) {
    final String identifierPropertyName = sessionFactory.getMetamodel().entityPersister(entityName).getIdentifierPropertyName();
    if (identifierPropertyName != null) {
        // Single id.
        return Arrays.asList(identifierPropertyName);
    }
    final Type identifierType = sessionFactory.getMetamodel().entityPersister(entityName).getIdentifierType();
    if (identifierType instanceof EmbeddedComponentType) {
        // Multiple ids.
        final EmbeddedComponentType embeddedComponentType = (EmbeddedComponentType) identifierType;
        return Arrays.asList(embeddedComponentType.getPropertyNames());
    }
    return Collections.emptyList();
}
Also used : RelationType(org.hibernate.envers.internal.entities.RelationType) EmbeddedComponentType(org.hibernate.type.EmbeddedComponentType) Type(org.hibernate.type.Type) EmbeddedComponentType(org.hibernate.type.EmbeddedComponentType)

Example 4 with Type

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

the class SimpleAuditExpression method addToQuery.

@Override
protected void addToQuery(EnversService enversService, AuditReaderImplementor versionsReader, String entityName, String alias, QueryBuilder qb, Parameters parameters) {
    String propertyName = CriteriaTools.determinePropertyName(enversService, versionsReader, entityName, propertyNameGetter);
    RelationDescription relatedEntity = CriteriaTools.getRelatedEntity(enversService, entityName, propertyName);
    if (relatedEntity == null) {
        // HHH-9178 - Add support to component type equality.
        // This basically will allow = and <> operators to perform component-based equality checks.
        // Any other operator for a component type will not be supported.
        // Non-component types will continue to behave normally.
        final SessionImplementor session = versionsReader.getSessionImplementor();
        final Type type = getPropertyType(session, entityName, propertyName);
        if (type != null && type.isComponentType()) {
            if (!"=".equals(op) && !"<>".equals(op)) {
                throw new AuditException("Component-based criterion is not supported for op: " + op);
            }
            final ComponentType componentType = (ComponentType) type;
            for (int i = 0; i < componentType.getPropertyNames().length; i++) {
                final Object componentValue = componentType.getPropertyValue(value, i, session);
                parameters.addWhereWithParam(alias, propertyName + "_" + componentType.getPropertyNames()[i], op, componentValue);
            }
        } else {
            parameters.addWhereWithParam(alias, propertyName, op, value);
        }
    } else {
        if (!"=".equals(op) && !"<>".equals(op)) {
            throw new AuditException("This type of operation: " + op + " (" + entityName + "." + propertyName + ") isn't supported and can't be used in queries.");
        }
        Object id = relatedEntity.getIdMapper().mapToIdFromEntity(value);
        relatedEntity.getIdMapper().addIdEqualsToQuery(parameters, id, alias, null, "=".equals(op));
    }
}
Also used : ComponentType(org.hibernate.type.ComponentType) Type(org.hibernate.type.Type) ComponentType(org.hibernate.type.ComponentType) RelationDescription(org.hibernate.envers.internal.entities.RelationDescription) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) AuditException(org.hibernate.envers.exception.AuditException)

Example 5 with Type

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

the class AuditMetadataGenerator method addValueInSecondPass.

private void addValueInSecondPass(Element parent, Value value, CompositeMapperBuilder currentMapper, String entityName, EntityXmlMappingData xmlMappingData, PropertyAuditingData propertyAuditingData, boolean insertable, boolean processModifiedFlag) {
    final Type type = value.getType();
    if (type instanceof ComponentType) {
        componentMetadataGenerator.addComponent(parent, propertyAuditingData, value, currentMapper, entityName, xmlMappingData, false);
        // mod flag field has been already generated in first pass
        return;
    } else if (type instanceof ManyToOneType) {
        toOneRelationMetadataGenerator.addToOne(parent, propertyAuditingData, value, currentMapper, entityName, insertable);
    } else if (type instanceof OneToOneType) {
        final OneToOne oneToOne = (OneToOne) value;
        if (oneToOne.getReferencedPropertyName() != null) {
            toOneRelationMetadataGenerator.addOneToOneNotOwning(propertyAuditingData, value, currentMapper, entityName);
        } else {
            // @OneToOne relation marked with @PrimaryKeyJoinColumn
            toOneRelationMetadataGenerator.addOneToOnePrimaryKeyJoinColumn(propertyAuditingData, value, currentMapper, entityName, insertable);
        }
    } else if (type instanceof CollectionType) {
        final CollectionMetadataGenerator collectionMetadataGenerator = new CollectionMetadataGenerator(this, (Collection) value, currentMapper, entityName, xmlMappingData, propertyAuditingData);
        collectionMetadataGenerator.addCollection();
    } else {
        return;
    }
    addModifiedFlagIfNeeded(parent, propertyAuditingData, processModifiedFlag);
}
Also used : CollectionType(org.hibernate.type.CollectionType) ManyToOneType(org.hibernate.type.ManyToOneType) ComponentType(org.hibernate.type.ComponentType) OneToOneType(org.hibernate.type.OneToOneType) TimestampType(org.hibernate.type.TimestampType) Type(org.hibernate.type.Type) ComponentType(org.hibernate.type.ComponentType) OneToOne(org.hibernate.mapping.OneToOne) ManyToOneType(org.hibernate.type.ManyToOneType) CollectionType(org.hibernate.type.CollectionType) Collection(org.hibernate.mapping.Collection) OneToOneType(org.hibernate.type.OneToOneType)

Aggregations

Type (org.hibernate.type.Type)234 EntityType (org.hibernate.type.EntityType)59 CollectionType (org.hibernate.type.CollectionType)49 AssociationType (org.hibernate.type.AssociationType)48 CompositeType (org.hibernate.type.CompositeType)45 JoinType (org.hibernate.sql.JoinType)40 Test (org.junit.Test)38 EntityPersister (org.hibernate.persister.entity.EntityPersister)29 ComponentType (org.hibernate.type.ComponentType)28 ArrayList (java.util.ArrayList)26 List (java.util.List)22 VersionType (org.hibernate.type.VersionType)22 QueryException (org.hibernate.QueryException)21 Session (org.hibernate.Session)20 Serializable (java.io.Serializable)18 HashMap (java.util.HashMap)16 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)16 Iterator (java.util.Iterator)14 PersistentClass (org.hibernate.mapping.PersistentClass)14 SQLException (java.sql.SQLException)13