This short article discusses how to compile projects data using javax.validation packages for Spring Boot 2.3.0 (and newer) applications.
Continue readingCategory Archives: Data Access
Solving the error Failed to auto-configure a DataSource: ‘spring.datasource.url’ is not specified
This tutorial will help you to solve the error Failed to auto-configure a DataSource: ‘spring.datasource.url’ is not specified in your Spring Boot applications.
Mapping DTOs in Spring Boot with MapStruct
This tutorial covers how to use MapStruct library to map automatically your Data Transfer Objects with your repository data. We will use the JPA layer of a Spring Boot application to access your data.
Continue readingHikari Connection Pool with Spring Boot made simple
HikariCP is a fast, simple, production ready JDBC connection pool. In this article we will learn how to configure it in Spring Boot applications. Then, we will cover how to monitor Hikari Connection Pool properties using Spring Boot actuator.
Continue readingSpring Boot JPA application with PostgreSQL
In this tutorial we will learn how to create a basic CRUD Spring Boot application that uses PostgreSQL as database. We will be using Spring Boot CLI, then we will import the application into an IDE to create the full CRUD application.
How to use JPA Native Query in Spring Boot applications
Hibernate and JPA can both execute native SQL statements against a Database. In this tutorial we will learn how to map SQL native queries in Spring Boot applications.
Continue readingHow to debug SQL statements in Spring Boot applications
Spring boot auto-configuration does not require a dedicated logging configuration file. So, in order to debug your SQL Statements in Spring Boot applications, by default can use the following settings in your application.properties file:
logging.level.org.hibernate.SQL=DEBUG logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
When this configuration is added, you will be able to see:
1) Each SQL Statement executed
2) The timing information so that you can find slow SQL queries
3) The single parameters used to bind your Prepared Statements
On the other hand, if you are using a YAML file to configure Spring Boot, the following configuration will do:
spring: jpa: properties: hibernate: show_sql: true format_sql: true logging: level: org: hibernate: type: trace
The default logging is good enough for most scenarios. But sometimes in enterprise applications, we need more fine control over logging with other complex requirements. In that case, having a dedicated logging configuration is suitable.
Spring boot by default uses logback, so to customize it’s behavior, all we need to add only logback.xml in classpath and define customization over the file.
Within the XML configuration file, you can add the following appender to debug SQL Statements:
<logger name="org.hibernate.SQL" level="trace" additivity="false"> <appender-ref ref="file" /> </logger>
To get bind variables as well:
<logger name="org.hibernate.type.descriptor.sql" level="trace"> <appender-ref ref="file" /> </logger>
In conclusion, we have covered several ways to debug / log SQL statements in Spring Boot applications.
SpringBoot with JPA example
In this tutorial we will learn how to create a basic application using Spring Boot and JPA. We will create the application with Spring Boot CLI, then import it into an IDE to include JPA classes.
GraphQL tutorial for Spring Boot developers
GraphQL is a specialized query language which gives us an alternative a more flexible approach to managing data in our application than restful design. In this tutorial we will learn how to use GraphQL with a Spring Boot application.
How to add initial data in Spring Boot JPA applications
In order to have some data inserted at application start up, all you have to do is creating a data.sql file in your src/main/resources folder and it will be automatically executed on startup. Within this file, place your SQL INSERT Statements:
INSERT into CUSTOMER(id,name,surname) VALUES (1,'aaaa','bbbbbb') INSERT into CUSTOMER(id,name,surname) VALUES (2,'cccc','dddddd')
On the other hand, if you want to execute DDL, such as CREATE TABLE statements, you have to place them into a src/main/resources/schema.sql file
CREATE TABLE task ( id INTEGER PRIMARY KEY, description VARCHAR(64) NOT NULL, completed BIT NOT NULL);
Consider, however, that Tables are by default created by Spring Boot if you include Entity objects for an in memory database like H2. If you still want to use schema.sql you’ll have to disable this feature by adding this to your application.properties:
spring.jpa.hibernate.ddl-auto=none
Finally, it is worth mentioning that if you’re using Spring boot 2, database initialization only works for embedded databases (H2, HSQLDB, …). If you want to use it for other databases as well, you need to change the spring.datasource.initialization-mode
property:
spring.datasource.initialization-mode=always
That’s all! We have demonstrated in this tutorial how to add initial data in Spring Boot JPA applications, by adding SQL scripts and DDL statements in your resources folder.