/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008-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.filter.hql;
import java.util.Date;
import org.hibernate.Session;
import org.junit.Test;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
/**
* @author Steve Ebersole
*/
public class JoinedFilteredBulkManipulationTest extends BaseCoreFunctionalTestCase {
public String[] getMappings() {
return new String[] {
"filter/hql/filter-defs.hbm.xml",
"filter/hql/Joined.hbm.xml"
};
}
@Test
@SuppressWarnings( {"UnnecessaryBoxing"})
public void testFilteredJoinedSubclassHqlDeleteRoot() {
Session s = openSession();
s.beginTransaction();
s.save( new Employee( "John", 'M', "john", new Date() ) );
s.save( new Employee( "Jane", 'F', "jane", new Date() ) );
s.save( new Customer( "Charlie", 'M', "charlie", "Acme" ) );
s.save( new Customer( "Wanda", 'F', "wanda", "ABC" ) );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.enableFilter( "sex" ).setParameter( "sexCode", Character.valueOf( 'M' ) );
int count = s.createQuery( "delete Person" ).executeUpdate();
assertEquals( 2, count );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.createQuery( "delete Person" ).executeUpdate();
s.getTransaction().commit();
s.close();
}
@Test
@SuppressWarnings( {"UnnecessaryBoxing"})
public void testFilteredJoinedSubclassHqlDeleteNonLeaf() {
Session s = openSession();
s.beginTransaction();
s.save( new Employee( "John", 'M', "john", new Date() ) );
s.save( new Employee( "Jane", 'F', "jane", new Date() ) );
s.save( new Customer( "Charlie", 'M', "charlie", "Acme" ) );
s.save( new Customer( "Wanda", 'F', "wanda", "ABC" ) );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.enableFilter( "sex" ).setParameter( "sexCode", Character.valueOf( 'M' ) );
int count = s.createQuery( "delete User" ).executeUpdate();
assertEquals( 2, count );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.createQuery( "delete Person" ).executeUpdate();
s.getTransaction().commit();
s.close();
}
@Test
@SuppressWarnings( {"UnnecessaryBoxing"})
public void testFilteredJoinedSubclassHqlDeleteLeaf() {
Session s = openSession();
s.beginTransaction();
s.save( new Employee( "John", 'M', "john", new Date() ) );
s.save( new Employee( "Jane", 'F', "jane", new Date() ) );
s.save( new Customer( "Charlie", 'M', "charlie", "Acme" ) );
s.save( new Customer( "Wanda", 'F', "wanda", "ABC" ) );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.enableFilter( "sex" ).setParameter( "sexCode", Character.valueOf( 'M' ) );
int count = s.createQuery( "delete Employee" ).executeUpdate();
assertEquals( 1, count );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.createQuery( "delete Person" ).executeUpdate();
s.getTransaction().commit();
s.close();
}
@Test
@SuppressWarnings( {"UnnecessaryBoxing"})
public void testFilteredJoinedSubclassHqlUpdateRoot() {
Session s = openSession();
s.beginTransaction();
s.save( new Employee( "John", 'M', "john", new Date() ) );
s.save( new Employee( "Jane", 'F', "jane", new Date() ) );
s.save( new Customer( "Charlie", 'M', "charlie", "Acme" ) );
s.save( new Customer( "Wanda", 'F', "wanda", "ABC" ) );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.enableFilter( "sex" ).setParameter( "sexCode", Character.valueOf( 'M' ) );
int count = s.createQuery( "update Person p set p.name = '<male>'" ).executeUpdate();
assertEquals( 2, count );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.createQuery( "delete Person" ).executeUpdate();
s.getTransaction().commit();
s.close();
}
@Test
@SuppressWarnings( {"UnnecessaryBoxing"})
public void testFilteredJoinedSubclassHqlUpdateNonLeaf() {
Session s = openSession();
s.beginTransaction();
s.save( new Employee( "John", 'M', "john", new Date() ) );
s.save( new Employee( "Jane", 'F', "jane", new Date() ) );
s.save( new Customer( "Charlie", 'M', "charlie", "Acme" ) );
s.save( new Customer( "Wanda", 'F', "wanda", "ABC" ) );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.enableFilter( "sex" ).setParameter( "sexCode", Character.valueOf( 'M' ) );
int count = s.createQuery( "update User u set u.username = :un where u.name = :n" )
.setString( "un", "charlie" )
.setString( "n", "Wanda" )
.executeUpdate();
assertEquals( 0, count );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.createQuery( "delete Person" ).executeUpdate();
s.getTransaction().commit();
s.close();
}
@Test
@SuppressWarnings( {"UnnecessaryBoxing"})
public void testFilteredJoinedSubclassHqlUpdateLeaf() {
Session s = openSession();
s.beginTransaction();
s.save( new Employee( "John", 'M', "john", new Date() ) );
s.save( new Employee( "Jane", 'F', "jane", new Date() ) );
s.save( new Customer( "Charlie", 'M', "charlie", "Acme" ) );
s.save( new Customer( "Wanda", 'F', "wanda", "ABC" ) );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.enableFilter( "sex" ).setParameter( "sexCode", Character.valueOf( 'M' ) );
int count = s.createQuery( "update Customer c set c.company = 'XYZ'" ).executeUpdate();
assertEquals( 1, count );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.createQuery( "delete Person" ).executeUpdate();
s.getTransaction().commit();
s.close();
}
}