HHH-2320 : Update detached entity with null joined properties changed to non-null
git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@12946 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
68dded3aea
commit
06f0676e20
|
@ -1764,6 +1764,7 @@ public abstract class AbstractEntityPersister
|
|||
}
|
||||
throw new StaleObjectStateException( getEntityName(), id );
|
||||
}
|
||||
return false;
|
||||
}
|
||||
catch( TooManyRowsAffectedException e ) {
|
||||
throw new HibernateException(
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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>
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue