spring boot multi-line import.sql application.yml configuration - java

I have a spring boot application configured with an application.yml file. I'm also using an import.sql file to load test data into my application using insert statements.
I want the import.sql to support multiline statements.
I found this similar question with an answer using and application.properties file Spring Mvc Hibernate Encoding/Multi-line import sql
However I can't seem to apply that answer to using my application.yml file which I have tried unsuccessfully to do as below
spring:
jpa:
show-sql: true
hibernate:
hbm2ddl:
import_files_sql_extractor:org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor
For reference I found the documentation for this property here https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/tool/hbm2ddl/ImportSqlCommandExtractor.html
But I still can't seem to configure it properly. Can anyone help? Thanks

I got multi-line import.sql statements working for Spring Boot 2 by adding property prefix "spring.jpa.properties".
So for application.yml configuration it is:
spring:
jpa:
properties:
hibernate:
hbm2ddl:
import_files_sql_extractor: org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor

in application.yml you can add config:
spring:
datasource: classpath:/init.sql
or add #SQL(value = "data.sql") annotation in your test class where data.sql has multiple lines sql commands.

Related

Spring Boot profile properties doesn't work with tests

Spring Boot version 2.2.6. I have case that there is several application-{profile}.yml files in folder src/main/resources/ and I want't to build project with Maven e.g mvn clean package -Dspring.profiles.active=dev
Then I have just application.yml file in folder src/test/resources, this should be a properties file to all test (IT/unit).
Now when I build with command mvn clean package -Dspring.profiles.active=dev, properties src/main/resources/application-dev.yml and src/test/resources/application.yml are MERGED and used for the tests. Well in test properties there might be pretty fatal confs e.g Hibernate auto-ddl: create-drop.
Have been reading docs but I don't find any logic why properties files are MERGED in this case. Is there any good practice that tests can be forced to use ALWAYS certain properties file? I think that just using some annotations in test files isn't the best practice, e.g #TestPropertySource or #ActiveProfiles, cause when you forgot to use these annotations then the case is same again. Is there some global configuration for all tests or some other better solutions?
The application.yml file has higher precedence over any environment-specific file, for example, application-dev.yml file. The standard inheritance applies, so you can override the parent properties in the profile-specific YAML file.
application.yml
server:
port: 9090
spring:
jpa:
hibernate:
ddl-auto: create-drop
application-dev.yml
spring:
jpa:
hibernate:
ddl-auto: none
In case you run with the dev profile, the value of spring.jpa.hibernate.ddl-auto parameter would be none.
I get that application.yml has higher precedence in folder /src/main, but that it takes over also from folder /src/test is kinda weird.
Yes that config can be made into profile-specific files (ddl-auto: none), tough I still have problem with datasource, somehow Spring Boot is picking application-dev.ymls datasource into tests also, this is working like other way around than that ddl-auto attribute.
src/test/resources/application.yml
spring:
datasource:
url: jdbc:h2:mem:test;DB_CLOSE_ON_EXIT=FALSE;MODE=PostgreSQL
src/main/resources/application-dev.yml
spring:
datasource:
url: jdbc:postgresql://localhost:5432/db

Spring boot: Oracle RAC DB connection configuration

In Spring boot application, Need to configure Oracle RAC DB URL. Can someone explain how to configure the Oracle RAC URL in application.properties?
jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL={PROTOCOL})(HOST={{URL})(PORT={PORT})))(CONNECT_DATA=(SERVICE_NAME={SERVICE_NAME})))
Have verified the Spring boot official doc and didn't find anything related. Even verified in the Common Properties and can't find any references.
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
Thanks for your help in advance!
Try with below.
jdbc:oracle:thin:#(DESCRIPTION=
# (LOAD_BALANCE=on)
# (ADDRESS=(PROTOCOL=TCP)(HOST=host1) (PORT=1521))
# (ADDRESS=(PROTOCOL=TCP)(HOST=host2)(PORT=1521))
# (CONNECT_DATA=(SERVICE_NAME=service_name)))
OR
# Oracle settings
spring.datasource.url=jdbc:oracle:thin:#localhost:1522:orcl
spring.datasource.username=HIBERNATE_TEST
spring.datasource.password=HIBERNATE_TEST
spring.datasource.driver.class=oracle.jdbc.driver.OracleDriver
OR
jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS_LIST=(LOAD_BALANCE=OFF)(FAILOVER=ON)
(ADDRESS=(PROTOCOL=TCP)(HOST=tst-db1.myco.com)(PORT=1604))
(ADDRESS=(PROTOCOL=TCP)(HOST=tst-db2.myco.com)(PORT=1604)))
(CONNECT_DATA=(SERVICE_NAME=mydb1.myco.com)(SERVER=DEDICATED)))
Sources :
https://docs.oracle.com/cd/E57185_01/EPMIS/apbs01s01.html
https://dzone.com/articles/configuring-spring-boot-for-oracle
This is what i did to connect to postgres in my project and it is in production now. For Oracle it is exactly same. In fact for any other RDBMS.
Add the properties in the application.yml or the application.properties in the spring boot project.
The below is yml configuration.
spring:
jpa:
database: POSTGRESQL
show-sql: false
datasource:
platform: postgres
url: jdbc:postgresql://123.3.4.89.com:1234/DatabaseName
username: user123
password: pass123
driver-class-name: org.postgresql.Driver
testWhileIdle: true
validationQuery: SELECT 1
Then add the driver in the pom or the gradle build file which build tool you are using.
And the jpa jar of spring boot.
This was entry in the build.gradle file.
compile ('org.springframework.boot:spring-boot-starter-data-jpa')
compile group: 'org.postgresql', name: 'postgresql', version: '42.2.2'
Thats it, now you can create your repositories and start pushing and fetching data in the Db.
Hope this helps, cheers !!!

How to access in-memory h2 database from Intellij IDEA

In Spring Boot project I am trying to see in-memory tables from my IDE.
How to access in-memory h2 database from Intellij IDEA.
Here is a snippet from my application.yml:
h2:
datasource:
url: jdbc:h2:mem:mydb
username: username
password: 123
driver-class-name: org.h2.Driver
init-sql: h2.sql
console:
enabled: true
path: /search/console
settings:
trace: false
web-allow-others: false
Intellij has no field to input username for in-memory database:
Test Connection shows success, however it doesn't see tables from h2.sql.
I can access them using h2 console.
Just please bear in mind this may show you the db but not the table as they will only be visible in the h2 console. For you to access them through IntelliJ you may need to change the url and connection to be of a file type rather than in memory.
So instead of this:
#spring.datasource.url=jdbc:h2:mem:testdb
You'd have something like this:
spring.datasource.url=jdbc:h2:file:~/Users/yourUser/IdeaProjects/resume-portal/src/main/resources/data/resume-portal;MV_STORE=false;AUTO_SERVER=TRUE
And then after creating a datasource from url
You should now see both the database and its tables.
And that will still also be available from the console if you connect using the new url.
PS: If'd prefer to use a relative path you can change the url to be something similar to jdbc:h2:file:./src/main/resources/data/resume-portal;MV_STORE=false;AUTO_SERVER=TRUE
Here is the application.yml file for the application.
spring.jpa.defer-datasource-initialization=true
#spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.url=jdbc:h2:file:./src/main/resources/data/resume-portal;MV_STORE=false;AUTO_SERVER=TRUE
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.datasource.username=sa
spring.datasource.password=
spring.sql.init.mode=always
By the default, IntellJ doesn't show any of database.
right click at the datasource then choose Properties
At the Schemas tab, you'll see a list of options to choose which database should be shown (I normally choose All databases). Choose the database which you need it to be shown
The result:
I found another way. I created the H2 Server bean and added it to my SpringBoot Application. It looks like this:
// Start internal H2 server so can query from IDE
#Bean(initMethod = "start", destroyMethod = "stop")
public Server h2Server() throws SQLException {
return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9092");
}
I also had the following on my application.properties
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.defer-datasource-initialization=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.sql.init.mode=always
# Initialise H2 with H2GIS for spatial support ? see schema-h2.sql also
spring.sql.init.platform=h2
spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.h2geodb.GeoDBDialect
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
h2.tcp.enabled=true
In order to get my data.sql put into the tables in Intellij I had to make sure I used create-drop and added the spring.sql.init.mode=always
Note: I was using the H2GIS dialect - but this has not so far caused any issues
The connection setup in Intellij was still remote with this string jdbc:h2:tcp://localhost:9092/mem:testdb - dont forget to change the port to 9092
I the connection window in Intellij I also checked all tables under Schema. Then the tables are present for the H2 in mem database.

Spring boot and JUnit with H2 JPA causes 'pg_class' not found

I currently have a spring boot application which contains 2 configuration files: application.yaml and application-test.yaml. The application-test profile is loaded correctly and any settings in that file are working as expected.
However i am having an issue with one setting in particular which is spring.jpa.hibernate.ddl-auto = 'update'. When this setting is defined in the application.yaml file it causes my JPA unit tests to fail with the exception "Table "PG_CLASS" not found".
I have tried overriding this setting with different values in the application-test configuration file to no avail.
My full configuration files are as follows:
application.yaml
#Configure Postgres backend datasource
spring.datasource.driverClassName: org.postgresql.Driver
spring.datasource.url: jdbc:postgresql://ec2-54-75-233-146.eu-west-1.compute.amazonaws.com:5432/xxxx?user=xxxx&password=xxxxx&sslmode=require
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults: false
spring.jpa.database-platform: org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto: update
application-test.yaml
spring.datasource.usernam: sa
spring.datasource.url: jdbc:h2:mem:tests;DB_CLOSE_DELAY=-1;MODE=PostgreSQL;DB_CLOSE_ON_EXIT=FALSE;
spring.datasource.driverClassName: org.h2.Driver
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_default: false
spring.jpa.database-platform: org.hibernate.dialect.H2
Uncommenting the "spring.jpa.hibernate.ddl-auto: update" setting in my application.yaml file does fix the issue. But i would like to keep this enabled. Any ideas?
The above errror seems to be caused by Hibernate using the wrong dialect (PostgreSQL instead of H2).
Maybe try adding the following in application-test.yaml:
spring.datasource.driverClassName: org.h2.Driver
spring.jpa.properties.hibernate.dialect: org.hibernate.dialect.H2Dialect
These properties worked for me.
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.hbm2ddl.auto=create
spring.datasource.usernam: sa
spring.datasource.url: jdbc:h2:mem:tests;DB_CLOSE_DELAY=-1;MODE=PostgreSQL;DB_CLOSE_ON_EXIT=FALSE;
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.main.allow-bean-definition-overriding=true

How to configure Spring Boot to execute a SQL query once with JdbcTemplate?

I use Spring Boot and JdbcTemplate with Maven. I would like to execute a single SQL query in the database at start.
Here is a example of the application.yml file :
spring
datasource
driverClassName: oracle.jdbc.OracleDriver
url: jdbc:oracle://localhost/test
username: dbuser
password: dbpass
initialize: true
I found on the web that it can be done by creating a data.sql file in the root of the classpath (/resources directory by example) (Visit http://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html for initialization a database using Spring JDBC). So I just create a data.sql file in the /resources directory, near the application.yml file, with content :
alter session set nls_numeric_characters='.,';
But, the data.sql file is not executed when starting the application. The implementation seems easy yet. Did I forget any parameterizations or dependencies ?
I would be pleased to have some help. Thanks.

Categories

Resources