Skip to content

Latest commit

 

History

History
124 lines (84 loc) · 6.78 KB

configure-spring-data-jpa-with-azure-sql-server.md

File metadata and controls

124 lines (84 loc) · 6.78 KB
titledescriptionms.datems.authorms.reviewerauthorms.topicms.custom
Use Spring Data JPA with Azure SQL Database
Learn how to use Spring Data JPA with an Azure SQL Database.
08/28/2024
karler
seal
KarlErickson
article
devx-track-java, devx-track-azurecli, team=cloud_advocates, spring-cloud-azure, passwordless-java, devx-track-extended-java

Use Spring Data JPA with Azure SQL Database

This tutorial demonstrates how to store data in Azure SQL Database using Spring Data JPA.

The Java Persistence API (JPA) is the standard Java API for object-relational mapping.

In this tutorial, we include two authentication methods: Microsoft Entra authentication and SQL Database authentication. The Passwordless tab shows the Microsoft Entra authentication and the Password tab shows the SQL Database authentication.

Microsoft Entra authentication is a mechanism for connecting to Azure Database for SQL Database using identities defined in Microsoft Entra ID. With Microsoft Entra authentication, you can manage database user identities and other Microsoft services in a central location, which simplifies permission management.

SQL Database authentication uses accounts stored in SQL Database. If you choose to use passwords as credentials for the accounts, these credentials will be stored in the user table. Because these passwords are stored in SQL Database, you need to manage the rotation of the passwords by yourself.

[!INCLUDE spring-data-prerequisites.md]

Important

To use passwordless connections, upgrade MS SQL Server Driver to version 12.1.0 or higher, and then create a Microsoft Entra admin user for your Azure SQL Database server instance. For more information, see the Create a Microsoft Entra admin section of Tutorial: Secure a database in Azure SQL Database.

See the sample application

In this tutorial, you'll code a sample application. If you want to go faster, this application is already coded and available at https://github.com/Azure-Samples/quickstart-spring-data-jpa-sql-server.

[!INCLUDE spring-data-sql-server-setup.md]

Store data from Azure SQL Database

With an Azure SQL Database instance, you can store data by using Spring Cloud Azure.

To install the Spring Cloud Azure Starter module, add the following dependencies to your pom.xml file:

  • The Spring Cloud Azure Bill of Materials (BOM):

    <dependencyManagement> <dependencies> <dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-dependencies</artifactId> <version>5.22.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

    [!NOTE] If you're using Spring Boot 2.x, be sure to set the spring-cloud-azure-dependencies version to 4.20.0. This Bill of Material (BOM) should be configured in the <dependencyManagement> section of your pom.xml file. This ensures that all Spring Cloud Azure dependencies are using the same version. For more information about the version used for this BOM, see Which Version of Spring Cloud Azure Should I Use.

  • The Spring Cloud Azure Starter artifact:

    <dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-starter</artifactId> </dependency>

    [!NOTE] As this is a dependency, it should be added in the <dependencies> section of the pom.xml. Its version is not configured here, as it is managed by the BOM that we added previously.

Configure Spring Boot to use Azure SQL Database

To store data from Azure SQL Database using Spring Data JPA, follow these steps to configure the application:

  1. Configure an Azure SQL Database credentials in the application.properties configuration file.

    logging.level.org.hibernate.SQL=DEBUG spring.datasource.url=jdbc:sqlserver://sqlservertest.database.windows.net:1433;databaseName=demo;authentication=DefaultAzureCredential;spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2016Dialect spring.jpa.hibernate.ddl-auto=create-drop
    logging.level.org.hibernate.SQL=DEBUG spring.datasource.url=jdbc:sqlserver://sqlservertest.database.windows.net:1433;database=demo;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;spring.datasource.username=<your_sql_server_non_admin_username> spring.datasource.password=<your_sql_server_non_admin_password> spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2016Dialect spring.jpa.hibernate.ddl-auto=create-drop

    [!WARNING] The configuration property spring.jpa.hibernate.ddl-auto=create-drop means that Spring Boot will automatically create a database schema at application start-up, and will try to delete it when it shuts down. This feature is great for testing, but remember that it will delete your data at each restart, so you shouldn't use it in production.

[!INCLUDE spring-data-jpa-create-application.md]

[!INCLUDE deploy-to-azure-spring-apps]

Next steps

[!div class="nextstepaction"] Azure for Spring developers

close