Skip to content

Commit 2d4f58a

Browse files
Merge branch 'main' into rjegl08/switchSampleLinks
2 parents 8d2f5ec + 6aae16e commit 2d4f58a

5 files changed

Lines changed: 194 additions & 65 deletions

File tree

get-started/in-a-nutshell.md

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,28 @@ Build Your First App with CAP {.subtitle}
1717

1818

1919

20+
::: details Optionally clone sample from GitHub ...
21+
22+
The sections below describe a hands-on walkthrough, in which you'd create a new project and fill it with content step by step. Alternatively, you can fast forward and get the final sample content from GitHub as follows:
23+
24+
::: code-group
25+
26+
```sh [Node.js]
27+
git clone https://github.com/capire/bookshop
28+
cd bookshop
29+
npm install
30+
```
31+
32+
```sh [Java]
33+
git clone https://github.com/sap-samples/cloud-cap-samples-java bookshop
34+
```
35+
36+
Note: When comparing the code from the repo in GitHub to the snippets given in the sections below you will recognise additions showcasing enhanced features. So, what you find in there is a superset of what we describe in this getting started guide.
37+
38+
:::
39+
40+
41+
2042
## Jumpstart a Project {#jumpstart}
2143

2244

@@ -88,28 +110,6 @@ After you completed the [*Initial Setup*](./), you jumpstart a project as follow
88110

89111
:::
90112

91-
92-
::: details Optionally clone sample from GitHub ...
93-
94-
The sections below describe a hands-on walkthrough, in which you'd create a new project and fill it with content step by step. Alternatively, you can get the final sample content from GitHub as follows:
95-
96-
::: code-group
97-
98-
```sh [Node.js]
99-
git clone https://github.com/capire/bookshop
100-
cd bookshop
101-
```
102-
103-
```sh [Java]
104-
git clone https://github.com/sap-samples/cloud-cap-samples-java bookshop
105-
```
106-
107-
Note: When comparing the code from the *cap/samples* on GitHub to the snippets given in the sections below you will recognise additions showcasing enhanced features. So, what you find in there is a superset of what we describe in this getting started guide.
108-
109-
:::
110-
111-
112-
113113
## Capture Domain Models
114114

115115

@@ -148,7 +148,7 @@ entity Genres : sap.common.CodeList { // [!code focus]
148148
```
149149
:::
150150

151-
*Find this source also in `capire/bookshop` [for Node.js](https://github.com/capire/bookshop/blob/main/db/schema.cds), and [for Java](https://github.com/SAP-samples/cloud-cap-samples-java/blob/main/db/books.cds)*{ .learn-more}
151+
_Find this source also in the GitHub repos [for Node.js](https://github.com/capire/bookshop/tree/main/db/schema.cds), and [for Java](https://github.com/sap-samples/cloud-cap-samples-java/blob/main/db/books.cds)_{ .learn-more}
152152
[Learn more about **Domain Modeling**.](../guides/domain-modeling){ .learn-more}
153153
[Learn more about **CDS Modeling Languages**.](../cds/){ .learn-more}
154154

@@ -160,7 +160,7 @@ entity Genres : sap.common.CodeList { // [!code focus]
160160
As soon as you save the *schema.cds* file, the still running `cds watch` reacts immediately with new output like this:
161161

162162
```log
163-
[cds] - connect to db > sqlite { database: ':memory:' }
163+
[cds] - connect to db > sqlite { url: ':memory:' }
164164
/> successfully deployed to in-memory database.
165165
```
166166

@@ -246,7 +246,7 @@ service CatalogService @(path:'/browse') { // [!code focus]
246246
```
247247
:::
248248

249-
*Find this source also on GitHub [for Node.js](https://github.com/capire/bookshop/tree/main/srv), and [for Java](https://github.com/SAP-samples/cloud-cap-samples-java/tree/main/srv)*{.learn-more}
249+
*Find this source also on GitHub [for Node.js](https://github.com/capire/bookshop/tree/main/srv), and [for Java](https://github.com/sap-samples/cloud-cap-samples-java/blob/main/srv)*{.learn-more}
250250

251251
[Learn more about **Defining Services**.](../guides/providing-services){ .learn-more}
252252

@@ -259,13 +259,20 @@ service CatalogService @(path:'/browse') { // [!code focus]
259259
This time `cds watch` reacted with additional output like this:
260260

261261
```log
262-
[cds] - serving AdminService { at: '/odata/v4/admin' }
263-
[cds] - serving CatalogService { at: '/browse' }
264-
262+
[cds] - serving AdminService {
263+
at: [ '/admin' ],
264+
decl: 'srv/admin-service.cds:2',
265+
impl: 'srv/admin-service.js'
266+
}
267+
[cds] - serving CatalogService {
268+
at: [ '/browse', '/rest/catalog', '/hcql/catalog' ],
269+
decl: 'srv/cat-service.cds:4',
270+
impl: 'srv/cat-service.js'
271+
}
265272
[cds] - server listening on { url: 'http://localhost:4004' }
266273
```
267274

268-
As you can see, the two service definitions have been compiled and generic service providers have been constructed to serve requests on the listed endpoints _/odata/v4/admin_ and _/browse_.
275+
As you can see, the two service definitions have been compiled and generic service providers have been constructed to serve OData requests on the listed endpoints _/admin_ and _/browse_.
269276

270277
</span>
271278

@@ -377,7 +384,7 @@ ID,name
377384
::: details `cds add data` can help you with the file and record generation
378385
Create empty CSV files with header lines only:
379386

380-
```sh
387+
```sh
381388
cds add data
382389
```
383390

@@ -389,24 +396,20 @@ cds add data --records 10
389396
[Find the full set of options here.](../tools/cds-cli.md#data){.learn-more}
390397
:::
391398

392-
[Find a full set of `.csv` files in **capire/bookshop**.](https://github.com/capire/bookshop/tree/main/db/data){ .learn-more target="_blank"}
393-
394399
<span class="impl node">
395400

396401
After you've added these files, `cds watch` restarts the server with output, telling us that the files have been detected and their content has been loaded into the database automatically:
397402

398403
```log
399404
[cds] - connect to db > sqlite { url: ':memory:' }
400-
> init from db/init.js
401-
> init from db/data/sap.capire.bookshop-Genres.csv
402-
> init from db/data/sap.capire.bookshop-Books.texts.csv
403-
> init from db/data/sap.capire.bookshop-Books.csv
404-
> init from db/data/sap.capire.bookshop-Authors.csv
405+
> init from db/init.js
406+
> init from db/data/sap.capire.bookshop-Authors.csv
407+
> init from db/data/sap.capire.bookshop-Books.csv
408+
> init from db/data/sap.capire.bookshop-Books_texts.csv
409+
> init from db/data/sap.capire.bookshop-Genres.csv
405410
/> successfully deployed to in-memory database.
406411
```
407412

408-
> Note: This is the output when you're using the [bookshop sample](https://github.com/capire/bookshop). It's less if you've followed the manual steps here.
409-
410413
</span>
411414

412415
<span class="impl java">
@@ -511,7 +514,7 @@ CAP provides out-of-the-box support for SAP Fiori UIs, for example, with respect
511514
### Vue.js UIs {#vue .impl .node}
512515

513516
Besides Fiori UIs, CAP services can be consumed from any UI frontends using standard AJAX requests.
514-
For example, you can [find a simple Vue.js app in **capire/bookshop**](https://github.com//capire/bookshop/tree/main/app/vue), which demonstrates browsing and ordering books using OData requests to [the `CatalogService` API we defined above](#services). {.impl .node}
517+
For example, you can [find a simple Vue.js app in the GitHub repo](https://github.com/capire/bookshop/tree/main/app/vue), which demonstrates browsing and ordering books using OData requests to [the `CatalogService` API we defined above](#services). {.impl .node}
515518

516519
![Shows the famous bookshop catalog service in a simple Vue.js UI.](assets/vue-app.png){style="margin:0" .impl .node .adapt}
517520

@@ -540,7 +543,7 @@ bookshop/
540543
└─ ...
541544
```
542545

543-
[See these files also in **capire/bookshop** in the _srv_ folder.](https://github.com//capire/bookshop/tree/main/srv){.learn-more}
546+
[See these files also in the GitHub repo.](https://github.com/capire/bookshop/tree/main/srv){.learn-more}
544547
[Learn more about providing service implementations **in Node.js**.](../node.js/core-services#implementing-services){.learn-more .impl .node}
545548
[Learn also **how to do that in Java** using Event Handler Classes.](../java/event-handlers/#handlerclasses){.learn-more .impl .java}
546549

@@ -765,8 +768,8 @@ public class SubmitOrderHandler implements EventHandler {
765768

766769
</span>
767770

768-
[Find this source also in **capire/bookshop**.](https://github.com//capire/bookshop/tree/main/srv/cat-service.js){ .learn-more .impl .node target="_blank"}
769-
[Find this source also in **cap/samples**.](https://github.com/SAP-samples/cloud-cap-samples-java/blob/main/srv/src/main/java/my/bookshop/handlers/CatalogServiceHandler.java#L166){ .impl .java .learn-more target="_blank"}
771+
[Find this source also in the GitHub repo.](https://github.com/capire/bookshop/tree/main/srv/cat-service.js){ .learn-more .impl .node target="_blank"}
772+
[Find this source also in the GitHub repo.](https://github.com/sap-samples/cloud-cap-samples-java/blob/main/srv/src/main/java/my/bookshop/handlers/CatalogServiceHandler.java#L166){ .impl .java .learn-more target="_blank"}
770773
[Learn more about **connecting to services** using `cds.connect`.](../node.js/cds-connect){ .learn-more .impl .node}
771774
[Learn more about **connecting to services** using `@Autowired`, `com.sap.cds.ql`, etc.](../java/services){.learn-more .impl .java}
772775
[Learn more about **reading and writing data** using `cds.ql`.](../node.js/cds-ql){ .learn-more .impl .node}

java/cds-data.md

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -473,24 +473,7 @@ The name of the CDS element referred to by a getter or setter, is defined throug
473473

474474
### Generated Accessor Interfaces {#generated-accessor-interfaces}
475475

476-
For all structured types of the CDS model, accessor interfaces can be generated using the [CDS Maven Plugin](./cqn-services/persistence-services#staticmodel). The generated accessor interfaces allow for hybrid access and easy serialization to JSON.
477-
478-
By default, the accessor interfaces provide the setter and getter methods inspired by the JavaBeans specification.
479-
480-
Following example uses accessor interfaces that have been generated with the default (JavaBeans) style:
481-
482-
```java
483-
Authors author = Authors.create();
484-
author.setName("Emily Brontë");
485-
486-
Books book = Books.create();
487-
book.setAuthor(author);
488-
book.setTitle("Wuthering Heights");
489-
```
490-
491-
Alternatively, you can generate accessor interfaces in _fluent style_. In this mode, the getter methods are named after the property names. To enable fluent chaining, the setter methods return the accessor interface itself.
492-
493-
Following is an example of the fluent style:
476+
For all structured types of the CDS model, accessor interfaces can be generated using the [CDS Maven Plugin](/java/assets/cds-maven-plugin-site/plugin-info.html). The generated accessor interfaces allow for hybrid access and easy serialization to JSON. Code generation is executed by default at build time and is configurable.
494477

495478
```java
496479
Authors author = Authors.create().name("Emily Brontë");
@@ -505,6 +488,8 @@ The way the interfaces are generated determines only how data is accessed by cus
505488

506489
Moreover, it doesn't change the way how event contexts and entities, delivered by CAP, look like. Such interfaces from CAP are always modelled in the default JavaBeans style.
507490

491+
See more in [Configuring Code Generation for Typed Access](/java/developing-applications/building#codegen-config) for advanced options. {.learn-more}
492+
508493
#### Renaming Elements in Java
509494

510495
Element names used in the CDS model might conflict with reserved [Java keywords](https://docs.oracle.com/javase/specs/jls/se13/html/jls-3.html#jls-3.9) (`class`, `private`, `transient`, etc.). In this case, the `@cds.java.name` annotation must be used to specify an alternative property name that will be used for the generation of accessor interfaces and [static model](./cqn-services/persistence-services#staticmodel) interfaces. The element name used as key in the underlying map for [dynamic access](#entities-and-structured-types) isn't affected by this annotation.

java/developing-applications/building.md

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,148 @@ Use the _.cdsrc.json_ file to add project specific configuration of `@sap/cds-dk
376376
[Learn more about configuration and `cds.env`](../../node.js/cds-env){.learn-more}
377377

378378

379-
### Using a Local cds-dk
379+
## Code Generation for Typed Access {#codegen-config}
380+
381+
The [interfaces for typed access](../cds-data#generated-accessor-interfaces) are generated at each build
382+
by the [`cds:generate`](/java/assets/cds-maven-plugin-site/generate-mojo.html) goal of the [CDS Maven Plugin](/java/assets/cds-maven-plugin-site/plugin-info.html).
383+
384+
You configure this goal just like any other Maven plugin via its configuration options via your application's POM. For example:
385+
386+
```xml [pom.xml]
387+
<execution>
388+
<id>cds.generate</id>
389+
<goals>
390+
<goal>generate</goal>
391+
</goals>
392+
<configuration>
393+
<basePackage>cds.gen</basePackage>
394+
...
395+
</configuration>
396+
</execution>
397+
```
398+
399+
Each time your application is built, these interfaces are regenerated. By default, they are excluded from your version control.
400+
401+
### Package for Generated Code
402+
403+
The option [`basePackage`](/java/assets/cds-maven-plugin-site/generate-mojo.html#basePackage) can be used to specify a base package prefix for generated code. The suffix package structure will reflect namespaces defined in your CDS model.
404+
405+
### Filter for CDS Entities
406+
407+
By default, the complete model of your application is generated including all imported or re-used models.
408+
You can use options [`includes`](/java/assets/cds-maven-plugin-site/generate-mojo.html#includes) and [`excludes`](/java/assets/cds-maven-plugin-site/generate-mojo.html#excludes) to specify the part of your overall model that is subject to code generation. Both inclusion and exclusion can be used together, inclusion is evaluated first, then exclusion filters out of the included set of entities.
409+
410+
These options use patterns that are applied on the fully qualified names of the entities in CDS models. For example, the pattern `my.bookshop.*` will cover all definitions with namespace `my.bookshop` and the pattern `my.bookshop.**` will cover all definitions with fully qualified name starting with `my.bookshop`.
411+
412+
:::warning Cross-namespace references are not resolved
413+
Options `includes` and `excludes` are simple filters. If included parts of your model reference types from the excluded area, the resulting code will not compile.
414+
:::
415+
416+
### Style of Interfaces
417+
418+
By default, the accessor interfaces provide the setter and getter methods inspired by the JavaBeans specification. In this style, getter and setter method names are prefixed with `get` and `set`:
419+
420+
421+
```java
422+
Authors author = Authors.create();
423+
author.setName("Emily Brontë");
424+
425+
Books book = Books.create();
426+
book.setAuthor(author);
427+
book.setTitle("Wuthering Heights");
428+
```
429+
430+
Alternatively, you can generate accessor interfaces in _fluent style_. In this mode, the getter methods are named after the property names. To enable fluent chaining, the setter methods return the accessor interface itself:
431+
432+
433+
```java
434+
Authors author = Authors.create().name("Emily Brontë");
435+
Books.create().author(author).title("Wuthering Heights");
436+
```
437+
438+
The generation mode is configured by the option [`methodStyle`](/java/assets/cds-maven-plugin-site/generate-mojo.html#methodStyle). The selected style affects all entities and event contexts in your services. The default value is `BEAN`, which represents JavaBeans-style interfaces.
439+
440+
Once, when starting a project, decide on the style of the interfaces that is best for your team and project.
441+
442+
The way the interfaces are generated only determines how data is accessed by custom code. It does not affect how the data is represented in memory and handled by the CAP Java runtime.
443+
444+
Moreover, it doesn't change the way how event contexts, delivered by CAP, look like. Such interfaces from CAP are always modelled in the default JavaBeans style.
445+
446+
### Code Generation Features
447+
448+
Other options in this goal enable or disable certain features that change the way generated code looks in a certain aspect. These changes can be incompatible with the existing code and require manual adaptation.
449+
450+
- [`strictSetters`](/java/assets/cds-maven-plugin-site/generate-mojo.html#strictSetters)
451+
452+
This switch changes the signature of the setter methods in typed access interfaces so that they require concrete type instead of generic `Map` interface.
453+
For example:
454+
455+
```java
456+
void setManager(Map<String, ?> manager); // [!code --]
457+
void setManager(Manager manager); // [!code ++]
458+
```
459+
460+
It does not introduce any additional type checks at runtime, the correctness of the assignment is checked only at the time of compilation.
461+
462+
- [`interfacesForAspects`](/java/assets/cds-maven-plugin-site/generate-mojo.html#interfacesForAspects)
463+
464+
If your entity is modelled with the [composition of aspects](/cds/cdl#with-named-targets), the generated interfaces always reference original aspect as type for setters and getters.
465+
When this switch is enabled, the code generator uses the type generated by the compiler instead of the type of the aspect itself and will include methods to fetch keys, for example.
466+
467+
:::warning Limitations
468+
This is supported only for the named aspects (inline targets are not supported) and does not respect all possible options how such entities might be exposed by services.
469+
:::
470+
471+
- [`betterNames`](/java/assets/cds-maven-plugin-site/generate-mojo.html#betterNames)
472+
473+
CDS models from external sources might include elements that have some special characters in their names or include elements that clash with Java keywords. Such cases always can be solved with the [renaming features](/java/cds-data#renaming-elements-in-java) provided by code generator, but in case of large models, this is tedious.
474+
When this switch is enabled, characters `/` and `$` behave as a separators for the name during case conversions, similar to `_` and `.`. For example, `GET_MATERIAL` yields `GetMaterial` (or `getMaterial` for attributes and methods). The same now applies for the names with `/`, for example, name `/DMO/GET_MATERIAL` will be converted to `DmoGetMaterial`.
475+
476+
The following conversions are applied:
477+
- Names from CDS model that are Java keywords are suffixed with `_`.
478+
- Names from CDS model that use characters that are not valid as Java identifiers, are replaced by `_`. This, however, might lead to a conflicts between names that yield the same name in Java.
479+
- Leading `_` will remain in the name after conversions. This supports conventions where an association and its foreign key have names like `_assoc` and `assoc`.
480+
These conversions no longer influence the splitting.
481+
482+
- [`cqnServiceGetters`](/java/assets/cds-maven-plugin-site/generate-mojo.html#cqnServiceGetters)
483+
484+
The method `getService()` in generated [event-specific Event Context interfaces](../event-handlers/#eventcontext) is overridden to return the typed service interface instead of the generic `Service` type.
485+
486+
:::warning Check migration guides!
487+
In major releases of CAP Java, some of these switches can be made the new default and some other switches might be removed. This might introduce compile errors
488+
in your application that needs to be fixed.
489+
:::
490+
491+
See [Maven Plugin Documentation](/java/assets/cds-maven-plugin-site/generate-mojo.html) for actual status of deprecation and switches that are not described here. {.learn-more}
492+
493+
### Annotation Detail Level
494+
495+
The option [`annotationDetailLevel`](/java/assets/cds-maven-plugin-site/generate-mojo.html#annotationDetailLevel) lets you choose the amount of the details for the Java annotation [`@Generated`](https://docs.oracle.com/en/java/javase/21/docs/api/java.compiler/javax/annotation/processing/Generated.html) added to each interface. This annotation has no effect at runtime but is evaluated by static code analysis tools to identify the artifacts as generated.
496+
497+
Following levels of the details are available:
498+
- `MINIMAL` (default) - only the annotation is added, no additional information is added.
499+
500+
```java
501+
@CdsName("service.Entity")
502+
@Generated("cds-maven-plugin")
503+
public interface Entity extends CdsData { }
504+
```
505+
506+
- `FULL` - annotation contains the timestamp of the generation.
507+
508+
```java
509+
@CdsName("service.Entity")
510+
@Generated(
511+
value = "cds-maven-plugin",
512+
date = "9999-12-31T23:59:59.999999Z",
513+
comments = ""
514+
)
515+
public interface Entity extends CdsData { }
516+
```
517+
518+
- `NONE` - no `@Generated` annotation is added. This is not recommended.
519+
520+
## Using a Local cds-dk
380521

381522
Starting with version 3.6.0 of the `cds-services-archetype`, the default setup of a newly created CAP Java project has changed. The `@sap/cds-dk` is maintained as a `devDependency` in `package.json` and installed with an `npm ci` during the Maven build.
382523
The `install-cdsdk` goal is no longer used to install the `@sap/cds-dk` locally and it's also marked as deprecated. The version of the `@sap/cds-dk` is no longer maintained in _pom.xml_, it's configured in the _package.json_:

0 commit comments

Comments
 (0)