You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Hugi Thordarson <hu...@karlmenn.is> on 2017/05/26 11:03:00 UTC

ColumnSelect and to-many relationships

Hi all,
I seem to recall that it was possible to fetch to-many relationships using ColumnSelect. However, if I attempt to do something like this…

public static void main( String[] args ) {
	List<Object[]> list = ObjectSelect
			.query( Receipt.class )
			.columns( Receipt.UNIQUE_ID, Receipt.ENTRIES )
			.where( Receipt.USER.dot( User.NAME ).eq( "Hugi Þórðarson" ) )
			.select( StrimillinnCore.newContext() );

	for( Object[] row : list ) {
		System.out.println( Arrays.asList( row ) );
	}
}

…the fetch will fail with a ClassCastException ([Ljava.lang.Object; cannot be cast to org.apache.cayenne.DataRow).

Any other way to do it? Or am I misremembering that this was possible?

Cheers,
- hugi

Re: ColumnSelect and to-many relationships

Posted by Nikita Timofeev <nt...@objectstyle.com>.
Hi Hugi,

You remembering this correctly, this was possible in 4.0M5.
Since 4.0.M6 there is API to select whole objects via column select.
It is working by default for toOne relationships,
for collections there is new method in Property flat(Class<?>), that
will return "flat" array of objects.

So your code should be like this:

List<Object[]> list = ObjectSelect
                        .query( Receipt.class )
                        .columns( Receipt.UNIQUE_ID,
Receipt.ENTRIES.flat(Entry.class) )
                        .where( Receipt.USER.dot( User.NAME ).eq(
"Hugi Þórðarson" ) )
                        .select( StrimillinnCore.newContext() );

But still Exception you got is incorrect, you supposed to get readable
message on how to use toMany relationships in ColumnSelect.
Could you please tell how property Receipt.ENTRIES is defined in the code?

On Fri, May 26, 2017 at 2:03 PM, Hugi Thordarson <hu...@karlmenn.is> wrote:
> Hi all,
> I seem to recall that it was possible to fetch to-many relationships using ColumnSelect. However, if I attempt to do something like this…
>
> public static void main( String[] args ) {
>         List<Object[]> list = ObjectSelect
>                         .query( Receipt.class )
>                         .columns( Receipt.UNIQUE_ID, Receipt.ENTRIES )
>                         .where( Receipt.USER.dot( User.NAME ).eq( "Hugi Þórðarson" ) )
>                         .select( StrimillinnCore.newContext() );
>
>         for( Object[] row : list ) {
>                 System.out.println( Arrays.asList( row ) );
>         }
> }
>
> …the fetch will fail with a ClassCastException ([Ljava.lang.Object; cannot be cast to org.apache.cayenne.DataRow).
>
> Any other way to do it? Or am I misremembering that this was possible?
>
> Cheers,
> - hugi



-- 
Best regards,
Nikita Timofeev