Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch '2.1.x' into 2.2.x

  • Loading branch information...
commit c1609645d3018a8a47294069c80306e36a794dad 2 parents b1e5493 + bc327a9
@graemerocher graemerocher authored
View
8 ...main/groovy/org/codehaus/groovy/grails/orm/hibernate/query/HibernateCriterionAdapter.java
@@ -40,12 +40,15 @@
public class HibernateCriterionAdapter {
private static final Map<Class<?>, CriterionAdaptor> criterionAdaptors = new HashMap<Class<?>, CriterionAdaptor>();
private String alias;
+ private PersistentEntity entity;
- public HibernateCriterionAdapter(Query.Criterion criterion, String alias) {
+ public HibernateCriterionAdapter(PersistentEntity entity, Query.Criterion criterion, String alias) {
this.criterion = criterion;
this.alias = alias;
+ this.entity = entity;
}
+
static {
criterionAdaptors.put(DetachedAssociationCriteria.class, new CriterionAdaptor() {
@Override
@@ -326,6 +329,9 @@ private static void applySubCriteriaToJunction(PersistentEntity entity, Hibernat
HibernateCriteriaBuilder.getHibernateDetachedCriteria((QueryableCriteria<?>) pc.getValue())
);
}
+ else {
+ HibernateQuery.doTypeConversionIfNeccessary(entity, pc);
+ }
}
CriterionAdaptor criterionAdaptor = criterionAdaptors.get(subCriterion.getClass());
if (criterionAdaptor != null) {
View
46 ...ernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/query/HibernateQuery.java
@@ -38,6 +38,10 @@
import org.hibernate.persister.entity.PropertyMapping;
import org.hibernate.type.BasicType;
import org.hibernate.type.TypeResolver;
+import org.springframework.core.convert.ConversionException;
+import org.springframework.core.convert.ConversionService;
+import org.springframework.core.convert.support.DefaultConversionService;
+import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.InvalidDataAccessResourceUsageException;
import org.springframework.orm.hibernate3.HibernateTemplate;
@@ -59,6 +63,7 @@
private Map<String, CriteriaAndAlias> createdAssociationPaths = new HashMap<String, CriteriaAndAlias> ();
private static final String ALIAS = "_alias";
private static Field opField = ReflectionUtils.findField(SimpleExpression.class, "op");
+ private static ConversionService conversionService = new DefaultConversionService();
public HibernateQuery(Criteria criteria, HibernateSession session, PersistentEntity entity) {
super(session, entity);
@@ -106,17 +111,30 @@ public void add(Criterion criterion) {
}
else if (criterion instanceof PropertyCriterion) {
PropertyCriterion pc = (PropertyCriterion) criterion;
- if (pc.getValue() instanceof QueryableCriteria) {
- DetachedCriteria hibernateDetachedCriteria = HibernateCriteriaBuilder.getHibernateDetachedCriteria((QueryableCriteria) pc.getValue());
+ Object value = pc.getValue();
+ if (value instanceof QueryableCriteria) {
+ DetachedCriteria hibernateDetachedCriteria = HibernateCriteriaBuilder.getHibernateDetachedCriteria((QueryableCriteria) value);
pc.setValue(hibernateDetachedCriteria);
}
+ else {
+ doTypeConversionIfNeccessary(getEntity(), pc);
+ }
}
- final org.hibernate.criterion.Criterion hibernateCriterion = new HibernateCriterionAdapter(criterion, alias).toHibernateCriterion(this);
+ final org.hibernate.criterion.Criterion hibernateCriterion = new HibernateCriterionAdapter(getEntity(), criterion, alias).toHibernateCriterion(this);
if (hibernateCriterion != null) {
addToCriteria(hibernateCriterion);
}
}
+ static void doTypeConversionIfNeccessary(PersistentEntity entity, PropertyCriterion pc) {
+ String property = pc.getProperty();
+ Object value = pc.getValue();
+ PersistentProperty p = entity.getPropertyByName(property);
+ if(p != null && !p.getType().isInstance(value)) {
+ pc.setValue( conversionService.convert(value, p.getType()));
+ }
+ }
+
org.hibernate.criterion.Criterion getRestrictionForFunctionCall(FunctionCallingCriterion criterion, @SuppressWarnings("hiding") PersistentEntity entity) {
org.hibernate.criterion.Criterion sqlRestriction;HibernateTemplate hibernateSession = (HibernateTemplate)session.getNativeInterface();
@@ -141,7 +159,7 @@ else if (criterion instanceof PropertyCriterion) {
if (basic != null && datastoreCriterion instanceof PropertyCriterion) {
PropertyCriterion pc = (PropertyCriterion) datastoreCriterion;
- final org.hibernate.criterion.Criterion hibernateCriterion = new HibernateCriterionAdapter(datastoreCriterion, alias).toHibernateCriterion(this);
+ final org.hibernate.criterion.Criterion hibernateCriterion = new HibernateCriterionAdapter(getEntity(),datastoreCriterion, alias).toHibernateCriterion(this);
if (hibernateCriterion instanceof SimpleExpression) {
SimpleExpression expr = (SimpleExpression) hibernateCriterion;
Object op = ReflectionUtils.getField(opField, expr);
@@ -209,16 +227,16 @@ public Query gt(String property, Object value) {
@Override
public Query and(Criterion a, Criterion b) {
- HibernateCriterionAdapter aa = new HibernateCriterionAdapter(a, alias);
- HibernateCriterionAdapter ab = new HibernateCriterionAdapter(a, alias);
+ HibernateCriterionAdapter aa = new HibernateCriterionAdapter(getEntity(),a, alias);
+ HibernateCriterionAdapter ab = new HibernateCriterionAdapter(getEntity(),a, alias);
addToCriteria(Restrictions.and(aa.toHibernateCriterion(this), ab.toHibernateCriterion(this)));
return this;
}
@Override
public Query or(Criterion a, Criterion b) {
- HibernateCriterionAdapter aa = new HibernateCriterionAdapter(a, alias);
- HibernateCriterionAdapter ab = new HibernateCriterionAdapter(a, alias);
+ HibernateCriterionAdapter aa = new HibernateCriterionAdapter(getEntity(),a, alias);
+ HibernateCriterionAdapter ab = new HibernateCriterionAdapter(getEntity(),a, alias);
addToCriteria(Restrictions.or(aa.toHibernateCriterion(this), ab.toHibernateCriterion(this)));
return this;
@@ -438,7 +456,7 @@ public Junction add(Criterion c) {
}
}
else {
- HibernateCriterionAdapter adapter = new HibernateCriterionAdapter(c, alias);
+ HibernateCriterionAdapter adapter = new HibernateCriterionAdapter(getEntity(),c, alias);
org.hibernate.criterion.Criterion criterion = adapter.toHibernateCriterion(HibernateQuery.this);
if (criterion != null) {
hibernateJunction.add(criterion);
@@ -578,7 +596,7 @@ public Query isNotNull(String property) {
@Override
public void add(Criterion criterion) {
- final org.hibernate.criterion.Criterion hibernateCriterion = new HibernateCriterionAdapter(criterion, alias).toHibernateCriterion(HibernateQuery.this);
+ final org.hibernate.criterion.Criterion hibernateCriterion = new HibernateCriterionAdapter(getEntity(),criterion, alias).toHibernateCriterion(HibernateQuery.this);
if (hibernateCriterion != null) {
addToCriteria(hibernateCriterion);
}
@@ -618,16 +636,16 @@ public Query gt(String property, Object value) {
@Override
public Query and(Criterion a, Criterion b) {
- HibernateCriterionAdapter aa = new HibernateCriterionAdapter(a, alias);
- HibernateCriterionAdapter ab = new HibernateCriterionAdapter(a, alias);
+ HibernateCriterionAdapter aa = new HibernateCriterionAdapter(getEntity(),a, alias);
+ HibernateCriterionAdapter ab = new HibernateCriterionAdapter(getEntity(),a, alias);
addToCriteria(Restrictions.and(aa.toHibernateCriterion(HibernateQuery.this), ab.toHibernateCriterion(HibernateQuery.this)));
return this;
}
@Override
public Query or(Criterion a, Criterion b) {
- HibernateCriterionAdapter aa = new HibernateCriterionAdapter(a, alias);
- HibernateCriterionAdapter ab = new HibernateCriterionAdapter(a, alias);
+ HibernateCriterionAdapter aa = new HibernateCriterionAdapter(getEntity(),a, alias);
+ HibernateCriterionAdapter ab = new HibernateCriterionAdapter(getEntity(),a, alias);
addToCriteria(Restrictions.or(aa.toHibernateCriterion(HibernateQuery.this), ab.toHibernateCriterion(HibernateQuery.this)));
return this;
}
View
7 .../src/main/groovy/org/codehaus/groovy/grails/domain/GrailsDomainClassPersistentEntity.java
@@ -130,7 +130,12 @@ public PersistentProperty getIdentity() {
}
public PersistentProperty getPropertyByName(String name) {
- return propertiesByName.get(name);
+ if(getIdentity().getName().equals(name)) {
+ return getIdentity();
+ }
+ else {
+ return propertiesByName.get(name);
+ }
}
public Class getJavaClass() {
View
14 ...rsistence/src/test/groovy/org/codehaus/groovy/grails/orm/hibernate/WhereMethodSpec.groovy
@@ -3,6 +3,7 @@ package org.codehaus.groovy.grails.orm.hibernate
import grails.gorm.DetachedCriteria
import spock.lang.Ignore
import org.codehaus.groovy.control.MultipleCompilationErrorsException
+import spock.lang.Issue
/**
* Tests the where method in Grails
@@ -12,6 +13,19 @@ class WhereMethodSpec extends GormSpec {
List getDomainClasses() {
[Person, Pet]
}
+ @Issue('GRAILS-9447')
+ def "Test where query integer type conversion"() {
+ given:"some people"
+ createPeopleWithPets()
+
+ when:"A where query is used with an integer value and a long property type"
+ def results = Pet.where { owner.id == 2 }.list()
+
+ then:"The correct results are returned and type conversion happens as expected"
+ results.size() == 3
+ results[0].id == 3
+
+ }
def "Test whereAny method"() {
given:"some people"

0 comments on commit c160964

Please sign in to comment.
Something went wrong with that request. Please try again.