Re-enabled additional tests

This commit is contained in:
Andrea Boriero 2021-06-28 13:04:19 +02:00
parent 911173d6eb
commit ce752c1428
10 changed files with 217 additions and 188 deletions

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.keymanytoone.bidir.embedded;
package org.hibernate.orm.test.keymanytoone.bidir.embedded;
import java.util.ArrayList;
import java.util.Collection;

View File

@ -0,0 +1,136 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.keymanytoone.bidir.embedded;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Root;
import org.hibernate.stat.spi.StatisticsImplementor;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
/**
* @author Steve Ebersole
*/
@DomainModel(
xmlMappings = "org/hibernate/orm/test/keymanytoone/bidir/embedded/Mapping.hbm.xml"
)
@SessionFactory(
generateStatistics = true
)
public class KeyManyToOneTest {
@AfterEach
public void tearDown(SessionFactoryScope scope){
scope.inTransaction(
session -> {
session.createQuery( "delete from Order" ).executeUpdate();
session.createQuery( "delete from Customer" ).executeUpdate();
}
);
}
@Test
public void testCriteriaRestrictionOnKeyManyToOne(SessionFactoryScope scope) {
scope.inTransaction( s -> {
s.createQuery( "from Order o where o.customer.name = 'Acme'" ).list();
// Criteria criteria = s.createCriteria( Order.class );
// criteria.createCriteria( "customer" ).add( Restrictions.eq( "name", "Acme" ) );
// criteria.list();
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Order> criteria = criteriaBuilder.createQuery( Order.class );
Root<Order> root = criteria.from( Order.class );
Join<Object, Object> customer = root.join( "customer", JoinType.INNER );
criteria.where( criteriaBuilder.equal( customer.get( "name" ), "Acme" ) );
s.createQuery( criteria ).list();
} );
}
@Test
public void testSaveCascadedToKeyManyToOne(SessionFactoryScope scope) {
// test cascading a save to an association with a key-many-to-one which refers to a
// just saved entity
scope.inTransaction( s -> {
Customer cust = new Customer( "Acme, Inc." );
Order order = new Order( cust, 1 );
cust.getOrders().add( order );
getStatistics( scope ).clear();
s.save( cust );
s.flush();
assertThat( getStatistics( scope ).getEntityInsertCount(), is( 2L ) );
s.delete( cust );
} );
}
@Test
public void testQueryingOnMany2One(SessionFactoryScope scope) {
Customer cust = new Customer( "Acme, Inc." );
Order order = new Order( cust, 1 );
scope.inTransaction( s -> {
cust.getOrders().add( order );
s.save( cust );
} );
scope.inTransaction( s -> {
List results = s.createQuery( "from Order o where o.customer.name = :name" )
.setParameter( "name", cust.getName() )
.list();
assertThat( results.size(), is( 1 ) );
} );
scope.inTransaction( s -> {
s.delete( cust );
} );
}
@Test
public void testLoadingStrategies(SessionFactoryScope scope) {
Customer cusotmer = scope.fromTransaction(
session -> {
Customer cust = new Customer( "Acme, Inc." );
Order order = new Order( cust, 1 );
cust.getOrders().add( order );
session.save( cust );
return cust;
}
);
scope.inTransaction(
session -> {
Customer cust = session.get( Customer.class, cusotmer.getId() );
assertThat( cust.getOrders().size(), is( 1 ) );
session.clear();
cust = (Customer) session.createQuery( "from Customer" ).uniqueResult();
assertThat( cust.getOrders().size(), is( 1 ) );
session.clear();
cust = (Customer) session.createQuery( "from Customer c join fetch c.orders" ).uniqueResult();
assertThat( cust.getOrders().size(), is( 1 ) );
session.clear();
session.delete( cust );
}
);
}
private StatisticsImplementor getStatistics(SessionFactoryScope scope) {
return scope.getSessionFactory().getStatistics();
}
}

View File

@ -16,7 +16,7 @@
foreign-key to another entity.
-->
<hibernate-mapping package="org.hibernate.test.keymanytoone.bidir.embedded">
<hibernate-mapping package="org.hibernate.orm.test.keymanytoone.bidir.embedded">
<class name="Customer" table="EMBD_KM2O_CUST">
<id name="id" column="ID" type="long">

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.keymanytoone.bidir.embedded;
package org.hibernate.orm.test.keymanytoone.bidir.embedded;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.keymanytoone.bidir.ondelete;
package org.hibernate.orm.test.keymanytoone.bidir.ondelete;
import java.io.Serializable;
import java.util.ArrayList;

View File

@ -0,0 +1,75 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.keymanytoone.bidir.ondelete;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
/**
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsCascadeDeleteCheck.class)
@DomainModel(
xmlMappings = "org/hibernate/orm/test/keymanytoone/bidir/ondelete/Mapping.hbm.xml"
)
@SessionFactory
public class KeyManyToOneCascadeDeleteTest {
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete from Order" ).executeUpdate();
session.createQuery( "delete from Customer" ).executeUpdate();
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-7807")
public void testEmbeddedCascadeRemoval(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Customer customer = new Customer( "Lukasz" );
Order order1 = new Order( customer, 1L );
order1.setItem( "laptop" );
Order order2 = new Order( customer, 2L );
order2.setItem( "printer" );
session.save( customer );
session.save( order1 );
session.save( order2 );
session.getTransaction().commit();
// Removing customer cascades to associated orders.
session.getTransaction().begin();
customer = session.get( Customer.class, customer.getId() );
session.delete( customer );
session.getTransaction().commit();
session.getTransaction().begin();
assertThat(
session.createQuery( "select count(*) from Customer" ).uniqueResult(),
is( 0L )
);
assertThat(
session.createQuery( "select count(*) from Order" ).uniqueResult(),
is( 0L )
);
}
);
}
}

View File

@ -16,7 +16,7 @@
of the composition is a foreign-key to another entity.
-->
<hibernate-mapping package="org.hibernate.test.keymanytoone.bidir.ondelete">
<hibernate-mapping package="org.hibernate.orm.test.keymanytoone.bidir.ondelete">
<class name="Customer" table="CUSTOMERS">
<id name="id" column="ID" type="long">

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.keymanytoone.bidir.ondelete;
package org.hibernate.orm.test.keymanytoone.bidir.ondelete;
import java.io.Serializable;

View File

@ -1,124 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.keymanytoone.bidir.embedded;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Root;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
/**
* @author Steve Ebersole
*/
public class KeyManyToOneTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {
return new String[] { "keymanytoone/bidir/embedded/Mapping.hbm.xml" };
}
@Override
public void configure(Configuration cfg) {
super.configure( cfg );
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
}
@Test
public void testCriteriaRestrictionOnKeyManyToOne() {
inTransaction( s -> {
s.createQuery( "from Order o where o.customer.name = 'Acme'" ).list();
// Criteria criteria = s.createCriteria( Order.class );
// criteria.createCriteria( "customer" ).add( Restrictions.eq( "name", "Acme" ) );
// criteria.list();
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Order> criteria = criteriaBuilder.createQuery( Order.class );
Root<Order> root = criteria.from( Order.class );
Join<Object, Object> customer = root.join( "customer", JoinType.INNER );
criteria.where( criteriaBuilder.equal( customer.get( "name" ), "Acme" ) );
s.createQuery( criteria ).list();
} );
}
@Test
public void testSaveCascadedToKeyManyToOne() {
// test cascading a save to an association with a key-many-to-one which refers to a
// just saved entity
inTransaction( s -> {
Customer cust = new Customer( "Acme, Inc." );
Order order = new Order( cust, 1 );
cust.getOrders().add( order );
sessionFactory().getStatistics().clear();
s.save( cust );
s.flush();
assertEquals( 2, sessionFactory().getStatistics().getEntityInsertCount() );
s.delete( cust );
} );
}
@Test
public void testQueryingOnMany2One() {
Customer cust = new Customer( "Acme, Inc." );
Order order = new Order( cust, 1 );
inTransaction( s -> {
cust.getOrders().add( order );
s.save( cust );
} );
inTransaction( s -> {
List results = s.createQuery( "from Order o where o.customer.name = :name" )
.setParameter( "name", cust.getName() )
.list();
assertEquals( 1, results.size() );
} );
inTransaction( s -> {
s.delete( cust );
} );
}
@Test
public void testLoadingStrategies() {
Session s = openSession();
s.beginTransaction();
Customer cust = new Customer( "Acme, Inc." );
Order order = new Order( cust, 1 );
cust.getOrders().add( order );
s.save( cust );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
cust = ( Customer ) s.get( Customer.class, cust.getId() );
assertEquals( 1, cust.getOrders().size() );
s.clear();
cust = ( Customer ) s.createQuery( "from Customer" ).uniqueResult();
assertEquals( 1, cust.getOrders().size() );
s.clear();
cust = ( Customer ) s.createQuery( "from Customer c join fetch c.orders" ).uniqueResult();
assertEquals( 1, cust.getOrders().size() );
s.clear();
s.delete( cust );
s.getTransaction().commit();
s.close();
}
}

View File

@ -1,58 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.keymanytoone.bidir.ondelete;
import org.hibernate.Session;
import org.junit.Assert;
import org.junit.Test;
import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
/**
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
@RequiresDialectFeature(DialectChecks.SupportsCascadeDeleteCheck.class)
public class KeyManyToOneCascadeDeleteTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {
return new String[] { "keymanytoone/bidir/ondelete/Mapping.hbm.xml" };
}
@Test
@TestForIssue( jiraKey = "HHH-7807" )
public void testEmbeddedCascadeRemoval() {
Session session = openSession();
session.getTransaction().begin();
Customer customer = new Customer( "Lukasz" );
Order order1 = new Order( customer, 1L );
order1.setItem( "laptop" );
Order order2 = new Order( customer, 2L );
order2.setItem( "printer" );
session.save( customer );
session.save( order1 );
session.save( order2 );
session.getTransaction().commit();
// Removing customer cascades to associated orders.
session.getTransaction().begin();
customer = (Customer) session.get( Customer.class, customer.getId() );
session.delete( customer );
session.getTransaction().commit();
session.getTransaction().begin();
Assert.assertEquals( "0", session.createQuery( "select count(*) from Customer" ).uniqueResult().toString() );
Assert.assertEquals( "0", session.createQuery( "select count(*) from Order" ).uniqueResult().toString() );
session.getTransaction().commit();
session.close();
}
}