Author: gbadner
Date: 2007-08-15 16:23:35 -0400 (Wed, 15 Aug 2007)
New Revision: 12946
Added:
core/trunk/testsuite/src/test/java/org/hibernate/test/join/JoinSuite.java
core/trunk/testsuite/src/test/java/org/hibernate/test/join/OptionalJoinTest.java
core/trunk/testsuite/src/test/java/org/hibernate/test/join/Thing.hbm.xml
core/trunk/testsuite/src/test/java/org/hibernate/test/join/Thing.java
Modified:
core/trunk/core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java
Log:
HHH-2320 : Update detached entity with null joined properties changed to non-null
Modified:
core/trunk/core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java
===================================================================
---
core/trunk/core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java 2007-08-15
20:21:22 UTC (rev 12945)
+++
core/trunk/core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java 2007-08-15
20:23:35 UTC (rev 12946)
@@ -1764,6 +1764,7 @@
}
throw new StaleObjectStateException( getEntityName(), id );
}
+ return false;
}
catch( TooManyRowsAffectedException e ) {
throw new HibernateException(
Added: core/trunk/testsuite/src/test/java/org/hibernate/test/join/JoinSuite.java
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/join/JoinSuite.java
(rev 0)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/join/JoinSuite.java 2007-08-15
20:23:35 UTC (rev 12946)
@@ -0,0 +1,22 @@
+//$Id: $
+package org.hibernate.test.join;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * @author Gail Badner
+ */
+
+/**
+ * Implementation of JoinSuite.
+ */
+public class JoinSuite {
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite( "Join tests");
+ suite.addTest( JoinTest.suite() );
+ suite.addTest( OptionalJoinTest.suite() );
+ return suite;
+ }
+}
Added: core/trunk/testsuite/src/test/java/org/hibernate/test/join/OptionalJoinTest.java
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/join/OptionalJoinTest.java
(rev 0)
+++
core/trunk/testsuite/src/test/java/org/hibernate/test/join/OptionalJoinTest.java 2007-08-15
20:23:35 UTC (rev 12946)
@@ -0,0 +1,369 @@
+//$Id: $
+package org.hibernate.test.join;
+
+/**
+ * @author Chris Jones and Gail Badner
+ */
+import org.hibernate.junit.functional.FunctionalTestCase;
+import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import junit.framework.Test;
+
+import java.sql.ResultSet;
+import java.util.List;
+
+public class OptionalJoinTest extends FunctionalTestCase {
+
+ public OptionalJoinTest(String name) {
+ super(name);
+ }
+
+ public static Test suite() {
+ return new FunctionalTestClassTestSuite( OptionalJoinTest.class );
+ }
+
+ public String[] getMappings() {
+ return new String[] { "join/Thing.hbm.xml" };
+ }
+
+ public void testUpdateNonNullOptionalJoinToDiffNonNull() throws Exception {
+ Session s = openSession();
+ Transaction t = s.beginTransaction();
+
+ // create a new thing with a non-null name
+ Thing thing = new Thing();
+ thing.setName("one");
+ s.save(thing);
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ List things = s.createQuery("from Thing").list();
+ assertEquals(1, things.size());
+ thing = (Thing)things.get(0);
+ assertEquals("one", thing.getName());
+ // give it a new non-null name and save it
+ thing.setName("one_changed");
+ s.update(thing);
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ things = s.createQuery("from Thing").list();
+ assertEquals(1, things.size());
+ thing = (Thing)things.get(0);
+ assertEquals("one_changed", thing.getName());
+ s.delete(thing);
+ t.commit();
+ s.close();
+ }
+
+ public void testUpdateNonNullOptionalJoinToDiffNonNullDetached() throws Exception {
+ Session s = openSession();
+ Transaction t = s.beginTransaction();
+
+ // create a new thing with a non-null name
+ Thing thing = new Thing();
+ thing.setName("one");
+ s.save(thing);
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ List things = s.createQuery("from Thing").list();
+ assertEquals(1, things.size());
+ thing = (Thing)things.get(0);
+ assertEquals("one", thing.getName());
+ t.commit();
+ s.close();
+
+ // change detached thing name to a new non-null name and save it
+ thing.setName("one_changed");
+
+ s = openSession();
+ t = s.beginTransaction();
+ s.update(thing);
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ things = s.createQuery("from Thing").list();
+ assertEquals(1, things.size());
+ thing = (Thing)things.get(0);
+ assertEquals("one_changed", thing.getName());
+ s.delete(thing);
+ t.commit();
+ s.close();
+ }
+
+ public void testMergeNonNullOptionalJoinToDiffNonNullDetached() throws Exception {
+ Session s = openSession();
+ Transaction t = s.beginTransaction();
+
+ // create a new thing with a non-null name
+ Thing thing = new Thing();
+ thing.setName("one");
+ s.save(thing);
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ List things = s.createQuery("from Thing").list();
+ assertEquals(1, things.size());
+ thing = (Thing)things.get(0);
+ assertEquals("one", thing.getName());
+ t.commit();
+ s.close();
+
+ // change detached thing name to a new non-null name and save it
+ thing.setName("one_changed");
+
+ s = openSession();
+ t = s.beginTransaction();
+ s.merge(thing);
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ things = s.createQuery("from Thing").list();
+ assertEquals(1, things.size());
+ thing = (Thing)things.get(0);
+ assertEquals("one_changed", thing.getName());
+ s.delete(thing);
+ t.commit();
+ s.close();
+ }
+
+
+ public void testUpdateNonNullOptionalJoinToNull() throws Exception {
+ Session s = openSession();
+ Transaction t = s.beginTransaction();
+
+ // create a new thing with a non-null name
+ Thing thing = new Thing();
+ thing.setName("one");
+ s.save(thing);
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ List things = s.createQuery("from Thing").list();
+ assertEquals(1, things.size());
+ thing = (Thing)things.get(0);
+ assertEquals("one", thing.getName());
+ // give it a null name and save it
+ thing.setName(null);
+ s.update(thing);
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ things = s.createQuery("from Thing").list();
+ assertEquals(1, things.size());
+ thing = (Thing)things.get(0);
+ assertNull(thing.getName());
+ s.delete(thing);
+ t.commit();
+ s.close();
+ }
+
+ public void testUpdateNonNullOptionalJoinToNullDetached() throws Exception {
+ Session s = openSession();
+ Transaction t = s.beginTransaction();
+
+ // create a new thing with a non-null name
+ Thing thing = new Thing();
+ thing.setName("one");
+ s.save(thing);
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ List things = s.createQuery("from Thing").list();
+ assertEquals(1, things.size());
+ thing = (Thing)things.get(0);
+ assertEquals("one", thing.getName());
+ t.commit();
+ s.close();
+
+ // give detached thing a null name and save it
+ thing.setName(null);
+
+ s = openSession();
+ t = s.beginTransaction();
+ s.update(thing);
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ things = s.createQuery("from Thing").list();
+ assertEquals(1, things.size());
+ thing = (Thing)things.get(0);
+ assertNull(thing.getName());
+ s.delete(thing);
+ t.commit();
+ s.close();
+ }
+
+ public void testMergeNonNullOptionalJoinToNullDetached() throws Exception {
+ Session s = openSession();
+ Transaction t = s.beginTransaction();
+
+ // create a new thing with a non-null name
+ Thing thing = new Thing();
+ thing.setName("one");
+ s.save(thing);
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ List things = s.createQuery("from Thing").list();
+ assertEquals(1, things.size());
+ thing = (Thing)things.get(0);
+ assertEquals("one", thing.getName());
+ t.commit();
+ s.close();
+
+ // give detached thing a null name and save it
+ thing.setName(null);
+
+ s = openSession();
+ t = s.beginTransaction();
+ s.merge(thing);
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ things = s.createQuery("from Thing").list();
+ assertEquals(1, things.size());
+ thing = (Thing)things.get(0);
+ assertNull(thing.getName());
+ s.delete(thing);
+ t.commit();
+ s.close();
+ }
+
+ public void testUpdateNullOptionalJoinToNonNull() throws Exception {
+ Session s = openSession();
+ Transaction t = s.beginTransaction();
+ // create a new thing with a null name
+ Thing thing = new Thing();
+ thing.setName(null);
+ s.save(thing);
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ List things = s.createQuery("from Thing").list();
+ assertEquals(1, things.size());
+ thing = (Thing)things.get(0);
+ assertNull(thing.getName());
+ // change name to a non-null value
+ thing.setName("two");
+ s.update(thing);
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ things = s.createQuery("from Thing").list();
+ assertEquals(1, things.size());
+ thing = ((Thing) things.get(0));
+ assertEquals("two", thing.getName());
+ s.delete(thing);
+ t.commit();
+ s.close();
+ }
+
+ public void testUpdateNullOptionalJoinToNonNullDetached() throws Exception {
+ Session s = openSession();
+ Transaction t = s.beginTransaction();
+ // create a new thing with a null name
+ Thing thing = new Thing();
+ thing.setName(null);
+ s.save(thing);
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ List things = s.createQuery("from Thing").list();
+ assertEquals(1, things.size());
+ thing = (Thing)things.get(0);
+ assertNull(thing.getName());
+ t.commit();
+ s.close();
+
+ // change detached thing name to a non-null value
+ thing.setName("two");
+
+ s = openSession();
+ t = s.beginTransaction();
+ s.update(thing);
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ things = s.createQuery("from Thing").list();
+ assertEquals(1, things.size());
+ thing = ((Thing) things.get(0));
+ assertEquals("two", thing.getName());
+ s.delete(thing);
+ t.commit();
+ s.close();
+ }
+
+ public void testMergeNullOptionalJoinToNonNullDetached() throws Exception {
+ Session s = openSession();
+ Transaction t = s.beginTransaction();
+ // create a new thing with a null name
+ Thing thing = new Thing();
+ thing.setName(null);
+ s.save(thing);
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ List things = s.createQuery("from Thing").list();
+ assertEquals(1, things.size());
+ thing = (Thing)things.get(0);
+ assertNull(thing.getName());
+ t.commit();
+ s.close();
+
+ // change detached thing name to a non-null value
+ thing.setName("two");
+
+ s = openSession();
+ t = s.beginTransaction();
+ s.merge(thing);
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ things = s.createQuery("from Thing").list();
+ assertEquals(1, things.size());
+ thing = ((Thing) things.get(0));
+ assertEquals("two", thing.getName());
+ s.delete(thing);
+ t.commit();
+ s.close();
+ }
+}
Added: core/trunk/testsuite/src/test/java/org/hibernate/test/join/Thing.hbm.xml
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/join/Thing.hbm.xml
(rev 0)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/join/Thing.hbm.xml 2007-08-15
20:23:35 UTC (rev 12946)
@@ -0,0 +1,24 @@
+<?xml version="1.0"?>
+<!DOCTYPE hibernate-mapping PUBLIC
+ "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
+ "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
+
+<!--
+ This mapping demonstrates optional joined properties
+-->
+
+<hibernate-mapping package="org.hibernate.test.join"
default-access="field">
+
+ <class name="Thing" table="thing">
+
+ <id name="id" column="thing_id" unsaved-value="0">
+ <generator class="native"/>
+ </id>
+
+ <join table="thing_name" optional="true">
+ <key column="thing_id"/>
+ <property name="name"/>
+ </join>
+ </class>
+
+</hibernate-mapping>
Added: core/trunk/testsuite/src/test/java/org/hibernate/test/join/Thing.java
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/join/Thing.java
(rev 0)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/join/Thing.java 2007-08-15
20:23:35 UTC (rev 12946)
@@ -0,0 +1,63 @@
+//$Id: $
+package org.hibernate.test.join;
+
+/**
+ * @author Chris Jones
+ */
+public class Thing {
+ private Employee salesperson;
+ private String comments;
+
+ /**
+ * @return Returns the salesperson.
+ */
+ public Employee getSalesperson() {
+ return salesperson;
+ }
+ /**
+ * @param salesperson The salesperson to set.
+ */
+ public void setSalesperson(Employee salesperson) {
+ this.salesperson = salesperson;
+ }
+ /**
+ * @return Returns the comments.
+ */
+ public String getComments() {
+ return comments;
+ }
+ /**
+ * @param comments The comments to set.
+ */
+ public void setComments(String comments) {
+ this.comments = comments;
+ }
+
+ Long id;
+ String name;
+
+ /**
+ * @return Returns the ID.
+ */
+ public Long getId() {
+ return id;
+ }
+ /**
+ * @param id The ID to set.
+ */
+ public void setId(Long id) {
+ this.id = id;
+ }
+ /**
+ * @return Returns the name.
+ */
+ public String getName() {
+ return name;
+ }
+ /**
+ * @param name The name to set.
+ */
+ public void setName(String name) {
+ this.name = name;
+ }
+}
Show replies by date