Search code examples
javamysqlapache-cayenne

How to retrieve only the PKs of a table


I'm working with Java Apache Cayenne, under a MySQL DB. I have quite a large table with a single bigint PK and some fields. I'd like to retrieve just only the PK values and not all the object that maps this entity, as it would be too resource-consuming.

Is there a snippet that I can use, instead of this one that retrieves all the objects?

    ObjectContext context = ... 
    SelectQuery select = new SelectQuery(MyClass.class);
    List<MyClass> result = context.performQuery(select);

Solution

  • You should try using SQLTemplate instead of SelectQuery. Here's a quick example:

    ObjectContext context = ... 
    SQLTemplate select = new SQLTemplate(MyClass.class, "SELECT #result('PK_COLUMN' 'long') FROM MY_TABLE");
    List result = context.performQuery(select);
    

    You can find more information here