ColumnSelect and to-many relationships

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

ColumnSelect and to-many relationships

Hugi Thordarson
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ColumnSelect and to-many relationships

Nikita Timofeev
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 <[hidden email]> 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
Loading...