Author: anthonyHib
Date: 2009-04-23 02:48:46 -0400 (Thu, 23 Apr 2009)
New Revision: 16415
Added:
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/sql/hand/Group.java
Modified:
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/loader/custom/sql/SQLQueryReturnProcessor.java
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/sql/hand/Person.java
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/sql/hand/query/NativeSQLQueries.hbm.xml
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/sql/hand/query/NativeSQLQueriesTest.java
Log:
HHH-2745 NullPointerException when eager fetching joined many-to-many with native SQL
query
Modified:
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/loader/custom/sql/SQLQueryReturnProcessor.java
===================================================================
---
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/loader/custom/sql/SQLQueryReturnProcessor.java 2009-04-23
05:56:50 UTC (rev 16414)
+++
core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/loader/custom/sql/SQLQueryReturnProcessor.java 2009-04-23
06:48:46 UTC (rev 16415)
@@ -374,7 +374,7 @@
alias2CollectionSuffix.put( alias, suffix );
collectionPropertyResultMaps.put( alias, propertyResults );
- if ( collectionPersister.isOneToMany() ) {
+ if ( collectionPersister.isOneToMany() || collectionPersister.isManyToMany()) {
SQLLoadable persister = ( SQLLoadable ) collectionPersister.getElementPersister();
addPersister( alias, filter( propertyResults ), persister );
}
Added: core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/sql/hand/Group.java
===================================================================
--- core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/sql/hand/Group.java
(rev 0)
+++
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/sql/hand/Group.java 2009-04-23
06:48:46 UTC (rev 16415)
@@ -0,0 +1,35 @@
+package org.hibernate.test.sql.hand;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Group {
+ public Long getId() {
+ return id;
+ }
+ public void setId(Long id) {
+ this.id = id;
+ }
+ public List<Person> getPersons() {
+ return persons;
+ }
+ public void setPersons(List<Person> persons) {
+ this.persons = persons;
+ }
+ private Long id;
+ private List<Person> persons = new ArrayList<Person>();
+ private String name;
+ public String getName() {
+ return name;
+ }
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public Group(String name) {
+ this.name = name;
+ }
+
+ public Group(){}
+
+}
Modified: core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/sql/hand/Person.java
===================================================================
---
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/sql/hand/Person.java 2009-04-23
05:56:50 UTC (rev 16414)
+++
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/sql/hand/Person.java 2009-04-23
06:48:46 UTC (rev 16415)
@@ -5,7 +5,7 @@
* @author Gavin King
*/
public class Person {
- private long id;
+ private Long id;
private String name;
public Person(String name) {
@@ -17,13 +17,13 @@
/**
* @return Returns the id.
*/
- public long getId() {
+ public Long getId() {
return id;
}
/**
* @param id The id to set.
*/
- public void setId(long id) {
+ public void setId(Long id) {
this.id = id;
}
/**
Modified:
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/sql/hand/query/NativeSQLQueries.hbm.xml
===================================================================
---
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/sql/hand/query/NativeSQLQueries.hbm.xml 2009-04-23
05:56:50 UTC (rev 16414)
+++
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/sql/hand/query/NativeSQLQueries.hbm.xml 2009-04-23
06:48:46 UTC (rev 16415)
@@ -31,6 +31,24 @@
</class>
+ <class name="Group" table="GROUPP">
+ <id name="id" unsaved-value="0" column="ID">
+ <generator class="increment"/>
+ </id>
+ <property name="name" column="NAME"
not-null="true"/>
+ <list name="persons" table="GROUP_PERSON"
+ cascade="none" inverse="false" lazy="true">
+ <key>
+ <column name= "GROUP_ID" />
+ </key>
+
+ <list-index column="POS" />
+ <many-to-many class="Person"
+ column="PERSON_ID" />
+ </list>
+
+ </class>
+
<class name="Employment" table="EMPLOYMENT">
<id name="employmentId" unsaved-value="0"
column="EMPID">
<generator class="increment"/>
@@ -258,5 +276,31 @@
FROM ORGANIZATION org
LEFT OUTER JOIN EMPLOYMENT emp ON org.ORGID = emp.EMPLOYER
</sql-query>
-
+ <sql-query name="manyToManyFetch">
+ <![CDATA[
+ SELECT groupp.ID as group_id,
+ groupp.NAME as group_name,
+ group_person.PERSON_ID as group_person_personId,
+ group_person.GROUP_ID as group_person_groupId,
+ group_person.POS as group_person_pos,
+ person.PERID as person_id,
+ person.NAME as person_name
+ FROM GROUPP groupp,
+ GROUP_PERSON group_person,
+ PERSON person
+ WHERE groupp.ID = group_person.GROUP_ID
+ and person.PERID = group_person.PERSON_ID
+ ]]>
+ <return alias="groupp" class="Group">
+ <return-property name="id" column="group_id" />
+ <return-property name="name" column="group_name"
/>
+ </return>
+ <return-join alias="group_person"
property="groupp.persons">
+ <return-property name="key"
column="group_person_groupId" />
+ <return-property name="index"
column="group_person_pos" />
+ <return-property name="element" column="person_id"
/>
+ <return-property name="element.id" column="person_id"
/>
+ <return-property name="element.name"
column="person_name" />
+ </return-join>
+ </sql-query>
</hibernate-mapping>
\ No newline at end of file
Modified:
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/sql/hand/query/NativeSQLQueriesTest.java
===================================================================
---
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/sql/hand/query/NativeSQLQueriesTest.java 2009-04-23
05:56:50 UTC (rev 16414)
+++
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/sql/hand/query/NativeSQLQueriesTest.java 2009-04-23
06:48:46 UTC (rev 16415)
@@ -14,21 +14,22 @@
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.Environment;
+import org.hibernate.junit.functional.FunctionalTestCase;
+import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
+import org.hibernate.test.sql.hand.Dimension;
+import org.hibernate.test.sql.hand.Employment;
+import org.hibernate.test.sql.hand.Group;
+import org.hibernate.test.sql.hand.Order;
import org.hibernate.test.sql.hand.Organization;
import org.hibernate.test.sql.hand.Person;
-import org.hibernate.test.sql.hand.Employment;
import org.hibernate.test.sql.hand.Product;
-import org.hibernate.test.sql.hand.Order;
-import org.hibernate.test.sql.hand.Dimension;
import org.hibernate.test.sql.hand.SpaceShip;
import org.hibernate.test.sql.hand.Speech;
-import org.hibernate.cfg.Configuration;
-import org.hibernate.cfg.Environment;
-import org.hibernate.junit.functional.FunctionalTestCase;
-import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
+import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.hibernate.transform.DistinctRootEntityResultTransformer;
import org.hibernate.transform.Transformers;
-import org.hibernate.transform.AliasToEntityMapResultTransformer;
/**
* Tests of various features of native SQL queries.
@@ -608,6 +609,64 @@
}
}
+ public void testAddJoinForManyToMany() {
+ Session s = openSession();
+ Transaction t = s.beginTransaction();
+ Person gavin = new Person("Gavin");
+ Person max = new Person("Max");
+ Person pete = new Person("Pete");
+
+ Group hibernate = new Group("Hibernate");
+ Group seam = new Group("Seam");
+
+ s.persist(gavin);
+ s.persist(max);
+ s.persist(pete);
+ s.persist(seam);
+ s.persist(hibernate);
+
+
+
+ hibernate.getPersons().add(gavin);
+ hibernate.getPersons().add(max);
+ seam.getPersons().add(gavin);
+ seam.getPersons().add(pete);
+
+ s.flush();
+ s.clear();
+
+// String sqlStr = "SELECT {groupp.*} , {gp.*} " +
+// "FROM GROUPP groupp, GROUP_PERSON gp, PERSON person WHERE groupp.ID =
gp.GROUP_ID and person.PERID = gp.PERSON_ID";
+//
+// List l = s.createSQLQuery( sqlStr )
+// .addEntity("groupp", Group.class)
+// .addJoin("gp","groupp.persons")
+// .list();
+ List l = s.getNamedQuery("manyToManyFetch").list();
+ //assertEquals( 2, l.size() );
+
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+
+ seam.getPersons().remove(gavin);
+ seam.getPersons().remove(pete);
+
+ hibernate.getPersons().remove(gavin);
+ hibernate.getPersons().remove(max);
+
+ s.delete(seam);
+ s.delete(hibernate);
+ s.delete(gavin);
+ s.delete(max);
+ s.delete(pete);
+
+ t.commit();
+ s.close();
+ }
+
private static class UpperCasedAliasToEntityMapResultTransformer extends
AliasToEntityMapResultTransformer {
public Object transformTuple(Object[] tuple, String[] aliases) {
String[] ucAliases = new String[aliases.length];
Show replies by date