Author: gbadner
Date: 2007-08-14 15:45:56 -0400 (Tue, 14 Aug 2007)
New Revision: 12936
Added:
core/branches/Branch_3_2/test/org/hibernate/test/component/basic/OptionalComponent.java
Modified:
core/branches/Branch_3_2/src/org/hibernate/type/TypeFactory.java
core/branches/Branch_3_2/test/org/hibernate/test/component/basic/ComponentTest.java
core/branches/Branch_3_2/test/org/hibernate/test/component/basic/Employee.java
core/branches/Branch_3_2/test/org/hibernate/test/component/basic/User.hbm.xml
Log:
HHH-2542 : Merge non-null component in a child persisted with a null component and added
test for optional components
Modified: core/branches/Branch_3_2/src/org/hibernate/type/TypeFactory.java
===================================================================
--- core/branches/Branch_3_2/src/org/hibernate/type/TypeFactory.java 2007-08-14 17:19:22
UTC (rev 12935)
+++ core/branches/Branch_3_2/src/org/hibernate/type/TypeFactory.java 2007-08-14 19:45:56
UTC (rev 12936)
@@ -553,7 +553,7 @@
AbstractComponentType componentType = ( AbstractComponentType ) types[i];
Type[] subtypes = componentType.getSubtypes();
Object[] origComponentValues = original[i] == null ? new Object[subtypes.length] :
componentType.getPropertyValues( original[i], session );
- Object[] targetComponentValues = componentType.getPropertyValues( target[i], session
);
+ Object[] targetComponentValues = target[i] == null ? new Object[subtypes.length] :
componentType.getPropertyValues( target[i], session );
replaceAssociations( origComponentValues, targetComponentValues, subtypes, session,
null, copyCache, foreignKeyDirection );
copied[i] = target[i];
}
Modified:
core/branches/Branch_3_2/test/org/hibernate/test/component/basic/ComponentTest.java
===================================================================
---
core/branches/Branch_3_2/test/org/hibernate/test/component/basic/ComponentTest.java 2007-08-14
17:19:22 UTC (rev 12935)
+++
core/branches/Branch_3_2/test/org/hibernate/test/component/basic/ComponentTest.java 2007-08-14
19:45:56 UTC (rev 12936)
@@ -9,6 +9,7 @@
import org.hibernate.Session;
import org.hibernate.Transaction;
+import org.hibernate.Hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.cfg.Mappings;
@@ -190,5 +191,131 @@
s.close();
}
+ public void testMergeComponent() {
+ Session s = openSession();
+ Transaction t = s.beginTransaction();
+ Employee emp = new Employee();
+ emp.setHireDate( new Date() );
+ emp.setPerson( new Person() );
+ emp.getPerson().setName( "steve" );
+ emp.getPerson().setDob( new Date() );
+ s.persist( emp );
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ emp = (Employee)s.get( Employee.class, emp.getId() );
+ t.commit();
+ s.close();
+
+ assertNull(emp.getOptionalComponent());
+ emp.setOptionalComponent( new OptionalComponent() );
+ emp.getOptionalComponent().setValue1( "emp-value1" );
+ emp.getOptionalComponent().setValue2( "emp-value2" );
+
+ s = openSession();
+ t = s.beginTransaction();
+ emp = (Employee)s.merge( emp );
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ emp = (Employee)s.get( Employee.class, emp.getId() );
+ t.commit();
+ s.close();
+
+ assertEquals("emp-value1", emp.getOptionalComponent().getValue1());
+ assertEquals("emp-value2", emp.getOptionalComponent().getValue2());
+ emp.getOptionalComponent().setValue1( null );
+ emp.getOptionalComponent().setValue2( null );
+
+ s = openSession();
+ t = s.beginTransaction();
+ emp = (Employee)s.merge( emp );
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ emp = (Employee)s.get( Employee.class, emp.getId() );
+ Hibernate.initialize(emp.getDirectReports());
+ t.commit();
+ s.close();
+
+ assertNull(emp.getOptionalComponent());
+
+ Employee emp1 = new Employee();
+ emp1.setHireDate( new Date() );
+ emp1.setPerson( new Person() );
+ emp1.getPerson().setName( "bozo" );
+ emp1.getPerson().setDob( new Date() );
+ emp.getDirectReports().add( emp1 );
+
+ s = openSession();
+ t = s.beginTransaction();
+ emp = (Employee)s.merge( emp );
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ emp = (Employee)s.get( Employee.class, emp.getId() );
+ Hibernate.initialize(emp.getDirectReports());
+ t.commit();
+ s.close();
+
+ assertEquals(1, emp.getDirectReports().size());
+ emp1 = (Employee)emp.getDirectReports().iterator().next();
+ assertNull( emp1.getOptionalComponent() );
+ emp1.setOptionalComponent( new OptionalComponent() );
+ emp1.getOptionalComponent().setValue1( "emp1-value1" );
+ emp1.getOptionalComponent().setValue2( "emp1-value2" );
+
+ s = openSession();
+ t = s.beginTransaction();
+ emp = (Employee)s.merge( emp );
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ emp = (Employee)s.get( Employee.class, emp.getId() );
+ Hibernate.initialize(emp.getDirectReports());
+ t.commit();
+ s.close();
+
+ assertEquals(1, emp.getDirectReports().size());
+ emp1 = (Employee)emp.getDirectReports().iterator().next();
+ assertEquals( "emp1-value1", emp1.getOptionalComponent().getValue1());
+ assertEquals( "emp1-value2", emp1.getOptionalComponent().getValue2());
+ emp1.getOptionalComponent().setValue1( null );
+ emp1.getOptionalComponent().setValue2( null );
+
+ s = openSession();
+ t = s.beginTransaction();
+ emp = (Employee)s.merge( emp );
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ emp = (Employee)s.get( Employee.class, emp.getId() );
+ Hibernate.initialize(emp.getDirectReports());
+ t.commit();
+ s.close();
+
+ assertEquals(1, emp.getDirectReports().size());
+ emp1 = (Employee)emp.getDirectReports().iterator().next();
+ assertNull(emp1.getOptionalComponent());
+
+ s = openSession();
+ t = s.beginTransaction();
+ s.delete( emp );
+ t.commit();
+ s.close();
+ }
+
}
Modified: core/branches/Branch_3_2/test/org/hibernate/test/component/basic/Employee.java
===================================================================
---
core/branches/Branch_3_2/test/org/hibernate/test/component/basic/Employee.java 2007-08-14
17:19:22 UTC (rev 12935)
+++
core/branches/Branch_3_2/test/org/hibernate/test/component/basic/Employee.java 2007-08-14
19:45:56 UTC (rev 12936)
@@ -1,6 +1,8 @@
package org.hibernate.test.component.basic;
import java.util.Date;
+import java.util.HashSet;
+import java.util.Set;
/**
* todo: describe Employee
@@ -11,6 +13,8 @@
private Long id;
private Person person;
private Date hireDate;
+ private OptionalComponent optionalComponent;
+ private Set directReports = new HashSet();
public Long getId() {
return id;
@@ -35,4 +39,20 @@
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
+
+ public OptionalComponent getOptionalComponent() {
+ return optionalComponent;
+ }
+
+ public void setOptionalComponent(OptionalComponent optionalComponent) {
+ this.optionalComponent = optionalComponent;
+ }
+
+ public Set getDirectReports() {
+ return directReports;
+ }
+
+ public void setDirectReports(Set directReports) {
+ this.directReports = directReports;
+ }
}
Added:
core/branches/Branch_3_2/test/org/hibernate/test/component/basic/OptionalComponent.java
===================================================================
---
core/branches/Branch_3_2/test/org/hibernate/test/component/basic/OptionalComponent.java
(rev 0)
+++
core/branches/Branch_3_2/test/org/hibernate/test/component/basic/OptionalComponent.java 2007-08-14
19:45:56 UTC (rev 12936)
@@ -0,0 +1,27 @@
+//$Id: $
+package org.hibernate.test.component.basic;
+
+/**
+ * @author Gail Badner
+ */
+
+public class OptionalComponent {
+ private String value1;
+ private String value2;
+
+ public String getValue1() {
+ return value1;
+ }
+
+ public void setValue1(String value1) {
+ this.value1 = value1;
+ }
+
+ public String getValue2() {
+ return value2;
+ }
+
+ public void setValue2(String value2) {
+ this.value2 = value2;
+ }
+}
Modified: core/branches/Branch_3_2/test/org/hibernate/test/component/basic/User.hbm.xml
===================================================================
---
core/branches/Branch_3_2/test/org/hibernate/test/component/basic/User.hbm.xml 2007-08-14
17:19:22 UTC (rev 12935)
+++
core/branches/Branch_3_2/test/org/hibernate/test/component/basic/User.hbm.xml 2007-08-14
19:45:56 UTC (rev 12936)
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
-<!DOCTYPE hibernate-mapping PUBLIC
+<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
@@ -8,7 +8,7 @@
-->
<hibernate-mapping package="org.hibernate.test.component.basic">
-
+
<class name="User" table="T_USER">
<id name="userName"/>
<timestamp name="lastModified"/>
@@ -19,9 +19,9 @@
<property name="address"/>
<property name="previousAddress" insert="false"/>
<property name="yob" formula="year(dob)"/>
- <property name="currentAddress"
- column="address"
- insert="false"
+ <property name="currentAddress"
+ column="address"
+ insert="false"
update="false"/>
</component>
</class>
@@ -35,8 +35,16 @@
<property name="name" update="false"
not-null="true"/>
<property name="dob" update="false"
not-null="true"/>
</component>
+ <component name="optionalComponent">
+ <property name="value1" not-null="false"/>
+ <property name="value2" not-null="false"/>
+ </component>
+ <set name="directReports" cascade="all-delete-orphan,merge"
lazy="true">
+ <key column="PARENT_ID" />
+ <one-to-many class="Employee"/>
+ </set>
</class>
-
+
<query name="userNameIn"><![CDATA[from User where person.name in
(:nameList) or userName in (:nameList)]]></query>
-
+
</hibernate-mapping>