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 2016/02/29 13:25:58 UTC
Counting the number of destination objects for a to-many relationship (or checking if empty)
Hi all,
Is there any simple way or me to count the number of destination objects of a to-many relationship? (without fetching) If not, is there some way to generate an expression from a relationship, so I can use that expression in my own counting functions?
Cheers,
- hugi
Re: Counting the number of destination objects for a to-many relationship (or checking if empty)
Posted by Hugi Thordarson <hu...@karlmenn.is>.
> Hmm, I'll look into the connection issue. Perhaps I did something
> incorrectly. I'll look at your CayenneUtils. Always in the market for
> more ideas. :-)
I think it was something I did while I was attempting to adapt the code for Cayenne 4.0. It was in my early Cayenne days, so I should really look into it again.
> I really should get around to finishing/updating/expanding/etc CBE ... one
> of these days.
:)
- hugi
>
> Thanks,
>
> mrg
>
>
> On Tue, Mar 1, 2016 at 5:12 AM, Hugi Thordarson <hu...@karlmenn.is> wrote:
>
>> Hi Michael!
>>
>> Thanks for the pointer. I did try to use your AggregateUtils with Cayenne
>> 4.0 a while back but it’s not API compatible with 4.0. I was able to get it
>> to work with some changes, but turned out I was creating a new DB
>> connection for each DB function invocation and some other things I didn’t
>> quite like. So I ended up constructing my own utility class that uses EJBQL
>> (still determining how sensible that was).
>>
>>
>> https://bitbucket.org/loftfar/jambalaya/src/22d799925a2c110b4823b36d335889e2ccfc3d06/src/main/java/jambalaya/CayenneUtils.java?at=master&fileviewer=file-view-default
>>
>> But thanks for CBE! IT’s been one of the most useful resources for me
>> while learning Cayenne.
>>
>> Cheers,
>> - hugi
>>
>>
>>
>>> On 29. feb. 2016, at 18:09, Michael Gentry <mg...@masslight.net>
>> wrote:
>>>
>>> PS. I think the code works correctly, but let me know if it doesn't.
>>>
>>>
>>> On Mon, Feb 29, 2016 at 1:09 PM, Michael Gentry <mg...@masslight.net>
>>> wrote:
>>>
>>>> Hi Hugi,
>>>>
>>>> I'm not sure if this will work for 4.0 (since earlier you said you are
>>>> using it), but this is what I'd use for 3.1:
>>>>
>>>>
>>>>
>> https://github.com/mrg/cbe/blob/master/FetchingObjects/Aggregates/src/main/java/cbe/fetching/utilities/AggregateUtils.java
>>>>
>>>> If you have A->>B, you'd construct a SelectQuery to fetch B's where toA
>> =
>>>> your A object. Basically, invert your query and count the matches.
>>>> Something like:
>>>>
>>>> SelectQuery selectQuery = new SelectQuery(B.class);
>>>> selectQuery.setQualifier(ExpressionFactory.matchExp("toA", a));
>>>> int countOfB = AggregateUtils.count(dataContext, selectQuery);
>>>>
>>>> Some other example uses:
>>>>
>>>>
>>>>
>> https://github.com/mrg/cbe/blob/master/FetchingObjects/Aggregates/src/main/java/cbe/fetching/Aggregates.java
>>>>
>>>> mrg
>>>>
>>>>
>>>> On Mon, Feb 29, 2016 at 7:25 AM, Hugi Thordarson <hu...@karlmenn.is>
>> wrote:
>>>>
>>>>> Hi all,
>>>>>
>>>>> Is there any simple way or me to count the number of destination
>> objects
>>>>> of a to-many relationship? (without fetching) If not, is there some
>> way to
>>>>> generate an expression from a relationship, so I can use that
>> expression in
>>>>> my own counting functions?
>>>>>
>>>>> Cheers,
>>>>> - hugi
>>>>
>>>>
>>>>
>>
>>
Re: Counting the number of destination objects for a to-many
relationship (or checking if empty)
Posted by Michael Gentry <mg...@masslight.net>.
Hmm, I'll look into the connection issue. Perhaps I did something
incorrectly. I'll look at your CayenneUtils. Always in the market for
more ideas. :-)
I really should get around to finishing/updating/expanding/etc CBE ... one
of these days.
Thanks,
mrg
On Tue, Mar 1, 2016 at 5:12 AM, Hugi Thordarson <hu...@karlmenn.is> wrote:
> Hi Michael!
>
> Thanks for the pointer. I did try to use your AggregateUtils with Cayenne
> 4.0 a while back but it’s not API compatible with 4.0. I was able to get it
> to work with some changes, but turned out I was creating a new DB
> connection for each DB function invocation and some other things I didn’t
> quite like. So I ended up constructing my own utility class that uses EJBQL
> (still determining how sensible that was).
>
>
> https://bitbucket.org/loftfar/jambalaya/src/22d799925a2c110b4823b36d335889e2ccfc3d06/src/main/java/jambalaya/CayenneUtils.java?at=master&fileviewer=file-view-default
>
> But thanks for CBE! IT’s been one of the most useful resources for me
> while learning Cayenne.
>
> Cheers,
> - hugi
>
>
>
> > On 29. feb. 2016, at 18:09, Michael Gentry <mg...@masslight.net>
> wrote:
> >
> > PS. I think the code works correctly, but let me know if it doesn't.
> >
> >
> > On Mon, Feb 29, 2016 at 1:09 PM, Michael Gentry <mg...@masslight.net>
> > wrote:
> >
> >> Hi Hugi,
> >>
> >> I'm not sure if this will work for 4.0 (since earlier you said you are
> >> using it), but this is what I'd use for 3.1:
> >>
> >>
> >>
> https://github.com/mrg/cbe/blob/master/FetchingObjects/Aggregates/src/main/java/cbe/fetching/utilities/AggregateUtils.java
> >>
> >> If you have A->>B, you'd construct a SelectQuery to fetch B's where toA
> =
> >> your A object. Basically, invert your query and count the matches.
> >> Something like:
> >>
> >> SelectQuery selectQuery = new SelectQuery(B.class);
> >> selectQuery.setQualifier(ExpressionFactory.matchExp("toA", a));
> >> int countOfB = AggregateUtils.count(dataContext, selectQuery);
> >>
> >> Some other example uses:
> >>
> >>
> >>
> https://github.com/mrg/cbe/blob/master/FetchingObjects/Aggregates/src/main/java/cbe/fetching/Aggregates.java
> >>
> >> mrg
> >>
> >>
> >> On Mon, Feb 29, 2016 at 7:25 AM, Hugi Thordarson <hu...@karlmenn.is>
> wrote:
> >>
> >>> Hi all,
> >>>
> >>> Is there any simple way or me to count the number of destination
> objects
> >>> of a to-many relationship? (without fetching) If not, is there some
> way to
> >>> generate an expression from a relationship, so I can use that
> expression in
> >>> my own counting functions?
> >>>
> >>> Cheers,
> >>> - hugi
> >>
> >>
> >>
>
>
Re: Counting the number of destination objects for a to-many relationship (or checking if empty)
Posted by Hugi Thordarson <hu...@karlmenn.is>.
Hi Michael!
Thanks for the pointer. I did try to use your AggregateUtils with Cayenne 4.0 a while back but it’s not API compatible with 4.0. I was able to get it to work with some changes, but turned out I was creating a new DB connection for each DB function invocation and some other things I didn’t quite like. So I ended up constructing my own utility class that uses EJBQL (still determining how sensible that was).
https://bitbucket.org/loftfar/jambalaya/src/22d799925a2c110b4823b36d335889e2ccfc3d06/src/main/java/jambalaya/CayenneUtils.java?at=master&fileviewer=file-view-default
But thanks for CBE! IT’s been one of the most useful resources for me while learning Cayenne.
Cheers,
- hugi
> On 29. feb. 2016, at 18:09, Michael Gentry <mg...@masslight.net> wrote:
>
> PS. I think the code works correctly, but let me know if it doesn't.
>
>
> On Mon, Feb 29, 2016 at 1:09 PM, Michael Gentry <mg...@masslight.net>
> wrote:
>
>> Hi Hugi,
>>
>> I'm not sure if this will work for 4.0 (since earlier you said you are
>> using it), but this is what I'd use for 3.1:
>>
>>
>> https://github.com/mrg/cbe/blob/master/FetchingObjects/Aggregates/src/main/java/cbe/fetching/utilities/AggregateUtils.java
>>
>> If you have A->>B, you'd construct a SelectQuery to fetch B's where toA =
>> your A object. Basically, invert your query and count the matches.
>> Something like:
>>
>> SelectQuery selectQuery = new SelectQuery(B.class);
>> selectQuery.setQualifier(ExpressionFactory.matchExp("toA", a));
>> int countOfB = AggregateUtils.count(dataContext, selectQuery);
>>
>> Some other example uses:
>>
>>
>> https://github.com/mrg/cbe/blob/master/FetchingObjects/Aggregates/src/main/java/cbe/fetching/Aggregates.java
>>
>> mrg
>>
>>
>> On Mon, Feb 29, 2016 at 7:25 AM, Hugi Thordarson <hu...@karlmenn.is> wrote:
>>
>>> Hi all,
>>>
>>> Is there any simple way or me to count the number of destination objects
>>> of a to-many relationship? (without fetching) If not, is there some way to
>>> generate an expression from a relationship, so I can use that expression in
>>> my own counting functions?
>>>
>>> Cheers,
>>> - hugi
>>
>>
>>
Re: Counting the number of destination objects for a to-many
relationship (or checking if empty)
Posted by Michael Gentry <mg...@masslight.net>.
PS. I think the code works correctly, but let me know if it doesn't.
On Mon, Feb 29, 2016 at 1:09 PM, Michael Gentry <mg...@masslight.net>
wrote:
> Hi Hugi,
>
> I'm not sure if this will work for 4.0 (since earlier you said you are
> using it), but this is what I'd use for 3.1:
>
>
> https://github.com/mrg/cbe/blob/master/FetchingObjects/Aggregates/src/main/java/cbe/fetching/utilities/AggregateUtils.java
>
> If you have A->>B, you'd construct a SelectQuery to fetch B's where toA =
> your A object. Basically, invert your query and count the matches.
> Something like:
>
> SelectQuery selectQuery = new SelectQuery(B.class);
> selectQuery.setQualifier(ExpressionFactory.matchExp("toA", a));
> int countOfB = AggregateUtils.count(dataContext, selectQuery);
>
> Some other example uses:
>
>
> https://github.com/mrg/cbe/blob/master/FetchingObjects/Aggregates/src/main/java/cbe/fetching/Aggregates.java
>
> mrg
>
>
> On Mon, Feb 29, 2016 at 7:25 AM, Hugi Thordarson <hu...@karlmenn.is> wrote:
>
>> Hi all,
>>
>> Is there any simple way or me to count the number of destination objects
>> of a to-many relationship? (without fetching) If not, is there some way to
>> generate an expression from a relationship, so I can use that expression in
>> my own counting functions?
>>
>> Cheers,
>> - hugi
>
>
>
Re: Counting the number of destination objects for a to-many
relationship (or checking if empty)
Posted by Michael Gentry <mg...@masslight.net>.
Hi Hugi,
I'm not sure if this will work for 4.0 (since earlier you said you are
using it), but this is what I'd use for 3.1:
https://github.com/mrg/cbe/blob/master/FetchingObjects/Aggregates/src/main/java/cbe/fetching/utilities/AggregateUtils.java
If you have A->>B, you'd construct a SelectQuery to fetch B's where toA =
your A object. Basically, invert your query and count the matches.
Something like:
SelectQuery selectQuery = new SelectQuery(B.class);
selectQuery.setQualifier(ExpressionFactory.matchExp("toA", a));
int countOfB = AggregateUtils.count(dataContext, selectQuery);
Some other example uses:
https://github.com/mrg/cbe/blob/master/FetchingObjects/Aggregates/src/main/java/cbe/fetching/Aggregates.java
mrg
On Mon, Feb 29, 2016 at 7:25 AM, Hugi Thordarson <hu...@karlmenn.is> wrote:
> Hi all,
>
> Is there any simple way or me to count the number of destination objects
> of a to-many relationship? (without fetching) If not, is there some way to
> generate an expression from a relationship, so I can use that expression in
> my own counting functions?
>
> Cheers,
> - hugi