HHH-2997 LikeExpression case sensitive not working properly

git-svn-id: https://svn.jboss.org/repos/hibernate/core/branches/Branch_3_3@19027 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Strong Liu 2010-03-18 21:50:25 +00:00
parent 9b09080499
commit 939e71053c
5 changed files with 172 additions and 1 deletions

View File

@ -93,7 +93,7 @@ public class LikeExpression implements Criterion {
Criteria criteria,
CriteriaQuery criteriaQuery) throws HibernateException {
return new TypedValue[] {
criteriaQuery.getTypedValue( criteria, propertyName, value.toString().toLowerCase() )
criteriaQuery.getTypedValue( criteria, propertyName, ignoreCase ? value.toString().toLowerCase() : value.toString() )
};
}
}

View File

@ -0,0 +1,116 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*
*/
package org.hibernate.test.criteria;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.hibernate.junit.functional.FunctionalTestCase;
public class LikeTest extends FunctionalTestCase {
public LikeTest( String string ) {
super( string );
}
public String[] getMappings() {
return new String[]{"criteria/TestObject.hbm.xml"};
}
public void testLike(){
Session session=openSession();
String uniq = "uniq" + System.currentTimeMillis();
// insert object
try {
Transaction tx = session.beginTransaction();
TestObject obj = new TestObject();
obj.setText("XyZ " + uniq + " blablabla");
session.saveOrUpdate(obj);
session.flush();
tx.commit();
} finally {
session.close();
}
String pattern = "XyZ " + uniq + "%";
// retrieve object - case sensitive - works ok
session = openSession();
try {
List objects = session.createCriteria(TestObject.class)
.add(Restrictions.like("text", pattern))
.list();
assertEquals(1, objects.size());
} finally {
session.close();
}
// retrieve object - case insensitive - works ok
session = openSession();
try {
List objects = session.createCriteria(TestObject.class)
.add(Restrictions.like("text", pattern).ignoreCase())
.list();
assertEquals(1, objects.size());
} finally {
session.close();
}
// retrieve object - case insensitive via custom expression - works ok
session = openSession();
try {
List objects = session.createCriteria(TestObject.class)
.add(StringExpression.stringExpression("text", pattern, true))
.list();
assertEquals(1, objects.size());
} finally {
session.close();
}
// retrieve object - case sensitive via custom expression - not working
session = openSession();
try {
List objects = session.createCriteria(TestObject.class)
.add(StringExpression.stringExpression("text", pattern, false))
.list();
assertEquals(1, objects.size());
} finally {
session.close();
}
}
}

View File

@ -0,0 +1,18 @@
package org.hibernate.test.criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.LikeExpression;
public class StringExpression extends LikeExpression {
private final static Character ESCAPE_CODE = new Character( '\\' );
protected StringExpression( String property, String value,
boolean ignoreCase ) {
super( property, value, ESCAPE_CODE, ignoreCase );
}
public static Criterion stringExpression( String propertyName,
String value, boolean ignoreCase ) {
return new StringExpression( propertyName, value, ignoreCase );
}
}

View File

@ -0,0 +1,15 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.criteria">
<class name="TestObject" table="test">
<id name="id">
<column name="ID" />
<generator class="sequence">
<param name="sequence">test_seq</param>
</generator>
</id>
<property name="text" />
</class>
</hibernate-mapping>

View File

@ -0,0 +1,22 @@
package org.hibernate.test.criteria;
public class TestObject {
private Integer id;
private String text;
public Integer getId() {
return id;
}
public void setId( Integer id ) {
this.id = id;
}
public String getText() {
return text;
}
public void setText( String text ) {
this.text = text;
}
}