You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by "ab@dataloy.com" <ab...@dataloy.com> on 2017/10/04 09:45:21 UTC

ObjectSelect.columnQuery

Should ObjectSelect.columnQuery  execute a SQL query with just one column selected?

I am using Cayenne 4.1 but running this code:

Property<Cargo> property1 = Property.create("area", Cargo.class);

List<Cargo> select = ObjectSelect.columnQuery(Cargo.class,property1).where(Expression.fromString("area>10")).select(ctx);

the query executed select all the columns of cargo table.

I need to specify which columns the queries must return because I have tables with hundreds of columns that I do not need, returning all the columns every time is very time consuming. 

Andrea

Re: ObjectSelect.columnQuery

Posted by Nikita Timofeev <nt...@objectstyle.com>.
Yes you can add this property:

Property<Cargo> property3 = Property.createSelf(Cargo.class);

But this will eagerly fetch all content of Cargo.

On Wed, Oct 4, 2017 at 3:30 PM, ab@dataloy.com <ab...@dataloy.com> wrote:
>
>
> On 2017-10-04 13:57, Nikita Timofeev <nt...@objectstyle.com> wrote:
>> ObjectSelect.columnQuery can select individual columns, as it was
>> designed just for that.
>> However you can also use it to get full entities along with some
>> arbitrary properties (aggregates, dependent objects, etc.).
>> In your case you created property with type of "Cargo" and it returns
>> you collection of Cargo objects.
>> Just use correct type of required column (String, Integer, etc.).
>>
>> However easiest (and also safe) way to use it is via static Properties
>> generated in a superclass of your entity, i.e. something like this:
>>
>> List<String> someStringProperties =
>> ObjectSelect.columnQuery(Cargo.class,
>> Cargo.SOME_STRING_PROPERTY).select(ctx);
>>
>>
>> On Wed, Oct 4, 2017 at 12:45 PM, ab@dataloy.com <ab...@dataloy.com> wrote:
>> > Should ObjectSelect.columnQuery  execute a SQL query with just one column selected?
>> >
>> > I am using Cayenne 4.1 but running this code:
>> >
>> > Property<Cargo> property1 = Property.create("area", Cargo.class);
>> >
>> > List<Cargo> select = ObjectSelect.columnQuery(Cargo.class,property1).where(Expression.fromString("area>10")).select(ctx);
>> >
>> > the query executed select all the columns of cargo table.
>> >
>> > I need to specify which columns the queries must return because I have tables with hundreds of columns that I do not need, returning all the columns every time is very time consuming.
>> >
>> > Andrea
>>
>>
>>
>> --
>> Best regards,
>> Nikita Timofeev
> But if I do something like this:
>
> Property<String> property1 = Property.create("area", String.class);
> Property<Double> property2 = Property.create("voyage.flCost", Double.class);
> List<Object[]> select = ObjectSelect.query(Cargo.class).columns(property1,property2).select(ctx);
>
> There is the possibility to get a list of Cargo objects?



-- 
Best regards,
Nikita Timofeev

Re: ObjectSelect.columnQuery

Posted by "ab@dataloy.com" <ab...@dataloy.com>.

On 2017-10-04 13:57, Nikita Timofeev <nt...@objectstyle.com> wrote: 
> ObjectSelect.columnQuery can select individual columns, as it was
> designed just for that.
> However you can also use it to get full entities along with some
> arbitrary properties (aggregates, dependent objects, etc.).
> In your case you created property with type of "Cargo" and it returns
> you collection of Cargo objects.
> Just use correct type of required column (String, Integer, etc.).
> 
> However easiest (and also safe) way to use it is via static Properties
> generated in a superclass of your entity, i.e. something like this:
> 
> List<String> someStringProperties =
> ObjectSelect.columnQuery(Cargo.class,
> Cargo.SOME_STRING_PROPERTY).select(ctx);
> 
> 
> On Wed, Oct 4, 2017 at 12:45 PM, ab@dataloy.com <ab...@dataloy.com> wrote:
> > Should ObjectSelect.columnQuery  execute a SQL query with just one column selected?
> >
> > I am using Cayenne 4.1 but running this code:
> >
> > Property<Cargo> property1 = Property.create("area", Cargo.class);
> >
> > List<Cargo> select = ObjectSelect.columnQuery(Cargo.class,property1).where(Expression.fromString("area>10")).select(ctx);
> >
> > the query executed select all the columns of cargo table.
> >
> > I need to specify which columns the queries must return because I have tables with hundreds of columns that I do not need, returning all the columns every time is very time consuming.
> >
> > Andrea
> 
> 
> 
> -- 
> Best regards,
> Nikita Timofeev
But if I do something like this:

Property<String> property1 = Property.create("area", String.class);
Property<Double> property2 = Property.create("voyage.flCost", Double.class);
List<Object[]> select = ObjectSelect.query(Cargo.class).columns(property1,property2).select(ctx);

There is the possibility to get a list of Cargo objects?

Re: ObjectSelect.columnQuery

Posted by "ab@dataloy.com" <ab...@dataloy.com>.

On 2017-10-04 13:57, Nikita Timofeev <nt...@objectstyle.com> wrote: 
> ObjectSelect.columnQuery can select individual columns, as it was
> designed just for that.
> However you can also use it to get full entities along with some
> arbitrary properties (aggregates, dependent objects, etc.).
> In your case you created property with type of "Cargo" and it returns
> you collection of Cargo objects.
> Just use correct type of required column (String, Integer, etc.).
> 
> However easiest (and also safe) way to use it is via static Properties
> generated in a superclass of your entity, i.e. something like this:
> 
> List<String> someStringProperties =
> ObjectSelect.columnQuery(Cargo.class,
> Cargo.SOME_STRING_PROPERTY).select(ctx);
> 
> 
> On Wed, Oct 4, 2017 at 12:45 PM, ab@dataloy.com <ab...@dataloy.com> wrote:
> > Should ObjectSelect.columnQuery  execute a SQL query with just one column selected?
> >
> > I am using Cayenne 4.1 but running this code:
> >
> > Property<Cargo> property1 = Property.create("area", Cargo.class);
> >
> > List<Cargo> select = ObjectSelect.columnQuery(Cargo.class,property1).where(Expression.fromString("area>10")).select(ctx);
> >
> > the query executed select all the columns of cargo table.
> >
> > I need to specify which columns the queries must return because I have tables with hundreds of columns that I do not need, returning all the columns every time is very time consuming.
> >
> > Andrea
> 
> 
> 
> -- 
> Best regards,
> Nikita Timofeev

Perfect, I got it. Now it works I expected.

Many thanks

Re: ObjectSelect.columnQuery

Posted by Nikita Timofeev <nt...@objectstyle.com>.
ObjectSelect.columnQuery can select individual columns, as it was
designed just for that.
However you can also use it to get full entities along with some
arbitrary properties (aggregates, dependent objects, etc.).
In your case you created property with type of "Cargo" and it returns
you collection of Cargo objects.
Just use correct type of required column (String, Integer, etc.).

However easiest (and also safe) way to use it is via static Properties
generated in a superclass of your entity, i.e. something like this:

List<String> someStringProperties =
ObjectSelect.columnQuery(Cargo.class,
Cargo.SOME_STRING_PROPERTY).select(ctx);


On Wed, Oct 4, 2017 at 12:45 PM, ab@dataloy.com <ab...@dataloy.com> wrote:
> Should ObjectSelect.columnQuery  execute a SQL query with just one column selected?
>
> I am using Cayenne 4.1 but running this code:
>
> Property<Cargo> property1 = Property.create("area", Cargo.class);
>
> List<Cargo> select = ObjectSelect.columnQuery(Cargo.class,property1).where(Expression.fromString("area>10")).select(ctx);
>
> the query executed select all the columns of cargo table.
>
> I need to specify which columns the queries must return because I have tables with hundreds of columns that I do not need, returning all the columns every time is very time consuming.
>
> Andrea



-- 
Best regards,
Nikita Timofeev