SAP CAP Blog Posts
cancel
Showing results for 
Search instead for 
Did you mean: 
qmacro
Developer Advocate
Developer Advocate
527

When defining a service, it's common to expose targets that are related. The classic example might be books and authors:

using bookshop as my from '../db/schema';

service AdminService {
  entity ListOfBooks as projection on my.Books excluding { price };
  entity Books       as projection on my.Books;
  entity Authors     as projection on my.Authors;
}

Given the expected relationship between Books and Authors at the schema level, the problem here is that the compiler cannot automatically determine whether Authors.books should refer to ListOfBooks or Books, and emits an error.

To resolve the ambiguity we can use the redirected to for the element, like this, where we say that Authors.books should refer to Books:

using bookshop as my from '../db/schema';

service AdminService {
  entity ListOfBooks as projection on my.Books excluding { price };
  entity Books       as projection on my.Books;

  entity Authors     as
    projection on my.Authors {
      *,
      books : redirected to Books
    };
}

Instead of creating an explicit block for the authors projection in order to add the redirected to to the books element, we can also use an annotation to point to the desired association target:

using bookshop as my from '../db/schema';

service AdminService {
  entity ListOfBooks as projection on my.Books;

  @this.is.the.target
  entity Books       as projection on my.Books;

  entity Authors     as projection on my.Authors;
}

Clearly the annotation is not @this.is.the.target, but it shows where and how we would employ it. But what is the annotation name?

This is a question from the June Developer Challenge on CAP Knowledge. And don't forget: always submit your answer as a hash, on its own - read the Taking part section of the intro post for more info. At the end of today, this question will be updated with links to further reading on this topic.

Further info:

25 Comments