|
Hibernate example source code file (QueryBuilderTest.java)
The Hibernate QueryBuilderTest.java source code/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2009, 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; import javax.persistence.EntityManager; import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; import javax.persistence.metamodel.EntityType; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.hibernate.ejb.criteria.predicate.ComparisonPredicate; import org.hibernate.ejb.metamodel.Address; import org.hibernate.ejb.metamodel.Alias; import org.hibernate.ejb.metamodel.Country; import org.hibernate.ejb.metamodel.CreditCard; import org.hibernate.ejb.metamodel.Customer; import org.hibernate.ejb.metamodel.Info; import org.hibernate.ejb.metamodel.LineItem; import org.hibernate.ejb.metamodel.MetamodelImpl; import org.hibernate.ejb.metamodel.Order; import org.hibernate.ejb.metamodel.Phone; import org.hibernate.ejb.metamodel.Product; import org.hibernate.ejb.metamodel.ShelfLife; import org.hibernate.ejb.metamodel.Spouse; import org.hibernate.ejb.test.BaseEntityManagerFunctionalTestCase; import org.junit.Test; import static org.junit.Assert.assertEquals; /** * @author Steve Ebersole */ public class QueryBuilderTest extends BaseEntityManagerFunctionalTestCase { @Override public Class[] getAnnotatedClasses() { return new Class[] { Address.class, Alias.class, Country.class, CreditCard.class, Customer.class, Info.class, LineItem.class, Order.class, Phone.class, Product.class, ShelfLife.class, Spouse.class }; } @Test public void testEqualityComparisonLiteralConversion() { EntityManager em = getOrCreateEntityManager(); em.getTransaction().begin(); CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder(); MetamodelImpl mm = (MetamodelImpl) em.getMetamodel(); CriteriaQuery<Integer> cquery = cb.createQuery( Integer.class ); Root<Product> product = cquery.from( Product.class ); EntityType<Product> Product_ = mm.entity( Product.class ); cquery.select( cb.toInteger( product.get( Product_.getSingularAttribute("quantity", Integer.class)) ) ); ComparisonPredicate predicate = (ComparisonPredicate) cb.equal( product.get( Product_.getSingularAttribute( "partNumber", Long.class ) ), 373767373 ); assertEquals( Long.class, predicate.getRightHandOperand().getJavaType() ); cquery.where( predicate ); em.createQuery( cquery ).getResultList(); predicate = (ComparisonPredicate) cb.ge( cb.length( product.get( Product_.getSingularAttribute( "name", String.class ) ) ), 4L ); assertEquals( Integer.class, predicate.getRightHandOperand().getJavaType() ); cquery.where( predicate ); em.createQuery( cquery ).getResultList(); em.getTransaction().commit(); em.close(); } @Test public void testEqualityComparisonEntityConversion() { EntityManager em = getOrCreateEntityManager(); em.getTransaction().begin(); Address address = new Address( "Street Id", "Fake Street", "Fake City", "Fake State", "Fake Zip" ); Phone phone1 = new Phone( "1", "555", "0001", address ); Phone phone2 = new Phone( "2", "555", "0002", address ); Phone phone3 = new Phone( "3", "555", "0003", address ); Phone phone4 = new Phone( "4", "555", "0004" ); Collection<Phone> phones = new ArrayList Other Hibernate examples (source code examples)Here is a short list of links related to this Hibernate QueryBuilderTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.