Signed-off-by: Michael Rudolf <michael.rudolf01@sap.com>
This commit is contained in:
Guy Veraghtert 2011-04-04 21:31:11 +02:00 committed by Michael Rudolf
parent 23a62802c8
commit 181496ac7c
2 changed files with 79 additions and 8 deletions

View File

@ -209,14 +209,7 @@ public class CriteriaQueryCompiler implements Serializable {
"Could not convert java type [" + javaType.getName() + "] to Hibernate type" "Could not convert java type [" + javaType.getName() + "] to Hibernate type"
); );
} }
int[] sqlTypeCodes = hibernateType.sqlTypes( factory ); return hibernateType.getName();
if ( sqlTypeCodes.length != 1 ) {
throw new IllegalArgumentException(
"Invalid Hibernate Type [" + hibernateType.getName() +
"] for cast : more than one column spanned"
);
}
return factory.getDialect().getCastTypeName( sqlTypeCodes[0] );
} }
}; };

View File

@ -0,0 +1,78 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. 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 Inc.
*
* 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.ejb.criteria.basic;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.hibernate.ejb.metamodel.AbstractMetamodelSpecificTest;
import org.hibernate.ejb.metamodel.Product;
import org.hibernate.ejb.metamodel.Product_;
import org.junit.Assert;
import org.junit.Test;
public class CastTest extends AbstractMetamodelSpecificTest {
private static final int QUANTITY = 2;
private CriteriaBuilder builder;
@Override
public void buildEntityManagerFactory() throws Exception {
super.buildEntityManagerFactory();
builder = entityManagerFactory().getCriteriaBuilder();
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
Product product = new Product();
product.setId( "product1" );
product.setPrice( 1.23d );
product.setQuantity( QUANTITY );
product.setPartNumber( ((long)Integer.MAX_VALUE) + 1 );
product.setRating( 1.999f );
product.setSomeBigInteger( BigInteger.valueOf( 987654321 ) );
product.setSomeBigDecimal( BigDecimal.valueOf( 987654.321 ) );
em.persist( product );
em.getTransaction().commit();
em.close();
}
@Test
public void testCastToString() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Product> criteria = builder.createQuery( Product.class );
Root<Product> root = criteria.from( Product.class );
criteria.where( builder.equal(root.get(Product_.quantity).as(String.class), builder.literal(String.valueOf(QUANTITY))) );
List<Product> result = em.createQuery( criteria ).getResultList();
Assert.assertEquals( 1, result.size() );
em.getTransaction().commit();
em.close();
}
}