From 46d82f795c50bd792db9dedff30f57aca6e0e613 Mon Sep 17 00:00:00 2001 From: Robin de Silva Jayasinghe Date: Tue, 19 Nov 2024 14:03:54 +0100 Subject: [PATCH] Native functions w/ diff DBs for CAP Java In order to use database dependent CDS models with CAP Java applications you need to consider some additional steps. Those are added to the database guide in this commit. --- guides/databases.md | 48 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/guides/databases.md b/guides/databases.md index 930782768..5b4dc2130 100644 --- a/guides/databases.md +++ b/guides/databases.md @@ -968,6 +968,54 @@ In case of conflicts, follow these steps to provide different models for differe } }}} ``` +
+ +3. For CAP Java setups you might need to reflect the different profiles in your CDS Maven plugin configuration. This might not be needed for all setups. For mixed local DB (sqlite, H2, or PostgreSQL) and hybrid HANA setup nothing special needs to be done since the local build defaults to the `development` profile. But for other setups like e.g. local PostgreSQL and SQLite you'll need two (profiled) `cds deploy` commands: + + ```xml + + cds.build + + cds + + + + build --for java + deploy --profile development --dry --out "${project.basedir}/src/main/resources/schema-h2.sql" + deploy --profile production --dry --out "${project.basedir}/src/main/resources/schema-postresql.sql" + + + + ``` + +4. For the Spring Boot side it's similar. If you have a local development DB and a hybrid profile with a remote HANA DB you only need to run in default (or any other) profile and you get the local DB and it's SQL schema that was written to your `src/main/resources`. For the HANA part the build and deploy part is done seperately and the application just needs to be started with a local `default-env.json` file and `cloud` profile or via `cds bind`. +Once you have 2 non-HANA local databases you need to have 2 distinct database configurations in your Spring Boot configuration (in most cases application.yaml). + + ```yaml + spring: + config: + activate: + on-profile: default,h2 + sql: + init: + schema-locations: classpath:schema-h2.sql + --- + spring: + config: + activate: + on-profile: postgresql + sql: + init: + schema-locations: classpath:schema-postgresql.sql + datasource: + url: "jdbc:postgresql://localhost:5432/my_schema" + driver-class-name: org.postgresql.Driver + hikari: + maximum-pool-size: 1 + max-lifetime: 0 + ``` + In case you use 2 different databases you also need to make sure that you have the JDBC drivers configured (on the classpath). +
CAP samples demonstrate this in [cap/samples/fiori](https://github.com/SAP-samples/cloud-cap-samples/commit/65c8c82f745e0097fab6ca8164a2ede8400da803).
There's also a [code tour](https://github.com/SAP-samples/cloud-cap-samples#code-tours) available for that.