Hibernate envers, liquibase and postgres. entity_aud is not found - spring-boot

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

Related

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

Session not created in Spring Session

Currently, I am testing Spring Session using Spring Boot
When I add Spring Security to a dependency, Session is normally created and stored in the spring_session table, but if I remove the Spring Security dependency, Session is not created and is not saved in the spring_session table.
My current settings are as below.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.session:spring-session-jdbc'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
compileOnly 'org.postgresql:postgresql'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
}
spring:
profiles:
active: local
---
server:
servlet:
session:
timeout: 3600
spring:
config:
activate:
on-profile: local
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost/dummy
username: post
password: post
jpa:
hibernate:
ddl-auto: create-drop
session:
store-type: jdbc
jdbc:
initialize-schema: always

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 + Hibernate + Grails ignores ddlAuto in yml file

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'
}

Resources