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());
}
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()]);
}
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;
}
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;
}
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());
}
Aggregations