[hibernate-commits] Hibernate SVN: r18573 - in core/trunk: testsuite/src/test/java/org/hibernate/test/orphan/one2one/fk and 1 other directories.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Mon Jan 18 13:27:12 EST 2010
Author: steve.ebersole at jboss.com
Date: 2010-01-18 13:27:12 -0500 (Mon, 18 Jan 2010)
New Revision: 18573
Added:
core/trunk/testsuite/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/
core/trunk/testsuite/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/DeleteOneToOneOrphansTest.java
core/trunk/testsuite/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/Employee.java
core/trunk/testsuite/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/EmployeeInfo.java
core/trunk/testsuite/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/Mapping.hbm.xml
Modified:
core/trunk/core/src/main/java/org/hibernate/cfg/HbmBinder.java
Log:
HHH-4726 - Add support for delete-orphan cascading to <one-to-one/>
Modified: core/trunk/core/src/main/java/org/hibernate/cfg/HbmBinder.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/cfg/HbmBinder.java 2010-01-18 16:32:46 UTC (rev 18572)
+++ core/trunk/core/src/main/java/org/hibernate/cfg/HbmBinder.java 2010-01-18 18:27:12 UTC (rev 18573)
@@ -1101,6 +1101,16 @@
simpleValue.addFormula( formula );
}
}
+
+ // todo : another GoodThing would be to go back after all parsing and see if all the columns
+ // (and no formulas) are contained in a defined unique key that only contains these columns.
+ // That too would mark this as a logical one-to-one
+ final Attribute uniqueAttribute = node.attribute( "unique" );
+ if ( uniqueAttribute != null
+ && "true".equals( uniqueAttribute.getValue() )
+ && ManyToOne.class.isInstance( simpleValue ) ) {
+ ( (ManyToOne) simpleValue ).markAsLogicalOneToOne();
+ }
}
else {
if ( node.elementIterator( "column" ).hasNext() ) {
Copied: core/trunk/testsuite/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/DeleteOneToOneOrphansTest.java (from rev 18568, core/trunk/testsuite/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/unidirectional/DeleteOneToOneOrphansTest.java)
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/DeleteOneToOneOrphansTest.java (rev 0)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/DeleteOneToOneOrphansTest.java 2010-01-18 18:27:12 UTC (rev 18573)
@@ -0,0 +1,92 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, 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.orphan.one2one.fk.composite;
+
+import java.util.List;
+
+import org.hibernate.Session;
+import org.hibernate.junit.functional.FunctionalTestCase;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+public class DeleteOneToOneOrphansTest extends FunctionalTestCase {
+ public DeleteOneToOneOrphansTest(String string) {
+ super( string );
+ }
+
+ public String[] getMappings() {
+ return new String[] { "orphan/one2one/fk/composite/Mapping.hbm.xml" };
+ }
+
+ private void createData() {
+ Session session = openSession();
+ session.beginTransaction();
+ Employee emp = new Employee();
+ emp.setInfo( new EmployeeInfo( 1L, 1L) );
+ session.save( emp );
+ session.getTransaction().commit();
+ session.close();
+ }
+
+ private void cleanupData() {
+ Session session = openSession();
+ session.beginTransaction();
+ session.createQuery( "delete EmployeeInfo" ).executeUpdate();
+ session.createQuery( "delete Employee" ).executeUpdate();
+ session.getTransaction().commit();
+ session.close();
+ }
+
+ public void testOrphanedWhileManaged() {
+ createData();
+
+ Session session = openSession();
+ session.beginTransaction();
+ List results = session.createQuery( "from EmployeeInfo" ).list();
+ assertEquals( 1, results.size() );
+ results = session.createQuery( "from Employee" ).list();
+ assertEquals( 1, results.size() );
+ Employee emp = ( Employee ) results.get( 0 );
+ assertNotNull( emp.getInfo() );
+ emp.setInfo( null );
+ session.getTransaction().commit();
+ session.close();
+
+ session = openSession();
+ session.beginTransaction();
+ emp = ( Employee ) session.get( Employee.class, emp.getId() );
+ assertNull( emp.getInfo() );
+ results = session.createQuery( "from EmployeeInfo" ).list();
+ assertEquals( 0, results.size() );
+ results = session.createQuery( "from Employee" ).list();
+ assertEquals( 1, results.size() );
+ session.getTransaction().commit();
+ session.close();
+
+ cleanupData();
+ }
+}
\ No newline at end of file
Copied: core/trunk/testsuite/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/Employee.java (from rev 18560, core/trunk/testsuite/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/Employee.java)
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/Employee.java (rev 0)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/Employee.java 2010-01-18 18:27:12 UTC (rev 18573)
@@ -0,0 +1,50 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, 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.orphan.one2one.fk.composite;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+public class Employee {
+ private Long id;
+ private EmployeeInfo info;
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public EmployeeInfo getInfo() {
+ return info;
+ }
+
+ public void setInfo(EmployeeInfo info) {
+ this.info = info;
+ }
+}
\ No newline at end of file
Copied: core/trunk/testsuite/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/EmployeeInfo.java (from rev 18560, core/trunk/testsuite/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/EmployeeInfo.java)
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/EmployeeInfo.java (rev 0)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/EmployeeInfo.java 2010-01-18 18:27:12 UTC (rev 18573)
@@ -0,0 +1,106 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, 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.orphan.one2one.fk.composite;
+
+import java.io.Serializable;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+public class EmployeeInfo {
+ public static class Id implements Serializable {
+ private Long companyId;
+ private Long personId;
+
+ public Id() {
+ }
+
+ public Id(Long companyId, Long personId) {
+ this.companyId = companyId;
+ this.personId = personId;
+ }
+
+ public Long getCompanyId() {
+ return companyId;
+ }
+
+ public void setCompanyId(Long companyId) {
+ this.companyId = companyId;
+ }
+
+ public Long getPersonId() {
+ return personId;
+ }
+
+ public void setPersonId(Long personId) {
+ this.personId = personId;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if ( this == o ) {
+ return true;
+ }
+ if ( o == null || getClass() != o.getClass() ) {
+ return false;
+ }
+
+ Id id = (Id) o;
+
+ return companyId.equals( id.companyId )
+ && personId.equals( id.personId );
+
+ }
+
+ @Override
+ public int hashCode() {
+ int result = companyId.hashCode();
+ result = 31 * result + personId.hashCode();
+ return result;
+ }
+ }
+
+ private Id id;
+
+ public EmployeeInfo() {
+ }
+
+ public EmployeeInfo(Long companyId, Long personId) {
+ this( new Id( companyId, personId ) );
+ }
+
+ public EmployeeInfo(Id id) {
+ this.id = id;
+ }
+
+ public Id getId() {
+ return id;
+ }
+
+ public void setId(Id id) {
+ this.id = id;
+ }
+}
\ No newline at end of file
Copied: core/trunk/testsuite/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/Mapping.hbm.xml (from rev 18560, core/trunk/testsuite/src/test/java/org/hibernate/test/orphan/one2one/fk/bidirectional/Mapping.hbm.xml)
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/Mapping.hbm.xml (rev 0)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/orphan/one2one/fk/composite/Mapping.hbm.xml 2010-01-18 18:27:12 UTC (rev 18573)
@@ -0,0 +1,49 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Hibernate, Relational Persistence for Idiomatic Java
+ ~
+ ~ Copyright (c) 2010, 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://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd'>
+
+<hibernate-mapping package="org.hibernate.test.orphan.one2one.fk.composite" >
+
+ <class name="Employee">
+ <id name="id" type="long" column="id">
+ <generator class="increment" />
+ </id>
+ <many-to-one name="info" unique="true" cascade="all,delete-orphan">
+ <column name="COMP_ID" />
+ <column name="PERS_ID" />
+ </many-to-one>
+ </class>
+
+ <class name="EmployeeInfo">
+ <composite-id class="EmployeeInfo$Id" name="id">
+ <key-property name="companyId" column="COMP_ID" />
+ <key-property name="personId" column="PERS_ID" />
+ </composite-id>
+ </class>
+
+</hibernate-mapping>
More information about the hibernate-commits
mailing list