Search in sources :

Example 1 with TypedValue

use of org.hibernate.engine.spi.TypedValue 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 2 with TypedValue

use of org.hibernate.engine.spi.TypedValue 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 3 with TypedValue

use of org.hibernate.engine.spi.TypedValue in project hibernate-orm by hibernate.

the class ParameterBinder method bindNamedParameters.

private static int bindNamedParameters(final PreparedStatement ps, final Map namedParams, final int start, final NamedParameterSource source, final SessionImplementor session) throws SQLException, HibernateException {
    if (namedParams != null) {
        final boolean debugEnabled = LOG.isDebugEnabled();
        // assumes that types are all of span 1
        final Iterator iter = namedParams.entrySet().iterator();
        int result = 0;
        while (iter.hasNext()) {
            final Map.Entry e = (Map.Entry) iter.next();
            final String name = (String) e.getKey();
            final TypedValue typedVal = (TypedValue) e.getValue();
            final int[] locations = source.getNamedParameterLocations(name);
            for (int location : locations) {
                if (debugEnabled) {
                    LOG.debugf("bindNamedParameters() %s -> %s [%s]", typedVal.getValue(), name, location + start);
                }
                typedVal.getType().nullSafeSet(ps, typedVal.getValue(), location + start, session);
            }
            result += locations.length;
        }
        return result;
    }
    return 0;
}
Also used : Iterator(java.util.Iterator) Map(java.util.Map) TypedValue(org.hibernate.engine.spi.TypedValue)

Example 4 with TypedValue

use of org.hibernate.engine.spi.TypedValue in project hibernate-orm by hibernate.

the class NativeSQLQueryPlan method bindNamedParameters.

/**
	 * Perform binding of all the JDBC bind parameter values based on the user-defined
	 * named query parameters into the JDBC {@link PreparedStatement}.
	 *
	 * @param ps The prepared statement to which to bind the parameter values.
	 * @param namedParams The named query parameters specified by the application.
	 * @param start JDBC paramer binds are positional, so this is the position
	 * from which to start binding.
	 * @param session The session from which the query originated.
	 *
	 * @return The number of JDBC bind positions accounted for during execution.
	 *
	 * @throws SQLException Some form of JDBC error binding the values.
	 * @throws HibernateException Generally indicates a mapping problem or type mismatch.
	 */
private int bindNamedParameters(final PreparedStatement ps, final Map namedParams, final int start, final SharedSessionContractImplementor session) throws SQLException {
    if (namedParams != null) {
        // assumes that types are all of span 1
        final Iterator iter = namedParams.entrySet().iterator();
        int result = 0;
        while (iter.hasNext()) {
            final Map.Entry e = (Map.Entry) iter.next();
            final String name = (String) e.getKey();
            final TypedValue typedval = (TypedValue) e.getValue();
            final int[] locs = getNamedParameterLocs(name);
            for (int loc : locs) {
                LOG.debugf("bindNamedParameters() %s -> %s [%s]", typedval.getValue(), name, loc + start);
                typedval.getType().nullSafeSet(ps, typedval.getValue(), loc + start, session);
            }
            result += locs.length;
        }
        return result;
    }
    return 0;
}
Also used : Iterator(java.util.Iterator) Map(java.util.Map) TypedValue(org.hibernate.engine.spi.TypedValue)

Example 5 with TypedValue

use of org.hibernate.engine.spi.TypedValue in project hibernate-orm by hibernate.

the class NamedParameterSpecification method bind.

/**
 * Bind the appropriate value into the given statement at the specified position.
 *
 * @param statement The statement into which the value should be bound.
 * @param qp The defined values for the current query execution.
 * @param session The session against which the current execution is occuring.
 * @param position The position from which to start binding value(s).
 *
 * @return The number of sql bind positions "eaten" by this bind operation.
 */
@Override
public int bind(PreparedStatement statement, QueryParameters qp, SharedSessionContractImplementor session, int position) throws SQLException {
    TypedValue typedValue = qp.getNamedParameters().get(name);
    typedValue.getType().nullSafeSet(statement, typedValue.getValue(), position, session);
    return typedValue.getType().getColumnSpan(session.getFactory());
}
Also used : TypedValue(org.hibernate.engine.spi.TypedValue)

Aggregations

TypedValue (org.hibernate.engine.spi.TypedValue)24 Type (org.hibernate.type.Type)9 Map (java.util.Map)5 ArrayList (java.util.ArrayList)4 Iterator (java.util.Iterator)3 CompositeType (org.hibernate.type.CompositeType)3 QueryException (org.hibernate.QueryException)2 RowSelection (org.hibernate.engine.spi.RowSelection)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 JoinType (org.hibernate.sql.JoinType)2 AssociationType (org.hibernate.type.AssociationType)2 CollectionType (org.hibernate.type.CollectionType)2 StringRepresentableType (org.hibernate.type.StringRepresentableType)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Serializable (java.io.Serializable)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1