[hibernate-commits] Hibernate SVN: r19472 - in core/trunk: testsuite/src/test/java/org/hibernate/test/naturalid/immutable and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Tue May 11 22:27:57 EDT 2010


Author: gbadner
Date: 2010-05-11 22:27:56 -0400 (Tue, 11 May 2010)
New Revision: 19472

Added:
   core/trunk/testsuite/src/test/java/org/hibernate/test/naturalid/immutable/Child.java
   core/trunk/testsuite/src/test/java/org/hibernate/test/naturalid/immutable/ImmutableEntityNaturalIdTest.java
   core/trunk/testsuite/src/test/java/org/hibernate/test/naturalid/immutable/Parent.java
   core/trunk/testsuite/src/test/java/org/hibernate/test/naturalid/immutable/ParentChildWithManyToOne.hbm.xml
Modified:
   core/trunk/core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java
Log:
HHH-1574 : AbstractEntityPersister.getNaturalIdentifierSnapshot doesn't work with many-to-one ids (Alex Burgel)

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	2010-05-12 02:11:27 UTC (rev 19471)
+++ core/trunk/core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java	2010-05-12 02:27:56 UTC (rev 19472)
@@ -4084,8 +4084,13 @@
 						return null;
 					}
 
+					final EntityKey key = new EntityKey( id, this, session.getEntityMode() );
+					Object owner = session.getPersistenceContext().getEntity( key );
 					for ( int i = 0; i < naturalIdPropertyCount; i++ ) {
 						snapshot[i] = extractionTypes[i].hydrate( rs, getPropertyAliases( "", naturalIdPropertyIndexes[i] ), session, null );
+						if (extractionTypes[i].isEntityType()) {
+							snapshot[i] = extractionTypes[i].resolve(snapshot[i], session, owner);
+						}
 					}
 					return snapshot;
 				}

Added: core/trunk/testsuite/src/test/java/org/hibernate/test/naturalid/immutable/Child.java
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/naturalid/immutable/Child.java	                        (rev 0)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/naturalid/immutable/Child.java	2010-05-12 02:27:56 UTC (rev 19472)
@@ -0,0 +1,43 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Middleware LLC 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 Middleware LLC.
+ *
+ * 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.naturalid.immutable;
+
+/**
+ * @author Alex Burgel
+ */
+public class Child {
+	
+	private Long id;
+    private Parent parent;
+	private String name;
+	
+	Child() {}
+
+	public Child(String name, Parent parent) {
+		this.name = name;
+        this.parent = parent;
+	}
+	
+}

Added: core/trunk/testsuite/src/test/java/org/hibernate/test/naturalid/immutable/ImmutableEntityNaturalIdTest.java
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/naturalid/immutable/ImmutableEntityNaturalIdTest.java	                        (rev 0)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/naturalid/immutable/ImmutableEntityNaturalIdTest.java	2010-05-12 02:27:56 UTC (rev 19472)
@@ -0,0 +1,144 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Middleware LLC 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 Middleware LLC.
+ *
+ * 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.naturalid.immutable;
+
+import java.lang.reflect.Field;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.hibernate.FetchMode;
+import org.hibernate.HibernateException;
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.Environment;
+import org.hibernate.criterion.Restrictions;
+import org.hibernate.junit.functional.FunctionalTestCase;
+
+/**
+ * @author Alex Burgel
+ */
+public class ImmutableEntityNaturalIdTest extends FunctionalTestCase {
+
+    public ImmutableEntityNaturalIdTest(String str) {
+        super(str);
+    }
+
+	public void testNaturalIdCheck() throws Exception {
+        Session s = openSession();
+        Transaction t = s.beginTransaction();
+        Parent p = new Parent("alex");
+        Child c = new Child("billy", p);
+        s.persist(p);
+        s.persist(c);
+		t.commit();
+		s.close();
+
+        Field name = c.getClass().getDeclaredField("name");
+        name.setAccessible(true);
+        name.set(c, "phil");
+
+		s = openSession();
+		t = s.beginTransaction();
+        try {
+            s.saveOrUpdate( c );
+			s.flush();
+            fail( "should have failed because immutable natural ID was altered");
+        }
+        catch (HibernateException he) {
+			// expected
+		}
+		finally {
+			t.rollback();
+			s.close();
+		}
+
+        name.set(c, "billy");
+
+		s = openSession();
+		t = s.beginTransaction();
+        s.delete(c);
+        s.delete(p);
+        t.commit();
+        s.close();
+    }
+
+    public void testSaveParentWithDetachedChildren() throws Exception {
+        Session s = openSession();
+        Transaction t = s.beginTransaction();
+
+        Parent p = new Parent("alex");
+        Child c = new Child("billy", p);
+
+        s.persist(p);
+        s.persist(c);
+        t.commit();
+        s.close();
+
+        s = openSession();
+        t = s.beginTransaction();
+
+        p = (Parent) s.createCriteria(Parent.class)
+				.add( Restrictions.eq("name", "alex") )
+				.setFetchMode("children", FetchMode.JOIN)
+        .setCacheable(true)
+        .uniqueResult();
+
+        t.commit();
+        s.close();
+
+        s = openSession();
+        t = s.beginTransaction();
+
+        Child c2 = new Child("joey", p);
+        p.getChildren().add(c2);
+
+        s.update(p);
+
+        // this fails if AbstractEntityPersister returns identifiers instead of entities from
+        // AbstractEntityPersister.getNaturalIdSnapshot()
+        s.flush();
+
+        s.delete(p);
+        t.commit();
+        s.close();
+    }
+
+    public void configure(Configuration cfg) {
+        cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true");
+        cfg.setProperty(Environment.USE_QUERY_CACHE, "true");
+        cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
+    }
+
+    public String[] getMappings() {
+        return new String[] { "naturalid/immutable/ParentChildWithManyToOne.hbm.xml" };
+    }
+
+    public static Test suite() {
+        return new TestSuite( ImmutableEntityNaturalIdTest.class);
+    }
+
+}

Added: core/trunk/testsuite/src/test/java/org/hibernate/test/naturalid/immutable/Parent.java
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/naturalid/immutable/Parent.java	                        (rev 0)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/naturalid/immutable/Parent.java	2010-05-12 02:27:56 UTC (rev 19472)
@@ -0,0 +1,58 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Middleware LLC 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 Middleware LLC.
+ *
+ * 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.naturalid.immutable;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author Alex Burgel
+ */
+public class Parent {
+	
+	private Long id;
+	private String name;
+	private List children = new ArrayList();
+
+	Parent() {}
+
+	public Parent(String name) {
+		this.name = name;
+	}
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public List getChildren() {
+        return children;
+    }
+
+    public void setChildren(List children) {
+        this.children = children;
+    }
+
+    
+}

Added: core/trunk/testsuite/src/test/java/org/hibernate/test/naturalid/immutable/ParentChildWithManyToOne.hbm.xml
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/naturalid/immutable/ParentChildWithManyToOne.hbm.xml	                        (rev 0)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/naturalid/immutable/ParentChildWithManyToOne.hbm.xml	2010-05-12 02:27:56 UTC (rev 19472)
@@ -0,0 +1,41 @@
+<?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 illustrates use of <natural-id> with a many-to-one.
+  
+-->
+
+<hibernate-mapping 
+	package="org.hibernate.test.naturalid.immutable"
+	default-access="field">
+	
+	<class name="Parent" table="Parent">
+		<id name="id">
+			<generator class="increment"/>
+		</id>
+		
+		<property name="name"/>
+		
+		<bag name="children" inverse="true" cascade="all">
+			<key column="parent" />
+			<one-to-many class="Child" />
+		</bag>
+
+	</class>
+
+	<class name="Child" table="Child">
+		<id name="id">
+			<generator class="increment"/>
+		</id>
+		
+		<natural-id mutable="false">
+			<many-to-one name="parent" class="Parent" />
+			<property name="name"/>
+		</natural-id>
+	</class>
+	
+</hibernate-mapping>
\ No newline at end of file



More information about the hibernate-commits mailing list