Spring Boot Data JPA with H2 and data.sql - Table not Found - spring-boot

I have a Spring Boot 2.5.0 project. I'm using spring-data-jap with the h2 in-memory database. I want to populate data on startup with a data.sql file but I'm getting a table not found exception. If I remove the data.sql file, I can see that a table for my entity does get created automatically. But if I include the data.sql file, I get the error saying the table doesn't exist. Maybe it is an error with my sql syntax of I have misconfigured the h2 database?
applicaltion.yml
spring:
datasource:
url: jdbc:h2:mem:test
driverClassName: org.h2.Driver
username: sa
password: sa
jpa:
database-platform: org.hibernate.dialect.H2Dialect
debug: true
data.sql
INSERT INTO BUSINESS_SUMMARY VALUES (1, "ALM470", "B48", 3);
BusinessSummary.java entity
#NoArgsConstructor(access = AccessLevel.PROTECTED)
#Getter
#Entity
public class BusinessSummary {
#Id
private Long id;
private String businessId;
private String businessDomainId;
private Integer cityCode;
}
BusinessSummaryRepository.java
#Repository
public interface BusinessSummaryRepository extends JpaRepository<BusinessSummary, Long> {
}
Exception:
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "BUSINESS_SUMMARY" not found; SQL statement:
INSERT INTO BUSINESS_SUMMARY VALUES(1, "ALM470", "B48", 3) [42102-200]

spring.jpa.defer-datasource-initialization=true
By default, data.sql scripts are now run before Hibernate is
initialized. This aligns the behavior of basic script-based
initialization with that of Flyway and Liquibase.
If you want to use
data.sql to populate a schema created by Hibernate, set
spring.jpa.defer-datasource-initialization to true. While mixing
database initialization technologies is not recommended, this will
also allow you to use a schema.sql script to build upon a
Hibernate-created schema before it’s populated via data.sql.
you'll have to convert spring.jpa.defer-datasource-initialization to yml.

If you're using hibernate as a JPA implementation, the best way I think is by using the file import.sql instead of data.sql for Database Initialization.
for more information on database initialization see the official Spring Boot documentation Database Initialization

in addition to defer-datasource-initialization: true, you may also need
spring:
sql:
init:
mode: always

spring.jpa.defer-datasource-initialization = true
spring.sql.init.mode = always
if still doesn`t work try renaming the file from data.sql to import.sql

Related

R2DBC and Spring Data integration issues

I have spring boot project (version 2.4.6) with spring data dependency (spring-boot-starter-data-jpa) and postgreSQL driver.
In project we are using Hibernate and data repositories, which configured via:
#EnableSpringDataCommons(basePackages = ["..."]) // path to folder with repositories
#EnableJpaAuditing(auditorAwareRef = "auditorAware")
#EnableTransactionManagement
#Configuration
class PersistenceConfig
And also I want to add reactive R2DBC. My plan is to use it in one specific place, where we have integration with other system, such communication happened via reactive data streaming. According on it, I need to update some state in database reactivly. That's why I'm added next dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
<version>2.4.6</version>
</dependency>
dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-postgresql</artifactId>
<scope>runtime</scope>
</dependency>
And also, next properties configuration:
spring:
name: configuration
url: jdbc:postgresql://${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DBNAME}
username: ${POSTGRES_USERNAME}
password: ${POSTGRES_PASSWORD}
driverClassName: org.postgresql.Driver
hikari:
maximum-pool-size: 2
jpa:
database: POSTGRESQL
database-platform: org.hibernate.dialect.PostgreSQLDialect
generate-ddl: false
open-in-view: false
properties:
javax:
persistence:
validation:
mode: auto
hibernate:
temp:
use_jdbc_metadata_defaults: false
jdbc:
time_zone: UTC
lob.non_contextual_creation: true
hibernate:
ddl-auto: none
r2dbc:
url: r2dbc:postgresql://${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DBNAME}
username: ${POSTGRES_USERNAME}
password: ${POSTGRES_PASSWORD}
liquibase.change-log: classpath:/db/changelog-master.xml
And finally, I have such data-layer logic service:
#Service
class MyDataReactiveService(
val operator: TransactionalOperator,
val template: R2dbcEntityTemplate
) {
fun updateObjectStatus(state: String, objectId: UUID): Mono<Int> =
template
.update(ObjectEntity::class.java)
.matching(query(Criteria.where("id").`is`(objectId)))
.apply(update("state", state))
.`as` { operator.transactional(it) }
}
Where ObjectEntity - regular spring data entity.
But, unfortunately, I have next error while application startup (inside tests):
Field objectRepository in com.slandshow.TestManager required a bean named 'entityManagerFactory' that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
TestManager - Test wrapper with injected beans:
#Service
class TestManager {
#Autowired
lateinit var objectRepository: ObjectRepository
...
}
And ObjectRepository:
interface ObjectRepository : JpaRepository<ObjectEntity> {
...
}
As far as I understand, such issue related to r2dbc and spring data misconfig.
But how can I fix it?
Since you did not post the code of ObjectRepository it’s hard to say what is wrong. However, I do not recommend to use JPA and R2DBC in same project for the same database..it’s a hassle and furthermore this may not give you any advantage. Instead I would recommend to use WebClient to make HTTP calls and use Kotlin Coroutine to fire query in dedicated thread (since you are using Kotlin already). In my opinion this will be better approach. However all this depends on your application i.e. how many queries you are firing after calls and so forth.

Hibernate JPA Spring Boot

I have created all the models for my spring boot application and successfully specified the relation between them as per business logic. In my properties file, I set the hibernate auto-ddl option as create to generate all the tables as per the relationship between the entities. I am also using liquibase for database migration.
The problem is that, from the logs I can see that hibernate is altering the table before creating it and therefore throwing the run time exception saying "Table not found". Why it is altering the table before creating it? How to solve this issue?
Any help would be highly appreciable. Thanks in advance.
Some logs example
Error executing DDL "alter table application drop foreign key FKrtuepaxepo3o6x0pkn0w62ucg" via JDBC Statement.
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "APPLICATION" not found; SQL statement:
alter table application drop foreign key FKrtuepaxepo3o6x0pkn0w62ucg
.......
.......
.......
Hibernate: drop table if exists application
create table application (id bigint not null auto_increment
alter table application add constraint UK_5jl5nuoh207t0japuutb4avd4 unique (application_name)
Why it is expecting table name as "APPLICATION" before creating "application"
#Table(name = "application")
#EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class Application extends AbstractAuditingEntity implements Serializable {
private static final long serialVersionUID = 1L;
}
I am using H2 database in the development and MYSQL in production. But, previously, I was using the "org.hibernate.dialect.MySQL5InnoDBDialect" dialect in "application-dev.yml". Changing it to h2 dialect(org.hibernate.dialect.H2Dialect) solved the problem.
But,what should be the proper dialect in application-prod.yml? I am using MYSQL in production. Current dialect I am using here is "org.hibernate.dialect.MySQL5InnoDBDialect". This will further create problem in production.
application-prod.yml
datasource:
# type: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/appscoredb
username:
password:
hikari:
poolName: Hikari
auto-commit: false
# h2:
# console:
# enabled: true
# settings:
# web-allow-others: true
jpa:
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
database: mysql
hibernate:
ddl-auto: none
show-sql: true
properties:
hibernate.id.new_generator_mappings: true
hibernate.connection.provider_disables_autocommit: true
hibernate.cache.use_second_level_cache: false
hibernate.cache.use_query_cache: false
hibernate.generate_statistics: false
liquibase:
# Remove 'faker' if you do not want the sample data to be loaded automatically
contexts: prod, faker
enabled: false
It is solved by using this dialect: org.hibernate.dialect.MySQL5Dialect
I hope it could help someone in near future.

Spring Boot with in memory database fails

I'm trying to test my Spring Boot application with an embedded database h2. As for dev and prod, I will be using a MySQL database.
I would like to have different application.yml and schema.sql file for each mode.
The project structure is:
src
--main
----resources
------application.yml
------schema.sql
--test
----resources
------application-test.yml
------schema-test.sql
This is my RespositoryTest :
#RunWith(SpringJUnit4ClassRunner.class)
#SpringBootTest
#DataJpaTest
public class IUserRepositoryTest {
#Autowired
private TestEntityManager entityManager;
#Autowired
private IUserRepository userRepository;
#Test
public void Should_ReturnEmployee_ForExistingEmail() {
User oneUser=new User("john","doe","example#email.com");
entityManager.persist(oneUser);
List<User> userList=userRepository.findUserByEmail("example#email.com");
assertThat(userList).isNotEmpty();
assertThat(userList.get(0)).isNotNull();
assertThat(userList.get(0).getEmail()).isEqualTo("example#email.com");
}
This is my test/resources/application-test.yml:
spring:
profiles: test
datasource:
url: jdbc:h2:mem:test;INIT=create schema IF NOT EXISTS mydb;DB_CLOSE_DELAY=-1
platform: h2
username: sa
password:
driverClassName: org.h2.Driver
jpa:
hibernate:
ddl-auto: create-drop
properties:
hibernate:
default-schema: mydb
dialect: org.hibernate.dialect.H2Dialect
This is my test/resources/schema-test.sql:
CREATE SCHEMA IF NOT EXISTS MYDB
As for my main/resources/application.yml:
logging:
level:
org.springframework.web: DEBUG
org:
hibernate:
SQL: DEBUG
spring:
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: root
password: toor
database:
driverClassName: com.mysql.jdbc.Driver
When I run my app as a spring boot one, the main application.yml is used and all is good, but when I run my tests, I get this error:
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement
Caused by: org.h2.jdbc.JdbcSQLException: Schema "MYDB" not found; SQL statement
Which causes all my tests to fail.
When I try to use this project structure:
src
--main
----resources
------application.yml
------schema.sql
--test
----resources
------application.yml
------schema.sql
The test succed but when I run my app as a spring boot, the test/resources/application.yml is the one being used instead of the main one.
Your tests are running with the "default" profile so it will only load the "default" configurations with no suffix (i.e. -test).
Try adding #ActiveProfiles("test") to your test class to enable the test profile.

Spring Boot Data Initialization

I am trying to insert some test data after my Spring Boot application has started. I have the below configuration in place. I do notice that the sql script is executed before the JPA entities are created resulting in a failure of the insert statement. Is there any other configuration I am missing here? As noted in my configuration I am using Spring Boot with H2 and EclipseLink for JPA.
Reference
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html
Configuration
# H2
spring.h2.console.enabled=true
# Datasource
spring.datasource.url=jdbc:h2:mem:testdb;MODE=ORACLE
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
# JPA
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.properties.eclipselink.weaving=false
data.sql is placed under /src/main/resources
Logs
2017-09-05 20:37:37,989 [restartedMain ] INFO
o.s.j.d.e.EmbeddedDatabaseFactory - Starting embedded database:
url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false',
username='sa'
2017-09-05 20:37:38,141 [restartedMain ] INFO
j.LocalContainerEntityManagerFactoryBean - Building JPA container
EntityManagerFactory for persistence unit 'default'
2017-09-05 20:37:38,446 [restartedMain ] INFO
o.s.o.j.AbstractEntityManagerFactoryBean - Initialized JPA
EntityManagerFactory for persistence unit 'default'
2017-09-05 20:37:38,510 [restartedMain ] INFO o.s.j.d.i.ScriptUtils
- Executing SQL script from URL [file:/develop/src/data-init/target/classes/data.sql]
error...
Entity
#Entity
#Table(name = "TEMPLATE_CONFIG")
#Data
public class TemplateUser {
#Id
#Column(name = "TEMPLATE_CONFIG_ID", nullable = false)
private Long configId;
#Column(name = "APP_CODE", nullable = false, length = 100)
private String appCode;
...
}
Update
Uploaded a sample project:
https://git.io/v5SWx
Spring-Boot loads data.sql or data-${somthing}.sql
You can do:
spring.datasource.platform=h2.
spring.datasource.data=myscript-test.sql. ( locations can be changed with this property)
I have a project on that way and is working with h2.
And just how last alternative, if you are not able to load the information maybe you can use the CommandLineRunner in your application file with jdbc template.
This is an existing bug in Spring as confirmed here by the Spring Team.
https://github.com/spring-projects/spring-boot/issues/10365

H2 DB and Flyway DB Migration using Spring

As am learning i am creating a simple application which uses H2 database to store a table using spring boot. I am using flyway to migrate/create a new table from JAVA program. I have specified the below properties in the 'application.properties' file:
logging.level.org.springframework.web=DEBUG
server.port=8080
spring.h2.console.enabled=true
spring.h2.console.path=/h2
spring.datasource.url=jdbc:h2:file:~/dasboot
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.driver
spring.datasource.max-active=10
spring.datasource.max-idle=8
spring.datasource.max-wait=10000
spring.datasource.min-evictable-idle-time-millis=1000
spring.datasource.min-idle=8
spring.datasource.time-between-eviction-run-millis=1
flyway.baseline-on-migrate=true
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.shows-sql=true
Below is the query for H2 am using (V2__create_shipwreck.sql). This query is under src/main/resources/db/migration directory and am using maven packaging:
CREATE TABLE SHIPWRECK(
ID INT AUTO_INCREMENT,
NAME VARCHAR(255),
DESCRIPTION VARCHAR(2000),
CONDITION VARCHAR(255),
DEPTH INT,
LATITUDE DOUBLE,
LONGITUDE DOUBLE,
YEAR_DISCOVERED INT
);
in the final output console i expect to have a 'SCHEMA_VERSION' and my 'SHIPWRECk' table. But am getting nothing:
it would be great if someone can help me to resolve the same.

Resources