mirror of
https://github.com/querydsl/querydsl.git
synced 2026-07-03 21:07:49 +08:00
Fix parameter handling
This commit is contained in:
parent
295f455e9c
commit
cfe4c0c4ba
@ -16,12 +16,14 @@ package com.querydsl.jpa.hibernate;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.hibernate.Query;
|
||||
import org.hibernate.type.*;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.querydsl.core.types.ParamExpression;
|
||||
import com.querydsl.core.types.ParamNotSetException;
|
||||
import com.querydsl.core.types.dsl.Param;
|
||||
@ -34,17 +36,26 @@ import com.querydsl.core.types.dsl.Param;
|
||||
*/
|
||||
public final class HibernateUtil {
|
||||
|
||||
private static final Map<Class<?>,Type> TYPES = new HashMap<Class<?>,Type>();
|
||||
private static final Set<Class<?>> BUILT_IN = ImmutableSet.of(Boolean.class, Byte.class,
|
||||
Character.class, Double.class, Float.class, Integer.class, Long.class, Short.class,
|
||||
String.class, BigDecimal.class, byte[].class, Byte[].class, java.util.Date.class,
|
||||
java.util.Calendar.class, java.sql.Date.class, java.sql.Time.class, java.sql.Timestamp.class,
|
||||
java.util.Locale.class, java.util.TimeZone.class, java.util.Currency.class, Class.class,
|
||||
java.io.Serializable.class, java.sql.Blob.class, java.sql.Clob.class);
|
||||
|
||||
private static final Map<Class<?>, Type> NUMERIC_TYPES;
|
||||
|
||||
static {
|
||||
TYPES.put(Byte.class, new ByteType());
|
||||
TYPES.put(Short.class, new ShortType());
|
||||
TYPES.put(Integer.class, new IntegerType());
|
||||
TYPES.put(Long.class, new LongType());
|
||||
TYPES.put(BigInteger.class, new BigIntegerType());
|
||||
TYPES.put(Double.class, new DoubleType());
|
||||
TYPES.put(Float.class, new FloatType());
|
||||
TYPES.put(BigDecimal.class, new BigDecimalType());
|
||||
ImmutableMap.Builder<Class<?>, Type> builder = ImmutableMap.builder();
|
||||
builder.put(Byte.class, new ByteType());
|
||||
builder.put(Short.class, new ShortType());
|
||||
builder.put(Integer.class, new IntegerType());
|
||||
builder.put(Long.class, new LongType());
|
||||
builder.put(BigInteger.class, new BigIntegerType());
|
||||
builder.put(Double.class, new DoubleType());
|
||||
builder.put(Float.class, new FloatType());
|
||||
builder.put(BigDecimal.class, new BigDecimalType());
|
||||
NUMERIC_TYPES = builder.build();
|
||||
}
|
||||
|
||||
private HibernateUtil() { }
|
||||
@ -67,10 +78,10 @@ public final class HibernateUtil {
|
||||
private static void setValue(Query query, String key, Object val) {
|
||||
if (val instanceof Collection<?>) {
|
||||
query.setParameterList(key, (Collection<?>) val);
|
||||
} else if (val.getClass().isArray()) {
|
||||
} else if (val instanceof Object[] && !BUILT_IN.contains(val.getClass())) {
|
||||
query.setParameterList(key, (Object[]) val);
|
||||
} else if (TYPES.containsKey(val.getClass())) {
|
||||
query.setParameter(key, val, TYPES.get(val.getClass()));
|
||||
} else if (NUMERIC_TYPES.containsKey(val.getClass())) {
|
||||
query.setParameter(key, val, NUMERIC_TYPES.get(val.getClass()));
|
||||
} else {
|
||||
query.setParameter(key, val);
|
||||
}
|
||||
|
||||
@ -544,13 +544,13 @@ public abstract class AbstractJPATest {
|
||||
|
||||
@Test
|
||||
public void Date() {
|
||||
assertEquals(2000, query().from(cat).select(cat.birthdate.year()).fetchFirst().intValue());
|
||||
assertEquals(2000, query().from(cat).select(cat.birthdate.year()).fetchFirst().intValue());
|
||||
assertEquals(200002, query().from(cat).select(cat.birthdate.yearMonth()).fetchFirst().intValue());
|
||||
assertEquals(2, query().from(cat).select(cat.birthdate.month()).fetchFirst().intValue());
|
||||
assertEquals(2, query().from(cat).select(cat.birthdate.dayOfMonth()).fetchFirst().intValue());
|
||||
assertEquals(3, query().from(cat).select(cat.birthdate.hour()).fetchFirst().intValue());
|
||||
assertEquals(4, query().from(cat).select(cat.birthdate.minute()).fetchFirst().intValue());
|
||||
assertEquals(0, query().from(cat).select(cat.birthdate.second()).fetchFirst().intValue());
|
||||
assertEquals(2, query().from(cat).select(cat.birthdate.month()).fetchFirst().intValue());
|
||||
assertEquals(2, query().from(cat).select(cat.birthdate.dayOfMonth()).fetchFirst().intValue());
|
||||
assertEquals(3, query().from(cat).select(cat.birthdate.hour()).fetchFirst().intValue());
|
||||
assertEquals(4, query().from(cat).select(cat.birthdate.minute()).fetchFirst().intValue());
|
||||
assertEquals(0, query().from(cat).select(cat.birthdate.second()).fetchFirst().intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -1166,7 +1166,7 @@ public abstract class AbstractJPATest {
|
||||
@Test
|
||||
public void Params() {
|
||||
Param<String> name = new Param<String>(String.class,"name");
|
||||
assertEquals("Bob123",query().from(cat).where(cat.name.eq(name)).set(name, "Bob123")
|
||||
assertEquals("Bob123", query().from(cat).where(cat.name.eq(name)).set(name, "Bob123")
|
||||
.select(cat.name).fetchFirst());
|
||||
}
|
||||
|
||||
@ -1615,5 +1615,13 @@ public abstract class AbstractJPATest {
|
||||
query().from(animal).orderBy(JPAExpressions.type(animal).asc(), animal.id.asc())
|
||||
.select(animal.id).fetch());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ExcludeIn(DERBY)
|
||||
public void Byte_Array() {
|
||||
QSimpleTypes simpleTypes = QSimpleTypes.simpleTypes;
|
||||
assertEquals(ImmutableList.of(), query().from(simpleTypes)
|
||||
.where(simpleTypes.byteArray.eq(new byte[]{0, 1})).select(simpleTypes).fetch());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -64,4 +64,6 @@ public class SimpleTypes {
|
||||
|
||||
// @Temporal(TemporalType.TIMESTAMP)
|
||||
java.sql.Timestamp timestamp;
|
||||
|
||||
byte[] byteArray;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user