cancel
Showing results for 
Search instead for 
Did you mean: 

CAP Java - Getting entity from within Java code

timrehrmann
Explorer
0 Kudos

Hello there!

This might be a stupid question but I really don't seem to get it to work.

I was following this SAP CAP Java tutorial and everything works perfectly fine:

https://developers.sap.com/mission.cap-java-app.html

Now, I want to have some custom stuff happening which needs to get data from the underlying entities after a POST request. So I added an @After Event Handler on the POST event and a call to a custom Java class with the ID of the posted entity. In this Java class I would like to get the entity that was just posted by ID and possibly some other entities from the underlying database as well for further processing.

How can I get an entity by ID from within the Java Code?

It seems to me that this is only possible via a REST call, but maybe I'm really missing something.

Maybe I'm also thinking of this wrong and there actually is a different best practice way of processing the data in Java.

Thanks and kind regards,

Tim

Accepted Solutions (1)

Accepted Solutions (1)

marcbecker
Contributor

You can build CQN statements to query data directly from the database or from other services.

For your use-case you will want to build a CqnSelect statement:

Select.from("bookshop.Books").byId(101);

You can then execute the query on the PersistenceService (database) or any other service that you have defined in your CDS model. See here for more information how to execute the query. And here for more information how to get access to the service objects.

You might also want to take a look at our CAP Java application, which also shows a lot of these things in a working application: https://github.com/SAP-samples/cloud-cap-samples-java

Answers (0)