Skip to Content

CDS OData Java service


I am new in this forum, and new to SAP Cloud Foundry/CAP. I can not find a solution to the below problem anywhere.

I have created a Odata Java service using the CAP Java Maven Archetype as mentioned here:

Other than splitting entities and services in two files, eventually I have combined them into one cds file:

service service {
	entity Case {
		key ID : Integer;
		caseNumber : Integer;
		description : String;
		phase  : String;

Next, I have created a CaseImpl, which extends HashMap<String, Object> and it implements the generated 'Case' interface. All four properties are provided in the CaseImpl, like this:

	public Integer getId() {
		return (Integer) get(ID);

	public void setId(final Integer id) {
		put(ID, id);

So, basically, CaseImpl is a HashMap containing all necessary String key / Object value combinations.

Then I have added a custom event handler, as mentioned here:

It contains this method:

	@On(event = CdsService.EVENT_READ, entity = "service.Case")
	public void onReadCases(final CdsReadEventContext context) {


... the getCases() method returns a collection of four CaseImpl instances that are identified by ID= 1, 2, 3 and 4.

And the entire collection is shown perfectly when calling /odata/v4/service/Case

However, when calling /odata/v4/service/Case(1) I expect to see only Case with ID=1, but I get: Result contains more than one row
	at ~[cds4j-core-1.8.0.jar:na]
	at ~[cds-adapter-odata-v4-1.4.0.jar:na]
	at$processReadRequest$6( ~[cds-adapter-odata-v4-1.4.0.jar:na]
	at ~[cds-services-impl-1.4.0.jar:na]
	at ~[cds-adapter-odata-v4-1.4.0.jar:na]
	at ~[cds-adapter-odata-v4-1.4.0.jar:na]
	at$processChangesetRequest$0( ~[cds-adapter-odata-v4-1.4.0.jar:na]
	at$runInReqContextIfNecessary$2( ~[cds-adapter-odata-v4-1.4.0.jar:na]
	at ~[cds-services-impl-1.4.0.jar:na]
	at ~[cds-adapter-odata-v4-1.4.0.jar:na]
	at ~[cds-adapter-odata-v4-1.4.0.jar:na]
	at ~[cds-adapter-odata-v4-1.4.0.jar:na]
	at ~[cds-adapter-odata-v4-1.4.0.jar:na]
	at ~[cds-adapter-odata-v4-1.4.0.jar:na]

(etc., no cause)

Also, when calling /odata/v4/service/Case?$top=2, all four cases are shown. As if the filter is ignored.

What am I doing wrong? The CDS version is 1.4.0.

Thanks in advance,

Kind regards,


Add a comment
10|10000 characters needed characters exceeded

Related questions

1 Answer

  • Posted on May 07 at 04:17 PM

    Well, boy meets world, I understand what we did wrong now.

    When implemeting the @On handle, apparently CDS expects you to provide exactly what is asked for. I assumed we could provide the entire collection of entities (Case) and that CDS would do any fitering for us.

    It appears CDS does exactly that, as long as it can retrieve the data from the database.

    The thing is, we need to be able to provide the lastest Cases to the client, which we retrieve from a backend. Not those cached in a database.

    We solved our problem by deleting the @On handle, and creating a @Before handle where we delete all Cases from the database, and insert the latest versions. Then CDS picks up those cases and it does any necessary filtering for us.

    	@Before(event = CdsService.EVENT_READ, entity = "service.Case")
    	public void beforeReadCases(final CdsReadEventContext context) {
    		final CdsService service = context.getService();
    		// Delete all cases from the database
    		final CqnDelete delete = Delete.from("service.Case");;
    		// Put new cases in the database
    		final CqnInsert insert = Insert.into("service.Case").entries(caseDao.getCases());;
    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.