[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-1088) IdentifierProjection does not work with composite keys

Michael Grünewald (JIRA) noreply at atlassian.com
Thu Apr 30 13:05:17 EDT 2009


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

Michael Grünewald commented on HHH-1088:
----------------------------------------

Hi, I have the same problem.

Hibernate just creates wrong SQl like this:
"select this_.comment as y0_this_.name as y1_this_.type as y2_this_.year as y3_ from CountryModel this_"

It recognizes that their is an compund key but don't use the right syntax.
Cause:
"select this_.comment as y0_, this_.name as y1_, this_.type as y2_, this_.year as y3_ from CountryModel this_"
would be correct, just a separating comma and space are missing if more than one identifier is used.

According to the source code from IdentifierProjection:


	public String toSqlString(Criteria criteria, int position, CriteriaQuery criteriaQuery) 
	throws HibernateException {
		StringBuffer buf = new StringBuffer();
		String[] cols = criteriaQuery.getIdentifierColumns(criteria);
		for ( int i=0; i<cols.length; i++ ) {
			buf.append( cols[i] )
				.append(" as y")
				.append(position + i)
				.append('_');
		}
		return buf.toString();

adding a simple if -statement should fix it :

public String toSqlString(final Criteria criteria, final int position, CriteriaQuery criteriaQuery)
	    throws HibernateException {
	final StringBuffer buf = new StringBuffer();
	final String[] cols = criteriaQuery.getIdentifierColumns(criteria);
	for (int i = 0, n = cols.length; i < n; i++) {
	    buf.append(cols[i]).append(" as y").append(position + i).append('_');

	 if (i < n - 1) {
		buf.append(", ");
	    }
	}
	return buf.toString();
    }
	}

IMHO projections to compound keys makes sense, to avoid manual SQL-Queries especially with eager realations, if you need all key attributes to provide a dialog which the user can choose from.

So if someone can please change the code.

Thanks a lot.

Greetings Michael

> IdentifierProjection does not work with composite keys
> ------------------------------------------------------
>
>                 Key: HHH-1088
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-1088
>             Project: Hibernate Core
>          Issue Type: Bug
>          Components: query-criteria
>    Affects Versions: 3.1 rc2
>            Reporter: Max Muermann
>         Attachments: CompositeIdProjection.java, CriteriaLoader.java
>
>
> When working with Criteria queries, the IdentifierProjection breaks if the entity has a composite key.
> In IdentifierProjection.java:
> public String toSqlString(Criteria criteria, int position, CriteriaQuery criteriaQuery) 
> 	throws HibernateException {
> 		StringBuffer buf = new StringBuffer();
> 		String[] cols = criteriaQuery.getIdentifierColumns(criteria);
> 		for ( int i=0; i<cols.length; i++ ) {
> 			buf.append( cols[i] )
> 				.append(" as y")
> 				.append(position + i)
> 				.append('_');
> 		}
> 		return buf.toString();
> 	}
> This method does not add commas as separators between the column names. Easily fixed by adding
> if (i<col.length-1)
>   buf.append(",");
> as the last statement inside the loop.
> However, this leads to another problem:
> the type returned by IdentifierProjection.geType is the (single) type of the composite id component. The query will however return the property values of the id component without a mapping step.

-- 
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