HHH-2088 : TypeMismatchException on object equality expression from one-to-one relationship

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@19136 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Gail Badner 2010-03-30 01:05:18 +00:00
parent 26516fb559
commit fe7d83566c
3 changed files with 43 additions and 2 deletions

View File

@ -23,6 +23,7 @@
*/
package org.hibernate.hql.ast.tree;
import org.hibernate.type.OneToOneType;
import org.hibernate.type.Type;
import org.hibernate.Hibernate;
import org.hibernate.TypeMismatchException;
@ -80,8 +81,8 @@ protected final void mutateRowValueConstructorSyntaxesIfNecessary(Type lhsType,
// resolve an expected type
SessionFactoryImplementor sessionFactory = getSessionFactoryHelper().getFactory();
if ( lhsType != null && rhsType != null ) {
int lhsColumnSpan = lhsType.getColumnSpan( sessionFactory );
if ( lhsColumnSpan != rhsType.getColumnSpan( sessionFactory ) ) {
int lhsColumnSpan = getColumnSpan( lhsType, sessionFactory );
if ( lhsColumnSpan != getColumnSpan( rhsType, sessionFactory ) ) {
throw new TypeMismatchException(
"left and right hand sides of a binary logic operator were incompatibile [" +
lhsType.getName() + " : "+ rhsType.getName() + "]"
@ -97,6 +98,14 @@ protected final void mutateRowValueConstructorSyntaxesIfNecessary(Type lhsType,
}
}
private int getColumnSpan(Type type, SessionFactoryImplementor sfi) {
int columnSpan = type.getColumnSpan( sfi );
if ( columnSpan == 0 && type instanceof OneToOneType ) {
columnSpan = ( ( OneToOneType ) type ).getIdentifierOrUniqueKeyType( sfi ).getColumnSpan( sfi );
}
return columnSpan;
}
/**
* Mutate the subtree relating to a row-value-constructor to instead use
* a series of ANDed predicates. This allows multi-column type comparisons

View File

@ -565,6 +565,32 @@ private int determineCount(Iterator iterator) {
return count;
}
public void testEntityAndOneToOneReturnedByQuery() {
Session s = openSession();
s.beginTransaction();
Human h = new Human();
h.setName( new Name( "Gail", null, "Badner" ) );
s.save( h );
User u = new User();
u.setUserName( "gbadner" );
u.setHuman( h );
s.save( u );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
Object [] result = ( Object [] ) s.createQuery( "from User u, Human h where u.human = h" ).uniqueResult();
assertNotNull( result );
assertEquals( u.getUserName(), ( ( User ) result[ 0 ] ).getUserName() );
assertEquals( h.getName().getFirst(), ( ( Human ) result[ 1 ] ).getName().getFirst() );
assertSame( ( ( User ) result[ 0 ] ).getHuman(), result[ 1 ] );
s.createQuery( "delete User" ).executeUpdate();
s.createQuery( "delete Human" ).executeUpdate();
s.getTransaction().commit();
s.close();
}
public void testNestedComponentIsNull() {
// (1) From MapTest originally...
// (2) Was then moved into HQLTest...

View File

@ -72,6 +72,12 @@ public void testOrphanedWhileManaged() {
assertEquals( 1, results.size() );
Employee emp = ( Employee ) results.get( 0 );
assertNotNull( emp.getInfo() );
results = session.createQuery( "from Employee e, EmployeeInfo i where e.info = i" ).list();
assertEquals( 1, results.size() );
Object [] result = ( Object [] ) results.get( 0 );
emp = ( Employee ) result[ 0 ];
assertNotNull( result[ 1 ] );
assertSame( emp.getInfo(), result[ 1 ] );
emp.setInfo( null );
session.getTransaction().commit();
session.close();