1

This is kind of related to my previous question where I discussed finding rows by foreign key IDs. This question is directed at finding a list of objects by primary key ID.

In the below code I am trying to find a list of Person objects by providing a list of ids. But Apache Cayenne cannot do that because ID_PK_COLUMN is a string and not a Property.

ObjectSelect
    .query(Person::class.java)
    .where(Person.ID_PK_COLUMN.in(listOfIds)) // <- Cannot perform this
    .select(context)

How can I find a list of Person objects by ID?

I know we have Cayenne.objectForPK but that only finds one object.

Using Apache Cayenne 4.1.

gurpreet-
  • 509
  • 8
  • 18
  • What do you get from just: `val expression = Person.ID_PK_COLUMN.in(listOfIds)` ? – Andre Artus Dec 01 '18 at 00:26
  • 1
    @AndreArtus: Well, `Person.ID_PK_COLUMN` returns a `String` so the `.in()` method does not work on it, which is why I wrote that I can't perform `.in()` on that particular expression. I think @andrus solved my answer. – gurpreet- Dec 01 '18 at 19:46

1 Answers1

3

Since ids are generally not mapped as object properties in Cayenne and "ID_PK_COLUMN" is a "db:" property, you need to build a "db" expression for the "where" method argument. There's an API for that:

ExpressionFactory.inDbExp(Person.ID_PK_COLUMN, listOfIds)

(BTW, Property is just syntactic sugar on top of ExpressionFactory)

andrus_a
  • 2,528
  • 1
  • 16
  • 10
  • Thank you for your answer, it works. Apache Cayenne is so powerful and fast, I wish more people knew about it. – gurpreet- Dec 01 '18 at 19:48