HHH-7807 - key-many-to-one cascade removal tests

This commit is contained in:
Lukasz Antoniak 2013-07-29 10:47:23 +02:00 committed by Brett Meyer
parent 456dfd83f9
commit 4140661b82
9 changed files with 560 additions and 0 deletions

View File

@ -444,6 +444,7 @@ component element, etc. to an entity). -->
<!ATTLIST key-many-to-one column CDATA #IMPLIED>
<!ATTLIST key-many-to-one foreign-key CDATA #IMPLIED>
<!ATTLIST key-many-to-one lazy (false|proxy) #IMPLIED>
<!ATTLIST key-many-to-one on-delete (cascade|noaction) "noaction">
<!-- An "any" association is a polymorphic association to any table with
the given identifier type. The first listed column is a VARCHAR column

View File

@ -0,0 +1,68 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2013, 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.keymanytoone.bidir.ondelete;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
public class Customer implements Serializable {
private Long id;
private String name;
private Collection orders = new ArrayList();
public Customer() {
}
public Customer(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Collection getOrders() {
return orders;
}
public void setOrders(Collection orders) {
this.orders = orders;
}
}

View File

@ -0,0 +1,75 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2013, 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.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();
}
}

View File

@ -0,0 +1,59 @@
<?xml version="1.0"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ Copyright (c) 2013, 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
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
This mapping demonstrates the use of database-level cascade
entity removal with composite ids mapped using key-many-to-one
feature. Essentially a composite id where part
of the composition is a foreign-key to another entity.
-->
<hibernate-mapping package="org.hibernate.test.keymanytoone.bidir.ondelete">
<class name="Customer" table="CUSTOMERS">
<id name="id" column="ID" type="long">
<generator class="increment" />
</id>
<property name="name" column="NAME" type="string" />
<!-- Bidirectional relation. -->
<bag name="orders" inverse="true" lazy="false" fetch="join">
<key column="CUSTOMER_ID" on-delete="cascade" />
<one-to-many class="Order" />
</bag>
</class>
<class name="Order" table="ORDERS">
<!-- Bidirectional relation. -->
<composite-id>
<key-many-to-one name="customer" column="CUSTOMER_ID" class="Customer" lazy="false" />
<key-property name="number" column="ORDER_NUMBER" type="long" />
</composite-id>
<property name="item" column="ITEM_DESCRIPTION" type="string" />
</class>
</hibernate-mapping>

View File

@ -0,0 +1,67 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2013, 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.keymanytoone.bidir.ondelete;
import java.io.Serializable;
/**
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
public class Order implements Serializable {
private Customer customer;
private long number;
private String item;
public Order() {
}
public Order(Customer customer, long number) {
this.customer = customer;
this.number = number;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public long getNumber() {
return number;
}
public void setNumber(long number) {
this.number = number;
}
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
}

View File

@ -0,0 +1,57 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2013, 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.keymanytoone.unidir.ondelete;
import java.io.Serializable;
/**
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
public class Customer implements Serializable {
private Long id;
private String name;
public Customer() {
}
public Customer(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,75 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2013, 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.keymanytoone.unidir.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/unidir/ondelete/Mapping.hbm.xml" };
}
@Test
@TestForIssue( jiraKey = "HHH-7807" )
public void testComponentCascadeRemoval() {
Session session = openSession();
session.getTransaction().begin();
Customer customer = new Customer( "Lukasz" );
Order order1 = new Order( new Order.Id( customer, 1L ) );
order1.setItem( "laptop" );
Order order2 = new Order( new Order.Id( 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();
}
}

View File

@ -0,0 +1,54 @@
<?xml version="1.0"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ Copyright (c) 2013, 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
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
This mapping demonstrates the use of database-level cascade
entity removal with composite ids mapped using key-many-to-one
feature. Essentially a composite id where part
of the composition is a foreign-key to another entity.
-->
<hibernate-mapping package="org.hibernate.test.keymanytoone.unidir.ondelete">
<class name="Customer" table="CUSTOMERS">
<id name="id" column="ID" type="long">
<generator class="increment" />
</id>
<property name="name" column="NAME" type="string" />
</class>
<class name="Order" table="ORDERS">
<!-- Unidirectional relation. -->
<composite-id name="id" class="Order$Id">
<key-many-to-one name="customer" column="CUSTOMER_ID" class="Customer" lazy="false" on-delete="cascade" />
<key-property name="number" column="ORDER_NUMBER" type="long" />
</composite-id>
<property name="item" column="ITEM_DESCRIPTION" type="string" />
</class>
</hibernate-mapping>

View File

@ -0,0 +1,104 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2013, 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.keymanytoone.unidir.ondelete;
import java.io.Serializable;
/**
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
public class Order implements Serializable {
private Id id;
private String item;
public Order() {
}
public Order(Id id) {
this.id = id;
}
public Id getId() {
return id;
}
public void setId(Id id) {
this.id = id;
}
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public static class Id implements Serializable {
private Customer customer;
private long number;
public Id() {
}
public Id(Customer customer, long number) {
this.customer = customer;
this.number = number;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public long getNumber() {
return number;
}
public void setNumber(long number) {
this.number = number;
}
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( !( o instanceof Id ) ) {
return false;
}
Id id = ( Id ) o;
return number == id.number && customer.equals( id.customer );
}
public int hashCode() {
int result = customer.hashCode();
result = 31 * result + ( int ) ( number ^ ( number >>> 32 ) );
return result;
}
}
}