/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2006-2011, 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.test.legacy; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.criterion.Example; import org.hibernate.criterion.Restrictions; import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; /** * Query by example test to allow nested components * * @author Emmanuel Bernard */ public class QueryByExampleTest extends LegacyTestCase { @Override public String[] getMappings() { return new String[] { "legacy/Componentizable.hbm.xml" }; } @Test public void testSimpleQBE() throws Exception { deleteData(); initData(); Session s = openSession(); Transaction t = s.beginTransaction(); Componentizable master = getMaster("hibernate", "open sourc%", "open source1"); Criteria crit = s.createCriteria(Componentizable.class); Example ex = Example.create(master).enableLike(); crit.add(ex); List result = crit.list(); assertNotNull(result); assertEquals(1, result.size()); t.commit(); s.close(); } @Test public void testJunctionNotExpressionQBE() throws Exception { deleteData(); initData(); Session s = openSession(); Transaction t = s.beginTransaction(); Componentizable master = getMaster("hibernate", null, "ope%"); Criteria crit = s.createCriteria(Componentizable.class); Example ex = Example.create(master).enableLike(); crit.add(Restrictions.or(Restrictions.not(ex), ex)); List result = crit.list(); assertNotNull(result); assertEquals(2, result.size()); t.commit(); s.close(); } @Test public void testExcludingQBE() throws Exception { deleteData(); initData(); Session s = openSession(); Transaction t = s.beginTransaction(); Componentizable master = getMaster("hibernate", null, "ope%"); Criteria crit = s.createCriteria(Componentizable.class); Example ex = Example.create(master).enableLike() .excludeProperty("component.subComponent"); crit.add(ex); List result = crit.list(); assertNotNull(result); assertEquals(3, result.size()); master = getMaster("hibernate", "ORM tool", "fake stuff"); crit = s.createCriteria(Componentizable.class); ex = Example.create(master).enableLike() .excludeProperty("component.subComponent.subName1"); crit.add(ex); result = crit.list(); assertNotNull(result); assertEquals(1, result.size()); t.commit(); s.close(); } private void initData() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Componentizable master = getMaster("hibernate", "ORM tool", "ORM tool1"); s.saveOrUpdate(master); master = getMaster("hibernate", "open source", "open source1"); s.saveOrUpdate(master); master = getMaster("hibernate", null, null); s.saveOrUpdate(master); t.commit(); s.close(); } private void deleteData() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); for ( Object entity : s.createQuery( "from Componentizable" ).list() ) { s.delete( entity ); } t.commit(); s.close(); } private Componentizable getMaster(String name, String subname, String subname1) { Componentizable master = new Componentizable(); if (name != null) { Component masterComp = new Component(); masterComp.setName(name); if (subname != null || subname1 != null) { SubComponent subComponent = new SubComponent(); subComponent.setSubName(subname); subComponent.setSubName1(subname1); masterComp.setSubComponent(subComponent); } master.setComponent(masterComp); } return master; } }