/* * 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.map; import java.util.List; import java.util.Map; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import static org.junit.Assert.assertEquals; /** * @author Gavin King */ public class MapIndexFormulaTest extends BaseCoreFunctionalTestCase { public String[] getMappings() { return new String[] { "map/UserGroup.hbm.xml" }; } @Test public void testIndexFunctionOnManyToManyMap() { Session s = openSession(); s.beginTransaction(); s.createQuery( "from Group g join g.users u where g.name = 'something' and index(u) = 'nada'" ) .list(); s.createQuery( "from Group g join g.users u where g.name = 'something' and minindex(u) = 'nada'" ) .list(); s.createQuery( "from Group g join g.users u where g.name = 'something' and maxindex(u) = 'nada'" ) .list(); s.getTransaction().commit(); s.close(); } @Test @SuppressWarnings( {"unchecked", "UnnecessaryBoxing"}) public void testIndexFormulaMap() { Session s = openSession(); Transaction t = s.beginTransaction(); User gavin = new User("gavin", "secret"); User turin = new User("turin", "tiger"); Group g = new Group("developers"); g.getUsers().put("gavin", gavin); g.getUsers().put("turin", turin); s.persist(g); gavin.getSession().put( "foo", new SessionAttribute("foo", "foo bar baz") ); gavin.getSession().put( "bar", new SessionAttribute("bar", "foo bar baz 2") ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); g = (Group) s.get(Group.class, "developers"); assertEquals( g.getUsers().size(), 2 ); g.getUsers().remove("turin"); Map smap = ( (User) g.getUsers().get("gavin") ).getSession(); assertEquals(smap.size(), 2); smap.remove("bar"); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); g = (Group) s.get(Group.class, "developers"); assertEquals( g.getUsers().size(), 1 ); smap = ( (User) g.getUsers().get("gavin") ).getSession(); assertEquals(smap.size(), 1); gavin = (User) g.getUsers().put("gavin", turin); s.delete(gavin); assertEquals( s.createQuery("select count(*) from SessionAttribute").uniqueResult(), new Long(0) ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); g = (Group) s.get(Group.class, "developers"); assertEquals( g.getUsers().size(), 1 ); turin = (User) g.getUsers().get("turin"); smap = turin.getSession(); assertEquals(smap.size(), 0); assertEquals( s.createQuery("select count(*) from User").uniqueResult(), Long.valueOf(1) ); s.delete(g); s.delete(turin); assertEquals( s.createQuery("select count(*) from User").uniqueResult(), Long.valueOf( 0 ) ); t.commit(); s.close(); } @Test @SuppressWarnings( {"unchecked"}) public void testSQLQuery() { Session s = openSession(); Transaction t = s.beginTransaction(); User gavin = new User("gavin", "secret"); User turin = new User("turin", "tiger"); gavin.getSession().put( "foo", new SessionAttribute("foo", "foo bar baz") ); gavin.getSession().put( "bar", new SessionAttribute("bar", "foo bar baz 2") ); s.persist(gavin); s.persist(turin); s.flush(); s.clear(); List results = s.getNamedQuery("userSessionData").setParameter("uname", "%in").list(); assertEquals( results.size(), 2 ); gavin = (User) ( (Object[]) results.get(0) )[0]; assertEquals( gavin.getName(), "gavin" ); assertEquals( gavin.getSession().size(), 2 ); s.createQuery("delete SessionAttribute").executeUpdate(); s.createQuery("delete User").executeUpdate(); t.commit(); s.close(); } }