#1
This guide shows how to handle database transactions with SAP HANA Express in a Spring Boot application. Straight to the point.

1. Project Setup

Generate project from Spring Initializr :
  • Dependencies: Spring Web, Spring Data JPA
Add SAP HANA JDBC driver (ngdbc.jar) manually into local Maven repo, then include in pom.xml.

2. Configure application.properties

spring.datasource.url=jdbc:sap://localhost:39015/?databaseName=HXE
spring.datasource.username=SYSTEM
spring.datasource.password=YourPassword
spring.datasource.driver-class-name=com.sap.db.jdbc.Driver

spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.HANARowStoreDialect

3. Create Entities

import jakarta.persistence.*;

@Entity
public class Account {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String owner;
    private double balance;

    // getters and setters
}

4. Create Repository

import org.springframework.data.jpa.repository.JpaRepository;

public interface AccountRepository extends JpaRepository<Account, Long> {
}

5. Service with Transactions

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AccountService {
    private final AccountRepository repo;

    public AccountService(AccountRepository repo) {
        this.repo = repo;
    }

    @Transactional
    public void transfer(Long fromId, Long toId, double amount) {
        Account from = repo.findById(fromId).orElseThrow();
        Account to = repo.findById(toId).orElseThrow();

        from.setBalance(from.getBalance() - amount);
        to.setBalance(to.getBalance() + amount);

        repo.save(from);
        repo.save(to);

        // if something fails here, transaction rolls back
    }
}

6. Controller to Test

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/accounts")
public class AccountController {
    private final AccountService service;

    public AccountController(AccountService service) {
        this.service = service;
    }

    @PostMapping("/transfer")
    public String transfer(@RequestParam Long from,
                           @RequestParam Long to,
                           @RequestParam double amount) {
        service.transfer(from, to, amount);
        return "Transfer successful";
    }
}

8. Run and Test

Start the app:
mvn spring-boot:run
Test with curl or Postman:
POST http://localhost:8080/accounts/transfer?from=1&to=2&amount=200
If an error occurs during transfer, Spring will rollback the transaction automatically

image quote pre code