[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-2871) can't specify property of the element in the collection

Vlad Rechkalov (JIRA) noreply at atlassian.com
Tue Oct 2 05:04:25 EDT 2007


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-2871?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_28279 ] 

Vlad Rechkalov commented on HHH-2871:
-------------------------------------

If collection is a collection of element there isn't way to join it!

Native sql doesn't supports multi-column types, so element can contain only one column. And it is possible to use CollectionPersister.getElementColumnAliases.

So, I think, SQLQueryParser should contains the following code:

        else if( propertyName.startsWith(ELEMENT_PREFIX) ) {
            if( collectionPersister.isOneToMany() )
                return resolveProperties( aliasName, propertyName.substring(ELEMENT_PREFIX.length()) );

            String[] columnAliases = collectionPersister.getElementColumnAliases(collectionSuffix);

            if ( columnAliases.length != 1 ) {
                // TODO: better error message since we actually support composites if names are explicitly listed.
                throw new QueryException(
                        "SQL queries only support properties mapped to a single column - property [" +
                        propertyName + "] is mapped to " + columnAliases.length + " columns.",
                        originalQueryString
                );
            }

            return columnAliases[0];
        }


> can't specify property of the element in the collection
> -------------------------------------------------------
>
>                 Key: HHH-2871
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2871
>             Project: Hibernate3
>          Issue Type: Bug
>          Components: query-sql
>    Affects Versions: 3.2.5
>            Reporter: Vlad Rechkalov
>         Attachments: SQLQueryParser.java
>
>   Original Estimate: 5 minutes
>  Remaining Estimate: 5 minutes
>
> In according to Hibernate documentation (topic 16.1.4.1. Alias and property references) collection properties can be specified a few ways:
> All properties of the the collection                             {[aliasname].*}
> All properties of the element in the collection        {[aliasname].element.*}
> Property of the element in the collection                 {[aliasname].element.[propertyname]}
> But the last way doesn't work.
> Example:
> There are two entities: Program and Document. Program has one-to-many inverse collection of Documents.
> Program
>   int getId()
>   List<Document> getDocuments()	// one-to-many inverse collection
> Document
>   int getId()
>   String getTitle()
>   Program getParent()		// many-to-one association
> Trying to select Program and fetch collection of Documents:
> select
>   p.id as {p.id},
>   d.program_id as {d.key},
>   d.id as {d.element},
>   d.id as {d.element.id},
>   d.title as {d.element.title},
>   d.program_id as {d.element.program}
> from ...
> SQLQuery q = session.createSQLQuery
> q.addEntity("p", Program.class);
> q.addJoin("d", "p.documents");
> q.list();                                                                  // throws exception
> There is a simple way to fix this bug:
> class SQLQueryParser
> method resolveCollectionProperties
> instead of code (line 140)
> 	else if ( "element.*".equals( propertyName ) ) {
> 		return resolveProperties( aliasName, "*" );
> 	}
> write
> 	else if( propertyName.startsWith(ELEMENT_PREFIX) ) {
> 		return resolveProperties( aliasName, propertyName.substring(ELEMENT_PREFIX.length()) );
> 	}
> comments: ELEMENT_PREFIX is defined as
> 	private static final String ELEMENT_PREFIX = "element.";
> The fixed file is attached.

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