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
Related
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
I'm trying use to postgresql but i have this error.Here's my application properties
server.port=8081
application.name= = Address Registration
application.version = 1.0.0
spring.datasource.url= jdbc:postgresql://localhost:5432/addressRegistration
spring.datasource.username= postgres
spring.datasource.password= 12345
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto= update
This can happen during the context loading when spring registers the beans required to finalize the program execution. There can be few options to check.
The dependency for h2 database is missing in your pom.xml. Hence the jar was not downloaded in the classpath.
Autoconfiguration is messed up. You can try with #EnableAutoConfiguration( exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
You might have some settings left with h2 database in your app.properties, however you don't want to use h2 database.
I'm trying to doing a test development with Spring on SQL Server 2019, but all I get is the Datasource url attribute issue.
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
As for my yml configration I don't think I set something wrong
spring:
datasource:
url: jdbc:sqlserver://localhost:1433;databaseName=yap_test
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
username: 'yap_user'
password: 'P#ssw0rd'
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
show_sql: false
format_sql: false
dialect: org.hibernate.dialect.SQLServer2012Dialect
use_nationalized_character_data: true
org:
hibernate:
envers:
audit_table_prefix: AUD_
audit_table_suffix: ''
revision_field_name: REVISION_ID
revision_type_field_name: REVISION_TYPE
store_data_at_delete: true
for the JDBC Driver i'm using mssql-jdbc-8.2.2.jre8
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.2.2.jre8</version>
</dependency>
I hope someone can help me figure out what's my configuration problem
Everything looks fine. there is no problem with property file.
1st of all try to check whether "mssql" jar is there or not in your local maven repository.
if it's there then update your property file as below:--
spring.datasource.driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
In your Case it should be:--(yml file)
spring:
datasource:
driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
if it's not there:--
solution :- give the mssql JAR path in the pom then run
I actually added exclude any YML file during jar repackage, so that's the reason why everytime I tried to run it locally it doesn't find the url attribute.
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.
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.