Spring Boot + Hibernate + Grails ignores ddlAuto in yml file - spring

I don't want Hibernate to recreate or update the database schema.
According to the documentation, you need to have this in your application.yml file:
spring.jpa.hibernate.ddl-auto: none
It doesn't work.
Here's my application.yml file:
spring:
profiles.active: default
spring.jpa.hibernate.ddl-auto: none
---
spring:
profiles: default
spring.datasource:
driverClassName: net.sourceforge.jtds.jdbc.Driver
url: jdbc:jtds:sqlserver://IP/DB
username: user
password: password
spring.jpa:
hibernate:
ddlAuto: validate
showSql: true
hibernate:
ddl-auto: none
---
spring:
profiles: unit-test
spring.datasource:
pooled: true
jmxExport: true
driverClassName: org.h2.Driver
username: sa
password:
dbCreate: create-drop
url: jdbc:h2:mem:devDb;MVCC=FALSE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=TRUE
Documentation link:
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
Note I've also changed ddl-auto with ddlAuto, still doesn't work.
build.gradle:
buildscript {
repositories {
mavenCentral()
jcenter()
maven { url "https://repo.spring.io/libs-release" }
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.2'
classpath "org.springframework.boot:spring-boot-gradle-plugin:1.3.+"
}
}
repositories {
mavenCentral()
mavenLocal()
jcenter()
}
apply plugin: 'spring-boot'
dependencies {
//Spring
compile "org.springframework.boot:spring-boot-configuration-processor"
compile "org.springframework.boot:spring-boot-starter"
compile "org.grails:gorm-hibernate4-spring-boot:5.0.+"
compile 'javax.el:javax.el-api:2.2.5'
//DB
testCompile 'com.h2database:h2:1.4.190'
compile 'net.sourceforge.jtds:jtds:1.3.1'
compile 'commons-dbcp:commons-dbcp:1.4'
}

Related

Hibernate envers, liquibase and postgres. entity_aud is not found

I have a spring boot v2.7.1 project running with liquibase and hibernate.
When i execute the command gradle diffChangeLog liquibase is generating tables with _AUD suffix and then hibernate is blowing an error
ERROR: relation "comment_aud" does not exist.
here are the relative information from my configuration files:
build.gradle
plugins {
id 'org.springframework.boot' version "${springBootVersion}" #2.7.1
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id "jacoco"
id 'org.liquibase.gradle' version '2.1.1'
}
apply plugin: 'org.liquibase.gradle'
depedencies {
implementation "org.liquibase:liquibase-core"
liquibaseRuntime 'org.liquibase:liquibase-gradle-plugin:2.1.1'
liquibaseRuntime 'org.liquibase:liquibase-core:4.8.0'
liquibaseRuntime 'info.picocli:picocli:4.6.3'
liquibaseRuntime 'org.liquibase.ext:liquibase-hibernate5:4.13.0'
liquibaseRuntime "org.postgresql:postgresql"
liquibaseRuntime 'org.springframework.boot:spring-boot-starter-data-jpa'
}
liquibase {
activities {
main {
driver 'org.postgresql.Driver'
url 'jdbc:postgresql://localhost:5432/database'
username 'admin'
password 'admin'
changeLogFile "src/main/resources/liquibase/changelogs/" + new Date().format("yyyy_MM_dd-HH_mm_ss") + "-changelog.xml"
referenceUrl 'hibernate:spring:mypackagedir.entity' +
'?dialect=org.hibernate.dialect.PostgreSQLDialect' +
'&implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy' +
'&?physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy'
defaultSchemaName ""
classpath "build/classes/java/main"
logLevel "debug"
}
}
runList = 'main'
}
application.yml
spring:
datasource:
driver-class-name: org.postgresql.Driver
password: ${database.password}
url: ${database.host}
username: ${database.username}
jpa:
hibernate:
ddl-auto: none
properties:
org:
hibernate:
envers:
global_with_modified_flag: true
store_data_at_delete: true
audit_table_suffix: _aud
database-platform: org.hibernate.dialect.PostgreSQL92Dialect
show-sql: true
liquibase:
enabled: true
part of the generated changelog after i do diffChangeLog
...
<createTable tableName="comment_AUD">
<column name="REV" type="INTEGER">
<constraints nullable="false" primaryKey="true" primaryKeyName="comment_AUDPK"/>
</column>
<column name="REVTYPE" type="SMALLINT"/>
...
as you can see liquibase is generating the audit table with _AUD although i set it to _aud in hibernate properties
i tried the following:
adding and removing all or one of naming_strategies in build.gradle file
&implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy&?physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
adding removing naming strategies in application.yml properties:
jpa:
hibernate:
naming:
implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
changing the suffix to audit_table_suffix:_AUD
but nothing helps! i am running in circles now and can't find a solution for this. can someone tell me how to fix this please?
Thanks in advance

Why does liquibase not start when the spring boot application starts?

I am trying to create a basic database structure when starting a spring boot application. I need that when starting the task :bootRun was run by liquibase.
It starts without errors, a connection to the database is created, but liquibase does not start. It does not give errors, there is no information in the log about it.
Why doesn't it start automatically with spring boot?
build.gradle
plugins {
id 'org.springframework.boot' version '3.0.0-SNAPSHOT'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'spring.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5:3.1.0.M1'
implementation group: 'org.liquibase', name: 'liquibase-core', version: '4.12.0'
runtimeOnly'org.postgresql:postgresql'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
compileOnly 'org.projectlombok:lombok'
}
tasks.named('test') {
useJUnitPlatform()
}
application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/db
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=none
spring.liquibase.change-log=classpath:db/changelog/db.changelog-root.yaml
src/db/changelog/db.changelog-root.yaml
databaseChangeLog:
- logicalFilePath: db/changelog/db.changelog-root.yaml
- changeSet:
id: create-a-structure
author: your_liquibase_username
changes:
- createTable:
tableName: app_user
columns:
- column:
name: id
type: BIGINT
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: username
type: varchar(254)
constraints:
unique: true
nullable: false
- column:
name: password
type: varchar(64)
constraints:
nullable: false
- column:
name: first_name
type: varchar(50)
nullable: false
- column:
name: last_name
type: varchar(50)
nullable: false
I believe the spring property for the changelog file should not have the classpath reference:
spring.liquibase.change-log=db/...
instead of
spring.liquibase.change-log=classpath:db/...
I had a similar problem. This solved it for me https://stackoverflow.com/a/41960712/3999476
I have a multi module maven project, I had to move the dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
to the same module where I added the dependency for liquibase.
Just noticed that you've put the liquibase yaml in:
src/db/changelog/db.changelog-root.yaml.
Since the specified value is:
spring.liquibase.change-log=classpath:db/changelog/db.changelog-root.yaml
Could you put the file into:
src/main/resources/db/changelog/db.changelog-root.yaml?

How to connect Spring and Oracle db (oracle cloud) 19c

if i try to connect this error show
oracle.net.ns.NetException: Invalid connection string format, a valid format is: "host:port:sid" (CONNECTION_ID=plSfz1GBTiKKWbZqZbjUrA==)
i think yml datasource or jpa is wrong...
but I checked out gogle anywhere but,
oracle cloud(wallet) <-> spring is I can't found it
plz help me!
this is my build.gradle
plugins {
id 'org.springframework.boot' version '2.5.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'war'
}
group = ''
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.projectlombok:lombok:1.18.20'
implementation 'junit:junit:4.13.1'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
compileOnly 'org.projectlombok:lombok:1.18.12'
annotationProcessor 'org.projectlombok:lombok:1.18.12'
runtimeOnly 'com.oracle.database.jdbc:ojdbc8'
implementation 'com.oracle.ojdbc:ucp'
implementation 'com.oracle.database.security:oraclepki'
implementation 'com.oracle.database.security:osdt_core'
implementation 'com.oracle.database.security:osdt_cert'
annotationProcessor 'org.projectlombok:lombok'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
this is my application.yml
# local, dev, prod 공통 설정
server:
port: 8080
tomcat:
uri-encoding: UTF-8
spring:
datasource:
driver-class-name: oracle.jdbc.OracleDriver
url: jdbc:oracle:thin:#[TNSNAME]?TNS_ADMIN=./src/main/resources/Wallet
username:
password:
jpa:
database-platform: org.hibernate.dialect.Oracle12cDialect
open-in-view: true
hibernate:
ddl-auto: none
# ddl-auto: create-drop
# ddl-auto: validate
If you use TNS_ADMIN, it has to be path to TNS_ADMIN file, and your connection has to be declared in tnsnames.ora.
Related reading: https://docs.oracle.com/en/cloud/paas/autonomous-database/adbsa/connect-jdbc-thin-wallet.html#GUID-BE543CFD-6FB4-4C5B-A2EA-9638EC30900D
Refer to this blog for code sample and also instructions.
i resolved this way,
spring:
datasource:
driver-class-name: oracle.jdbc.OracleDriver
url: jdbc:oracle:thin:#<***TLSNAME>?TNS_ADMIN=D:/Wallet
username: username
password: password

Configure application.properties in spring boot for public url in keycloak

I'm developing spring boot application (v 2.1.0) with keycloak to secure app (v 4.5.0).
I already configured keycloak security in gradle and application.properties.
However keycloak returns unauthorized (401 Error) even for urls which are not added to security constraints.
Gradle and application.properties file are provided following
Gradle
buildscript {
ext.kotlin_version = '1.3.11' // Required for Kotlin integration
ext.spring_boot_version = '2.1.0.RELEASE'
ext.keycloak_version = '4.5.0.Final'
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // Required for Kotlin integration
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#spring-support
classpath "org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version"
}
}
apply plugin: 'kotlin' // Required for Kotlin integration
apply plugin: "kotlin-spring" // https://kotlinlang.org/docs/reference/compiler-plugins.html#spring-support
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
jar {
baseName = 'gs-rest-service'
version = '0.1.0'
}
repositories {
jcenter()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-data-rest')
compile group: 'org.hibernate', name: 'hibernate-envers', version: '5.1.0.Final'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" // Required for Kotlin integration
compile("org.jetbrains.kotlin:kotlin-reflect")
compile "org.springframework.boot:spring-boot-starter-web"
// compile 'org.springframework.boot:spring-boot-starter-security'
//KeyCloak
compile group: 'org.keycloak', name: 'keycloak-spring-boot-starter', version: '4.7.0.Final'
compile group: 'org.keycloak', name: 'keycloak-spring-boot-adapter', version: '4.7.0.Final'
compile "org.keycloak:keycloak-admin-client:$keycloak_version"
//RestEasy
// https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-client
compile group: 'org.jboss.resteasy', name: 'resteasy-client', version: '3.6.2.Final'
// https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jaxrs
compile group: 'org.jboss.resteasy', name: 'resteasy-jaxrs', version: '3.6.2.Final'
// https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jackson2-provider
compile group: 'org.jboss.resteasy', name: 'resteasy-jackson2-provider', version: '3.6.2.Final'
// //Oauth2
// // https://mvnrepository.com/artifact/org.springframework.security.oauth/spring-security-oauth2
// compile group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version: '2.3.4.RELEASE'
// https://mvnrepository.com/artifact/org.springframework/spring-jdbc
compile group: 'org.springframework', name: 'spring-jdbc', version: '5.1.0.RELEASE'
// https://mvnrepository.com/artifact/org.postgresql/postgresql
compile group: 'org.postgresql', name: 'postgresql', version: '9.3-1100-jdbc41'
compile group: 'org.postgresql', name: 'postgresql', version: '42.2.5'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
Application.properties
#Server
server.port = 8090
# Database
spring.jpa.database=postgresql
#spring.datasource.platform
spring.jpa.show-sql=false
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/dbname
spring.datasource.username=postgres
spring.datasource.password=123
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
# new
keycloak.realm = realm
keycloak.auth-server-url = http://localhost:8080/auth
keycloak.ssl-required = external
keycloak.resource = client_id
keycloak.credentials.secret = client_secret
keycloak.realm-key=public_key
keycloak.security-constraints[0].auth-roles[0]=USER
keycloak.security-constraints[0].security-collections[0].patterns[0]=/user/*
keycloak.security-constraints[0].security-collections[0].patterns[1]=/createStudent
keycloak.security-constraints[0].auth-roles[1]=admin
keycloak.security-constraints[0].security-collections[1].patterns[0]=/createStudent2
keycloak.security-constraints[0].security-collections[1].patterns[1]=/createRole
keycloak.security-constraints[0].security-collections[1].patterns[2]=/roles
keycloak.security-constraints[0].security-collections[1].patterns[3]=/assignRole
keycloak.security-constraints[0].security-collections[1].patterns[4]=/users
I want login and main page of the app to be public
/login and /home
Thanks in advance!

Zuul and Consul integration issue

I have problem setting up Spring Cloud application with Zuul and Consul service discovery. I have Consul server agent installed and running locally:
./src/main/bash/local_run_consul.sh
When I run Spring Boot application with #EnableZuulProxy annotation I get the following error:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.netflix.zuul.filters.RouteLocator]: Factory method 'routeLocator' threw exception; nested exception is java.lang.IllegalStateException: Unable to locate service in consul agent: edge-server-8765
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
... 73 more
Sources:
build.gradle
buildscript {
ext {
springBootVersion = '1.2.7.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'war'
apply plugin: 'spring-boot'
war {
baseName = 'edge-server'
version = '0.0.1-SNAPSHOT'
}
mainClassName = 'com.akamai.pulsar.onboarding.edge.app.ZuulApplication'
dependencies {
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: 'spring-boot-starter-tomcat'
}
compile("org.springframework.boot:spring-boot-starter-undertow")
compile("org.springframework.boot:spring-boot-starter-actuator")
compile('org.springframework.cloud:spring-cloud-starter-zuul:1.0.0.RELEASE')
compile('org.springframework.cloud:spring-cloud-starter-consul-all:1.0.0.M3')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
application.yml
info:
component: Zuul Server
endpoints:
restart:
enabled: true
shutdown:
enabled: true
health:
sensitive: false
zuul:
ignoredServices: "*"
routes:
test-api:
path: /test-api/**
server:
port: 8765
logging:
level:
ROOT: INFO
org.springframework.web: INFO
bootstrap.yml
spring:
application:
name: edge-server
cloud:
consul:
host: localhost
port: 8500
config:
enabled: true
ZuulApplication.java
#SpringBootApplication
#EnableDiscoveryClient
#EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ZuulApplication.class).web(true).run(args);
}
}
Note: When I comment out the #EnableZuulProxy annotation, my service runs without problems and is registered in Consul correctly.
I have no idea what to do. Maybe there is some bad dependency issue?
Thanks in advance.
There is a bug in spring-cloud-consul with ConsulDiscoveryClient.getLocalServiceInstance.
Setting zuul.ignoreLocalService: false will work around it for now.

Resources