diff --git a/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e1/a/DerivedIdentitySimpleParentIdClassDepTest.java b/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e1/a/DerivedIdentitySimpleParentIdClassDepTest.java index e519746bd7..bad437125c 100644 --- a/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e1/a/DerivedIdentitySimpleParentIdClassDepTest.java +++ b/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e1/a/DerivedIdentitySimpleParentIdClassDepTest.java @@ -1,5 +1,6 @@ package org.hibernate.test.annotations.derivedidentities.e1.a; +import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.test.annotations.TestCase; import org.hibernate.test.util.SchemaUtil; @@ -50,14 +51,11 @@ public class DerivedIdentitySimpleParentIdClassDepTest extends TestCase { // List depList = s.createQuery("Select d from Dependent d where d.name='LittleP'").list(); // the following query is not finding the entity 'd' added above - List depList = s.createQuery("Select d from Dependent d where d.name='LittleP' and d.emp.name='Paula'").list(); - Object newDependent = null; - if (depList.size() > 0) { - newDependent = (Dependent) depList.get(0); - } - if (newDependent != d) { - fail("PC entity instance (" + d +") does not match returned query result value (" + newDependent); - } + Query query = s.createQuery("Select d from Dependent d where d.name='LittleP' and d.emp.empName='Paula'"); + List depList = query.list(); + assertEquals( 1, depList.size() ); + Object newDependent = (Dependent) depList.get(0); + assertSame( d, newDependent ); s.getTransaction().rollback(); s.close(); } diff --git a/annotations/src/test/resources/log4j.properties b/annotations/src/test/resources/log4j.properties index 268c2ec06f..0be8139ad8 100644 --- a/annotations/src/test/resources/log4j.properties +++ b/annotations/src/test/resources/log4j.properties @@ -30,7 +30,7 @@ log4j.logger.org.hibernate.SQL=debug #log4j.logger.org.hibernate.engine.CascadingAction=debug ### log JDBC bind parameters ### -log4j.logger.org.hibernate.type=debug +log4j.logger.org.hibernate.type=trace ### log schema export/update ### log4j.logger.org.hibernate.tool.hbm2ddl=debug diff --git a/core/src/main/java/org/hibernate/persister/entity/AbstractPropertyMapping.java b/core/src/main/java/org/hibernate/persister/entity/AbstractPropertyMapping.java index 1f9a8408e2..d387fc8243 100644 --- a/core/src/main/java/org/hibernate/persister/entity/AbstractPropertyMapping.java +++ b/core/src/main/java/org/hibernate/persister/entity/AbstractPropertyMapping.java @@ -27,6 +27,9 @@ package org.hibernate.persister.entity; import java.util.HashMap; import java.util.Map; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import org.hibernate.MappingException; import org.hibernate.QueryException; import org.hibernate.engine.Mapping; @@ -44,6 +47,7 @@ import org.hibernate.util.StringHelper; * @author Gavin King */ public abstract class AbstractPropertyMapping implements PropertyMapping { + private static final Logger log = LoggerFactory.getLogger( AbstractPropertyMapping.class ); private final Map typesByPropertyPath = new HashMap(); private final Map columnsByPropertyPath = new HashMap(); @@ -124,9 +128,24 @@ public abstract class AbstractPropertyMapping implements PropertyMapping { return result; } - protected void addPropertyPath(String path, Type type, String[] columns, - String[] columnReaders, String[] columnReaderTemplates, + protected void addPropertyPath( + String path, + Type type, + String[] columns, + String[] columnReaders, + String[] columnReaderTemplates, String[] formulaTemplates) { + // TODO : not quite sure yet of the difference, but this is only needed from annotations for @Id @ManyToOne support + if ( typesByPropertyPath.containsKey( path ) ) { + if ( log.isTraceEnabled() ) { + log.trace( + "Skipping duplicate registration of path [" + path + + "], existing type = [" + typesByPropertyPath.get(path) + + "], incoming type = [" + type + "]" + ); + } + return; + } typesByPropertyPath.put(path, type); columnsByPropertyPath.put(path, columns); columnReadersByPropertyPath.put(path, columnReaders); diff --git a/testsuite/src/test/java/org/hibernate/test/keymanytoone/bidir/component/EagerKeyManyToOneTest.java b/testsuite/src/test/java/org/hibernate/test/keymanytoone/bidir/component/EagerKeyManyToOneTest.java index 2447d3724e..7d384abdb2 100644 --- a/testsuite/src/test/java/org/hibernate/test/keymanytoone/bidir/component/EagerKeyManyToOneTest.java +++ b/testsuite/src/test/java/org/hibernate/test/keymanytoone/bidir/component/EagerKeyManyToOneTest.java @@ -1,5 +1,7 @@ package org.hibernate.test.keymanytoone.bidir.component; +import java.util.List; + import junit.framework.Test; import org.hibernate.HibernateException; diff --git a/testsuite/src/test/java/org/hibernate/test/keymanytoone/bidir/component/LazyKeyManyToOneTest.java b/testsuite/src/test/java/org/hibernate/test/keymanytoone/bidir/component/LazyKeyManyToOneTest.java index cfa43dd9cc..a54b36d895 100644 --- a/testsuite/src/test/java/org/hibernate/test/keymanytoone/bidir/component/LazyKeyManyToOneTest.java +++ b/testsuite/src/test/java/org/hibernate/test/keymanytoone/bidir/component/LazyKeyManyToOneTest.java @@ -1,5 +1,7 @@ package org.hibernate.test.keymanytoone.bidir.component; +import java.util.List; + import junit.framework.Test; import org.hibernate.Session; @@ -29,6 +31,32 @@ public class LazyKeyManyToOneTest extends FunctionalTestCase { cfg.setProperty( Environment.GENERATE_STATISTICS, "true" ); } + public void testQueryingOnMany2One() { + Session s = openSession(); + s.beginTransaction(); + Customer cust = new Customer( "Acme, Inc." ); + Order order = new Order( new Order.Id( cust, 1 ) ); + cust.getOrders().add( order ); + s.save( cust ); + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.beginTransaction(); + List results = s.createQuery( "from Order o where o.id.customer.name = :name" ) + .setParameter( "name", cust.getName() ) + .list(); + assertEquals( 1, results.size() ); + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.beginTransaction(); + s.delete( cust ); + s.getTransaction().commit(); + s.close(); + } + public void testSaveCascadedToKeyManyToOne() { // test cascading a save to an association with a key-many-to-one which refers to a // just saved entity diff --git a/testsuite/src/test/java/org/hibernate/test/keymanytoone/bidir/embedded/KeyManyToOneTest.java b/testsuite/src/test/java/org/hibernate/test/keymanytoone/bidir/embedded/KeyManyToOneTest.java index a5b2d57844..dc4e532d44 100644 --- a/testsuite/src/test/java/org/hibernate/test/keymanytoone/bidir/embedded/KeyManyToOneTest.java +++ b/testsuite/src/test/java/org/hibernate/test/keymanytoone/bidir/embedded/KeyManyToOneTest.java @@ -1,5 +1,7 @@ package org.hibernate.test.keymanytoone.bidir.embedded; +import java.util.List; + import junit.framework.Test; import org.hibernate.Session; @@ -45,6 +47,32 @@ public class KeyManyToOneTest extends FunctionalTestCase { s.close(); } + public void testQueryingOnMany2One() { + Session s = openSession(); + s.beginTransaction(); + Customer cust = new Customer( "Acme, Inc." ); + Order order = new Order( cust, 1 ); + cust.getOrders().add( order ); + s.save( cust ); + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.beginTransaction(); + List results = s.createQuery( "from Order o where o.customer.name = :name" ) + .setParameter( "name", cust.getName() ) + .list(); + assertEquals( 1, results.size() ); + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.beginTransaction(); + s.delete( cust ); + s.getTransaction().commit(); + s.close(); + } + public void testLoadingStrategies() { Session s = openSession(); s.beginTransaction();