HHH-3828 Criteria: Restriction whith class does not work

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18658 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Strong Liu 2010-01-27 23:06:38 +00:00
parent d04362e464
commit 1904e02b63
6 changed files with 172 additions and 1 deletions

View File

@ -531,6 +531,14 @@ public class CriteriaQueryTranslator implements CriteriaQuery {
if ( q != null ) { if ( q != null ) {
Type type = q.getDiscriminatorType(); Type type = q.getDiscriminatorType();
String stringValue = q.getDiscriminatorSQLValue(); String stringValue = q.getDiscriminatorSQLValue();
if (stringValue != null && stringValue.length() > 2
&& stringValue.startsWith("'")
&& stringValue.endsWith("'")) { // remove the single
// quotes
stringValue = stringValue.substring(1,
stringValue.length() - 1);
}
// Convert the string value into the proper type. // Convert the string value into the proper type.
if ( type instanceof NullableType ) { if ( type instanceof NullableType ) {
NullableType nullableType = ( NullableType ) type; NullableType nullableType = ( NullableType ) type;

View File

@ -0,0 +1,39 @@
/*
* 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;
public abstract class AbstractFoo {
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}

View File

@ -0,0 +1,47 @@
/*
* 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;
public class Bar {
private Integer id;
AbstractFoo myFoo;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public AbstractFoo getMyFoo() {
return myFoo;
}
public void setMyFoo(AbstractFoo myFoo) {
this.myFoo = myFoo;
}
}

View File

@ -41,7 +41,7 @@ public class CriteriaQueryTest extends FunctionalTestCase {
} }
public String[] getMappings() { public String[] getMappings() {
return new String[] { "criteria/Enrolment.hbm.xml", "hql/Animal.hbm.xml" }; return new String[] { "criteria/Enrolment.hbm.xml","criteria/Foo.hbm.xml", "hql/Animal.hbm.xml" };
} }
public void configure(Configuration cfg) { public void configure(Configuration cfg) {
@ -707,6 +707,32 @@ public class CriteriaQueryTest extends FunctionalTestCase {
s.close(); s.close();
} }
public void testClassProperty2() {
Session session = openSession();
Transaction t = session.beginTransaction();
GreatFoo foo = new GreatFoo();
Bar b = new Bar();
b.setMyFoo(foo);
foo.setId(1);
b.setId(1);
session.persist(b);
session.flush();
t.commit();
session=openSession();
t=session.beginTransaction();
// OK, one BAR in DB
assertEquals(1, session.createCriteria(Bar.class).list().size());
Criteria crit = session.createCriteria(Bar.class, "b").createAlias(
"myFoo", "m").add(
Property.forName("m.class").eq(GreatFoo.class));
assertEquals(1, crit.list().size());
crit = session.createCriteria(Bar.class, "b").createAlias("myFoo", "m")
.add(Restrictions.eq("m.class", GreatFoo.class));
assertEquals(1, crit.list().size());
t.commit();
session.close();
}
public void testProjectedId() { public void testProjectedId() {
Session s = openSession(); Session s = openSession();
Transaction t = s.beginTransaction(); Transaction t = s.beginTransaction();

View File

@ -0,0 +1,22 @@
<?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="AbstractFoo">
<id name="id">
</id>
<discriminator type="string" column="subtype" />
<subclass name="GreatFoo" discriminator-value="KAPUT">
</subclass>
</class>
<class name="Bar">
<id name="id">
</id>
<one-to-one name="myFoo" cascade="all" />
</class>
</hibernate-mapping>

View File

@ -0,0 +1,29 @@
/*
* 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;
public class GreatFoo extends AbstractFoo {
}