org.h2.jdbc.JdbcSQLException: Schema "myschema" already exists - h2

The issue is when I execute my integrations tests I get the exception :
Caused by: org.h2.jdbc.JdbcSQLException: Schema "myschema" already exists; SQL statement:
CREATE SCHEMA myschema [90078-196]
I have this application-test.yml :
debug: true
spring:
data:
jpa:
repositories:
enabled: true
jpa:
database: h2
generate-ddl: true
show-sql: true
hibernate:
ddl-auto: create-drop
properties:
hibernate.hbm2ddl.auto: create-drop
datasource:
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=TRUE;DATABASE_TO_UPPER=false;INIT=CREATE SCHEMA IF NOT EXISTS myschema
username: sa
password:
driver-class-name: org.h2.Driver
What am I missing here ?

hbm2ddl is set as create-drop changing it to update would solve your issue.

Related

How to set Spring profiles for deploying to Heroku

I have a Spring web app, with 3 different profiles for prod dev and test Each of these uses a different database, local for test and dev and a heroku postgres for prod.
I have set up my application.yaml file as below,
spring:
profiles:
active:
- dev
datasource:
url: jdbc:postgresql://ec2-35-168-65-132.compute-1.amazonaws.com:5432/*******
username: username
password: ******************
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
format_sql: true
show-sql: true
javax:
persistence:
validation:
mode: none
show-sql: true
data:
web:
pageable:
default-page-size: 10
max-page-size: 100
---
spring:
config:
activate:
on-profile: prod
datasource:
url: jdbc:postgresql://ec2-35-168-65-132.compute-1.amazonaws.com:5432/********
username: username
password: **********
jpa:
hibernate:
ddl-auto: update
syftgolf:
upload-path: uploads-prod
---
spring:
config:
activate:
on-profile: dev
datasource:
url: jdbc:postgresql://localhost:5432/testdb
username: postgres
password: admin
jpa:
hibernate:
ddl-auto: create-drop
syftgolf:
upload-path: uploads-dev
---
spring:
config:
activate:
on-profile: test
datasource:
url: jdbc:postgresql://localhost:5432/syftgolf
username: postgres
password: admin
jpa:
hibernate:
ddl-auto: create-drop
syftgolf:
upload-path: uploads-test
I have slowly been updating this app over the months from a barebones project I did. I cant for the life of me, remember what this first part does.
spring:
profiles:
active:
- dev
datasource:
url: jdbc:postgresql://ec2-35-168-65-132.compute-1.amazonaws.com:5432/*********
username: username
password: *********
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
format_sql: true
show-sql: true
javax:
persistence:
validation:
mode: none
show-sql: true
data:
web:
pageable:
default-page-size: 10
max-page-size: 100
I think spring:profiles:active: -dev sets the active profile to dev, so it should then use the database under dev? But I cant remember why the rest of the lines are placed here and not under a specific profile,
datasource:
url: jdbc:postgresql://ec2-35-168-65-132.compute-1.amazonaws.com:5432/***********
username: username
password: *************************
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
format_sql: true
show-sql: true
javax:
persistence:
validation:
mode: none
show-sql: true
data:
web:
pageable:
default-page-size: 10
max-page-size: 100
if I remove this and just leave the active profile part, I get an error that the app cant start.
Please could someone help with the best way of laying out this application.yaml file and also is setting active profile in this file the correct way of doing it?
So if I am working on the app, i set this to dev do I then need to change it to prod before I push it to heroku using the heroku cli?
The best way would be to save an environment variable in your heroku application which will be using by your application to set the profile.
For example, this is a configuration for a simple spring boot thymeleaf application.
server:
port: ${PORT:4000}
spring:
application:
name: PROFILE-TEST
---
spring:
config:
activate:
on-profile: dev
custom:
message: "This is development environment"
---
spring:
config:
activate:
on-profile: prod
custom:
message: "This is production environment"
---
spring:
profiles:
active: ${PROFILE:dev}
Here I'm using spring.config.activate.on-profile to set an environment to have different custom.message.
By Default I'm setting the active profile with spring.profiles.active=${PROFILE:dev}. PROFILE is the environment variable we're gonna use to set different value during deployment. If there is no environment variable set for PROFILE, it'll default back to dev.
Now if I've written a controller which will only act on profile dev with the #Profile annotation like this
#Profile("dev")
#Controller
#RequestMapping("/")
#RequiredArgsConstructor
public class DevHomeController {
private final CustomConfigurationProperties customConfigurationProperties;
#GetMapping
public ModelAndView home(ModelAndView modelAndView) {
modelAndView.addObject("message", customConfigurationProperties.getMessage());
modelAndView.setViewName("dev-home.html");
return modelAndView;
}
}
It'll show something like
I've written another controller which will have a profile value #Profile("prod")
In the console type the below command to set an environment variable in heroku.
heroku config:set PROFILE=prod
And deploy the application. I'll look like
The message you see on the screen is coming from custom.message property you have set in your application.yml, and as we have set the environment variable for PROFILE, heroku is using that to set the running profile.

There is not connect another profile in Spring boot

I have a project with two properties.
application.yml (major)
spring:
config:
activate:
on-profile: local
application:
name: test-app
datasource:
cluster:
enabled: true
driverClassName: org.postgresql.Driver
hikari:
maximum-pool-size: 30
minimum-idle: 3
autoCommit: false
jpa:
database-platform: org.hibernate.dialect.PostgreSQL95Dialect
properties:
hibernate:
connection.provider_disables_autocommit: true
jdbc.lob.non_contextual_creation: true
open-in-view: false
application-local.yml
spring:
datasource:
read:
url: jdbc:postgresql://localhost:5432/test-app
write:
url:jdbc:postgresql://localhost:5432/test-app
username:
password:
driver-class-name: org.postgresql.Driver
jpa:
hibernate:
ddl-auto: update
properties:
open-in-view: false
I also have added the setting in IntelliJ IDEA
VM options also has used.
-Dspring.profiles.active=local
But during the launch I get an error:
Description:
Failed to configure a DataSource: 'url' attribute is not specified and
no embedded datasource could be configured.
Reason: Failed to determine suitable jdbc url
I assume that the specified profile did not start
Who has any ideas how to fix this ?

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.

Set hibernate dialect for jpa in YML

I tried to set hibernate dialect for jpa in YML,
checked many topics, but it does not set:
spring:
datasource:
hikari:
allow-pool-suspension: true
connection-timeout: 1000
name: testDb
jpa:
database: h2
generate-ddl: false
database-platform: h2
package-to-scan: com.x.model
properties:
hibernate:
dialect: com.x.data.core.hibernate.dialect.ExtendedH2Dialect
h2:
console:
enabled: true
path: /h2
How to fix this?
what is com.x.data.core.hibernate.dialect.ExtendedH2Dialect ? You have to use dialect as org.hibernate.dialect.H2Dialect
below is the sample
server:
port: 8096
spring:
datasource:
driverClassName: org.h2.Driver
url: jdbc:h2:~/test
username: sa
password:
h2:
console:
enabled: true
jpa:
hibernate.ddl-auto: update
generate-ddl: false
show-sql: false
properties:
hibernate:
dialect: org.hibernate.dialect.H2Dialect
Note:
I'm using spring-boot-starter-data-jpa 2.0.5

jhipster Oracle database connectivity

I can't seem to get the Liquibase running against oracle.
My application-dev.yml has:
spring:
profiles: dev
datasource:
dataSourceClassName: oracle.jdbc.pool.OracleDataSource
url: jdbc:oracle:thin:#localhost:1521:orcl
username: rest_test
password: rest_test
jpa:
database-platform: org.hibernate.dialect.OracleDialect
database: ORACLE
openInView: false
show_sql: true
generate-ddl: false
hibernate:
ddl-auto: none
But I recieved:
[DEBUG] com.steve.config.DatabaseConfiguration - Configuring Datasource
[ERROR] com.zaxxer.hikari.util.PropertyBeanSetter - Property url is does not exist on target class class oracle.jdbc.pool.OracleDataSource
Which is odd as the class seems to have a url property:
[btuser#localhost pool]$ strings OracleDataSource.class | grep -i seturl
setURL
Any thoughts?
Thanks
fixed this now, this config now works:
spring:
profiles: dev
datasource:
driverClassName: oracle.jdbc.OracleDriver
dataSourceClassName: oracle.jdbc.pool.OracleDataSource
url: jdbc:oracle:thin:#localhost:1521:orcl
username: rest_test
password: rest_test
jpa:
database-platform: org.hibernate.dialect.Oracle10gDialect
database: ORACLE
openInView: false
show_sql: true
generate-ddl: false
hibernate:
ddl-auto: none
naming-strategy: org.hibernate.cfg.EJB3NamingStrategy
properties:
hibernate.cache.use_second_level_cache: true
hibernate.cache.use_query_cache: false
hibernate.generate_statistics: false
hibernate.cache.region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory

Resources