[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-2831) Native SQL queries with addJoin or <return-join/> return object arrays instead of single Entities

Gerke Geurts (JIRA) noreply at atlassian.com
Wed Dec 17 07:04:39 EST 2008


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-2831?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=31975#action_31975 ] 

Gerke Geurts commented on HHH-2831:
-----------------------------------

When working on issue NH-1612 (http://jira.nhibernate.org/browse/NH-1612) I encountered the same issue. The bug seems to be in the CustomLoader.ResultRowProcessor.buildResultRow() method (http://viewvc.jboss.org/cgi-bin/viewvc.cgi/hibernate/core/trunk/core/src/main/java/org/hibernate/loader/custom/CustomLoader.java?view=markup). 

Replacing:

		public Object buildResultRow(
				Object[] data,
				ResultSet resultSet,
				boolean hasTransformer,
				SessionImplementor session) throws SQLException, HibernateException {

			Object[] resultRow;
			if ( !hasScalars ) {
				resultRow = data;
			}
			else {
				// build an array with indices equal to the total number
				// of actual returns in the result Hibernate will return
				// for this query (scalars + non-scalars)
				resultRow = new Object[ columnProcessors.length ];
				for ( int i = 0; i < columnProcessors.length; i++ ) {
					resultRow[i] = columnProcessors[i].extract( data, resultSet, session );
				}
			}

			return ( hasTransformer )
			       ? resultRow
			       : ( resultRow.length == 1 )
			         ? resultRow[0]
			         : resultRow;
		}

with:

		public Object buildResultRow(
				Object[] data,
				ResultSet resultSet,
				boolean hasTransformer,
				SessionImplementor session) throws SQLException, HibernateException {

			Object[] resultRow;
	                if (hasScalars || !hasTransformer)
        	        {
	                    // build an array with indices equal to the total number
	                    // of actual returns in the result Hibernate will return
	                    // for this query (scalars + non-scalars)
        	            resultRow = new Object[columnProcessors.Length];
                	    for (int i = 0; i < columnProcessors.Length; i++)
	                    {
        	                resultRow[i] = columnProcessors[i].Extract(data, resultSet, session);
                	    }
	                }
        	        else
	                {
	                    resultRow = data;
	                }

	                return hasTransformer || resultRow.Length != 1
        	            ? resultRow
                	    : resultRow[0];
		}

Does the trick in NHIbernate.


> Native SQL queries with addJoin or <return-join/> return object arrays instead of single Entities
> -------------------------------------------------------------------------------------------------
>
>                 Key: HHH-2831
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2831
>             Project: Hibernate Core
>          Issue Type: Bug
>          Components: query-sql
>    Affects Versions: 3.2.4, 3.2.5
>         Environment: Hibernate 3.2.4.ga and 3.2.5.ga with hsqldb on Win XP
>            Reporter: Jeremy Grodberg
>         Attachments: NativeSQLQueriesTest.patch
>
>
> Although the documentation is not crystal clear, I read it to say that using addJoin should eagerly fetch an associated object but should NOT return it as a separate value.  In section 16.1.3. "Handling associations and collections" of the online documentation it says: "It is possible to eagerly join in the Dog to avoid the possible extra roundtrip for initializing the proxy. This is done via the addJoin() method, which allows you to join in an association or collection."  This comes BEFORE the section on returning multiple entities, so I say the documentation at least implies it will only return a single entity at the top level.  Also, if the intention is to return multiple entities, addEntity() works fine for that, so what then would be the difference of addJoin()?  If I'm wrong about what addJoin() should do, please clarify that in the documentation and also clarify how, if it is possible, one could eagerly fetch the association without changing the return type of the qu
 ery.
> I have reproduced this problem in the Hibernate JUnit tests in 3.2.4.ga and 3.2.5.ga, specifically NativeSQLQueriesTest.testSQLQueryInterface().
> I'm attaching a patch to the Hibernate junit test org.hibernate.test.sql.hand.query.NativeSQLQueriesTest.java released in 3.2.5.ga that adds a test to ensure that a query with addJoin only returns a (list of) entities, not a list of Object arrays containing entities.  Currently, the assertion fails because instead of returning a list of Organizations we get a list of Object[3] = { Organization, Employment, Person }, which is exactly what we get when the addJoin()s are replaced with appropriate addEntity() calls.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the hibernate-issues mailing list