Skip to Content

Unable to connect to HDI container using Beans in Java Springboot application on cloud foundry

Hi All,

I am trying to create a simple spring boot application to expose a Hana database container table using bean. I am developing the application using SAP WebIDE Full stack on Sap Cloud platform.

I am able to successfully build the java module as well as the entire MTA project but the deployment of the MTAR to cloud foundry is failing. I have attached the log files for more info.

springboot-hana-2018-06-13-06-44-468460000.txt

Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>


  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
  </parent>


  <groupId>com.company.hana_java</groupId>
  <artifactId>springboot_hana</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>


  <name>com.company.hana_java:springboot_hana</name>


  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.source.level>1.8</java.source.level>
    <java.target.level>${java.source.level}</java.target.level>
  </properties>


  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>com.sap.xs.java</groupId>
      <artifactId>xs-env</artifactId>
      <version>1.6.0</version>
    </dependency>
    <dependency>
      <groupId>com.sap.db.jdbc</groupId>
      <artifactId>ngdbc</artifactId>
      <version>2.0.5</version>
    </dependency>
    <dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-data-jpa</artifactId>
	</dependency>
	<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-spring-service-connector</artifactId>
  </dependency>
  <dependency> 
        <groupId>com.sap.hana.cloud</groupId> 
        <artifactId>spring-cloud-sap-connector</artifactId> 
    <version>1.0.1.RELEASE</version> 
  </dependency> 
  <dependency> 
        <groupId>com.sap.hana.cloud</groupId> 
        <artifactId>spring-cloud-cloudfoundry-hana-service-connector</artifactId> 
    <version>1.0.4.RELEASE</version> 
  </dependency>


    <!-- test dependencies -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-junit</artifactId>
      <version>2.0.0.0</version>
      <scope>test</scope>
    </dependency>
  </dependencies>


  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>${java.source.level}</source>
          <target>${java.target.level}</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <includes>
            <include>**/Test*Unit*</include>
          </includes>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.9</version>
        <executions>
          <execution>
            <id>default-prepare-agent</id>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Application.java

package com.company.hana_java.springboot_hana;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;




@SpringBootApplication
@ComponentScan({"com.company.hana_java.springboot_hana.HelloController"})
@EntityScan("com.company.hana_java.springboot_hana.Product")
@EnableJpaRepositories
public class Application {


    public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
  }


}


HelloController.java

package com.company.hana_java.springboot_hana;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

import com.company.hana_java.springboot_hana.Product;
import com.company.hana_java.springboot_hana.ProductRepository;

@Controller
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class HelloController {
	
  @Autowired
  private ProductRepository productrepo;
  
  @RequestMapping(value = "/", method = RequestMethod.GET, produces = "text/plain")
  public @ResponseBody Iterable<Product> getProduct(){
  return productrepo.findAll();
  }
}

Product.java

package com.company.hana_java.springboot_hana;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Id;


@Entity
@Table(name="\"hana_java.hana_database_java.data::master.product\"")
public class Product {
	
	@Id
	@Column(name="product_id")
	private String ProductId;
	
	@Column(name="product_name")
	private String ProductName;
	
	public String getId() {
		return ProductId;
	}
	public void setId(String ProductId) {
		this.ProductId = ProductId;
	}
	public String getName() {
		return ProductName;
	}
	public void setName(String ProductName) {
		this.ProductName = ProductName;
	}	
	
	public String toString()
	{
		return ProductId + "|" + ProductName;
	}
}

ProductRepository.java

package com.company.hana_java.springboot_hana;


import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.company.hana_java.springboot_hana.Product;


@Repository
public interface ProductRepository extends CrudRepository<Product,Long> {


}

Apart for these files i have created a application.properties file and provided all the details of the HDI container.

spring.datasource.host=***
spring.datasource.port=***
spring.datasource.driverClassName=com.sap.db.jdbc.Driver
spring.datasource.url=***
spring.datasource.username=***
spring.datasource.password=***
spring.jpa.generate-ddl=true

mta.yaml

ID: hana_java
_schema-version: '2.1'
description: 'This is a simple java-hana application '
version: 0.0.1


modules:
 - name: hana_database_java
   type: hdb
   path: hana_database_java
   requires:
    - name: hdi_hana_database_java




 - name: springboot_hana
   type: java
   path: springboot_hana
   parameters:
      memory: 1024M
   provides:
    - name: springboot_hana_api
      properties:
         url: ${default-url}
   requires:
    - name: hdi_hana_database_java
      properties:
         JBP_CONFIG_RESOURCE_CONFIGURATION: '[tomcat/webapps/ROOT/META-INF/context.xml:
            {"service_name_for_DefaultDB" : "~{hdi-container-name}"}]'


resources:
 - name: hdi_hana_database_java
   properties:
      hdi-container-name: ${service-name}
   type: com.sap.xs.hdi-container

Thanks,

Vignesh Jeyabalan

Add a comment
10|10000 characters needed characters exceeded

Related questions

1 Answer

  • Posted on Jan 11, 2019 at 11:14 AM

    hi Vignesh,

    I think one issue in your example is that you are trying to create artifacts in the hdi through the regular Spring Boot / Hibernate generation procedure. This will not work, since only native HDI artifacts will be allowed to create new db artifacts.

    Other than that I'm trying to achieve the same, did you have any success in the attempt?

    thanks, regards

    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.