You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Sheldon <wa...@web.de> on 2012/03/30 11:08:15 UTC

select over PKs

Hello,

can I fetch data from db with a list of private keys? One possibility is to
do this with SQLTemplate and than create a SELECT statement.
But I want to do this like:
Cayenne.objectForPK(context, Class, List of PKs)

Sheldon

--
View this message in context: http://cayenne.195.n3.nabble.com/select-over-PKs-tp3870305p3870305.html
Sent from the Cayenne - User mailing list archive at Nabble.com.

Re: select over PKs

Posted by Aristedes Maniatis <ar...@maniatis.org>.
On 2/04/12 5:49 PM, Sheldon wrote:
> If I specify a pageSize than I get any PK of objects.
> I want, that I know a list of PKs and get their Objects.

I suspect that the suggestion here is to think about using Cayenne in a different way. What you are trying to do often suggests that you are working against the ORM rather than with it. Of course it is certainly possible you have a legitimate use for this approach, but often when people ask this question the answer is "Think about how you got that list of PKs in the first place and what you are wanting to do with them. Instead of passing PKs around your application, just use objects that these PKs point to." If you get these PKs from outside your application, you may have no option. But if you are generating them inside the application, then don't do that.

Does that help at all?

Cheers
Ari


-- 
-------------------------->
Aristedes Maniatis
GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A

Re: select over PKs

Posted by gilbertoca <gi...@gmail.com>.
Sheldon wrote
> 
> If I specify a pageSize than I get any PK of objects.
> I want, that I know a list of PKs and get their Objects.
> 
> Sheldon
> 

You can add a NamedQuery[1] in the modeler, for example:
<pre>
	<query name="Vehicle.findInLicensePlate"
factory="org.apache.cayenne.map.SelectQueryBuilder" root="obj-entity"
root-name="Vehicle">
		<qualifier></qualifier>
	</query>        
	<query name="Vehicle.findByLicensePlate"
factory="org.apache.cayenne.map.SelectQueryBuilder" root="obj-entity"
root-name="Vehicle">
		<qualifier></qualifier>
	</query>
</pre>

Then [2]:
<pre>
    /**
     * Test of Vehicle.findInLicensePlate named query.
     */
    @Test
    public void findInLicensePlate() {
        System.out.println("\nGetting an Vehicle IN
Vehicle.findInLicensePlate.\n");
        //JPA-->Query query =
em.createNamedQuery("Vehicle.findInLicensePlate");
        //JPA-->query.setParameter("licensePlate", new
String[]{"LC101","LC102"});
        //JPA-->int size = query.getResultList().size();
        ObjectContext context = runtime.getContext();
        NamedQuery query = new NamedQuery("Vehicle.findInLicensePlate",
Collections.singletonMap("licensePlate", new String[]{"LC101","LC102"}));
        List entities = context.performQuery(query);
        assertEquals("Should have 2 Vehicle", 2, entities.size());
    }

</pre>

Hth,

Gilberto

[1]
http://cayenne.apache.org/doc/api/org/apache/cayenne/query/NamedQuery.html
[2]
http://code.google.com/p/construtor/source/browse/trunk/park-samples/park-cayenne/src/test/java/park/model/VehicleTest.java

--
View this message in context: http://cayenne.195.n3.nabble.com/select-over-PKs-tp3870305p3878913.html
Sent from the Cayenne - User mailing list archive at Nabble.com.

Re: select over PKs

Posted by Sheldon <wa...@web.de>.
If I specify a pageSize than I get any PK of objects.
I want, that I know a list of PKs and get their Objects.

Sheldon

--
View this message in context: http://cayenne.195.n3.nabble.com/select-over-PKs-tp3870305p3876993.html
Sent from the Cayenne - User mailing list archive at Nabble.com.

Re: select over PKs

Posted by Michael Gentry <mg...@masslight.net>.
Hi Sheldon,

I guess I don't understand why you can't specify a page size (say 100
or 1000) on your query (unless you are getting the list of PKs from an
external source), especially since you want actual Java objects in the
end.

mrg


On Fri, Mar 30, 2012 at 10:45 AM, Sheldon <wa...@web.de> wrote:
> Hello Michael,
>
> I want to use a SelectQuery (but without using of a String "select ...")
>
> Thanks
> Sheldon
>
> --
> View this message in context: http://cayenne.195.n3.nabble.com/select-over-PKs-tp3870305p3871032.html
> Sent from the Cayenne - User mailing list archive at Nabble.com.

Re: select over PKs

Posted by Sheldon <wa...@web.de>.
Hello Michael,

I want to use a SelectQuery (but without using of a String "select ...")

Thanks
Sheldon

--
View this message in context: http://cayenne.195.n3.nabble.com/select-over-PKs-tp3870305p3871032.html
Sent from the Cayenne - User mailing list archive at Nabble.com.

Re: select over PKs

Posted by Michael Gentry <mg...@masslight.net>.
Hi Sheldon,

Is your list of PKs coming from an external source or are you wanting
to use a SelectQuery?

Thanks,

mrg


On Fri, Mar 30, 2012 at 9:28 AM, Sheldon <wa...@web.de> wrote:
> Yes I mean primary key.
> For example I have a list of primary keys (that should not be random list
> keys) and I want to fetch the related objects of these keys.
> Is this possible?
>
> Sheldon
>
> --
> View this message in context: http://cayenne.195.n3.nabble.com/select-over-PKs-tp3870305p3870799.html
> Sent from the Cayenne - User mailing list archive at Nabble.com.

Re: select over PKs

Posted by Sheldon <wa...@web.de>.
Yes I mean primary key.
For example I have a list of primary keys (that should not be random list
keys) and I want to fetch the related objects of these keys.
Is this possible?

Sheldon

--
View this message in context: http://cayenne.195.n3.nabble.com/select-over-PKs-tp3870305p3870799.html
Sent from the Cayenne - User mailing list archive at Nabble.com.

Re: select over PKs

Posted by Michael Gentry <mg...@masslight.net>.
Hi Sheldon,

Like Andrew, I think you meant "primary keys" rather than "private
keys".  I'm going to guess at what you really are trying to do, but I
may fail.  :-)

You can have Cayenne fetch primary keys only instead of full objects
by using a paginated query:

http://cayenne.apache.org/doc30/paginated-queries.html

If your query returns 10,000 rows, you essentially have fetched 10,000
PKs.  When you access a "page" in the paginated query, Cayenne will
automatically inflate the objects on that page (and only the page you
accessed) with actual Java objects.

If this isn't what you are looking for, please explain more.

Thanks!

mrg


On Fri, Mar 30, 2012 at 5:08 AM, Sheldon <wa...@web.de> wrote:
> Hello,
>
> can I fetch data from db with a list of private keys? One possibility is to
> do this with SQLTemplate and than create a SELECT statement.
> But I want to do this like:
> Cayenne.objectForPK(context, Class, List of PKs)
>
> Sheldon
>
> --
> View this message in context: http://cayenne.195.n3.nabble.com/select-over-PKs-tp3870305p3870305.html
> Sent from the Cayenne - User mailing list archive at Nabble.com.

Re: select over PKs

Posted by Andrew Lindesay <ap...@lindesay.co.nz>.
Hi Sheldon;

I think you mean "primary keys" rather than "private keys"?

I have recently (re)put some material I have written around Cayenne 
(+more) online;

	http://www.silvereye.co.nz/lestuff.html

You may find something ~similar~ to what you are looking for in;

	LECayHelper.queryForObjectIds(..)

Regards;

On 30/03/12 10:08 PM, Sheldon wrote:
> Hello,
>
> can I fetch data from db with a list of private keys? One possibility is to
> do this with SQLTemplate and than create a SELECT statement.
> But I want to do this like:
> Cayenne.objectForPK(context, Class, List of PKs)
>
> Sheldon
>
> --
> View this message in context: http://cayenne.195.n3.nabble.com/select-over-PKs-tp3870305p3870305.html
> Sent from the Cayenne - User mailing list archive at Nabble.com.
>


-- 
Andrew Lindesay
www.silvereye.co.nz