RDBMS deployment prerequisites

For service projects with RDBMS support before running your pipeline(s) and deploying the service project you need to do some of the below tasks

Warning:

Make sure your database schema and tables are already reviewed and created by your DB admin!

Tip:

To generate your DB ddl files see Generating ddl

Database connection

You need to provide a connection to the database that will be used by the service project. This can be done either by adding connection properties in you service project's application.yaml

spring.datasource:
  url: "jdbc:db2://<host>:<port>/<database-name>"
  username: <user name>
  password: <password>
  driver-class-name: com.ibm.db2.jcc.DB2Driver

or by adding a configuration class that creates the necessary datasource bean programmatically:

 @Configuration
 public class DataSourceConfig {
    
    @Bean
    public DataSource getDataSource() {
        DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName("com.ibm.db2.jcc.DB2Driver");
        dataSourceBuilder.url("<database-url>");
        dataSourceBuilder.username("<database-username>");
        dataSourceBuilder.password("<database-password");
        return dataSourceBuilder.build();
    }
}

JPA configuration

Overwriting naming strategy (Java Spring Boot Stack 2.0)

If the naming strategy isn't configured in your k5-project.yml, it will be treated as legacy. Hence the spring defaults are overwritten as following in the application.yml:

spring.jpa:
  hibernate:
    naming:
      implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl
      physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

The naming of tables and columns is also set by default e.g. a column for the property 'age' will a @Column(name="age") annotation, if nothing else is modelled in the designer.

To disable this legacy support and to switch to the spring defaults, set namingStrategies to default in your k5-project.yml.

  aggregatePersistenceSupport:
    databaseType: DB2
    enabled: true
    namingStrategies: default 

Now you can overwrite the defaults by changing the implicit-strategy and the physical-strategy field in your application.yml. For further details please feel free to read the section Configure Hibernate Naming Strategy of 'Spring Boot Reference Documentation'.

Note:

Projects created after 4.1.1 automatically have the default naming strategy applied.

Naming collision in entities with default naming strategy

Naming collisions can occur if you are embedding two entities that have the same property name. You can solve this by adding arbitrary annotations JPA. An example for this is, if the same entity is embedded several times within an entity (e.g. having a delivery address and a billing address). To handle this, please add @jakarta.persistence.AttributeOverride as custom annotation like the following example.

@jakarta.persistence.AttributeOverride(name="location", column=@Column("delivery_location"))

@jakarta.persistence.AttributeOverride(name="location", column=@Column("billing_location"))

Generating database DDL files

Java Spring Boot Stack 2.0

  • You can generate database DDL scripts for your entities by using the SchemaGenerator class found in the base package, the same where your application class is located
  • To generate .ddl scripts, run the "main" method of the class from within your IDE
  • Default configuration parameters can be overwritten by your project's application.yaml or application-local.yaml
  • You may also overwrite the class at your own volition

Java Spring Boot Stack 1.0 (Deprecated)

  • You can generate database DDL scripts for your entities by configuring and using the Maven plugin jpa2ddl included in your service project's pom.xml file
  • You need to provide "SCHEMA_NAME" to be used in the .ddl scripts
  • You can generate .ddl scripts by running the "generate" task of the plugin from within your IDE
  • You need to provide the .ddl scripts to your database administrator and ask for them to be reviewed and your database schema and tables to be created
<plugin>
    <groupId>com.devskiller.jpa2ddl</groupId>
    <artifactId>jpa2ddl-maven-plugin</artifactId>
    <version>0.9.12</version>
    <extensions>false</extensions>
    <!-- required to determine whether to run automatically or not  -->
    <configuration>
        <outputPath>${basedir}/src/main/resources/database.sql</outputPath>
        <packages>
            <package>de.knowis.kb.demo.kbdemo.sdk.domain</package>
        </packages>
        <jpaProperties>
            <property>
                <name>hibernate.dialect</name>
                <value>org.hibernate.dialect.DB2Dialect</value>
            </property>
            <property>
                <name>hibernate.implicit_naming_strategy</name>
                <value>org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl</value>
            </property>
            <property>
                <name>hibernate.physical_naming_strategy</name>
                <value>org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl</value>
            </property>
            <property>
                <name>hibernate.default_schema</name>
                <value>SCHEMA_NAME</value>
            </property>
        </jpaProperties>
        <formatOutput>true</formatOutput>
        <delimiter>;</delimiter>
        <action>CREATE</action>
    </configuration>
</plugin>