Spring Boot cannot generate table in a PostgreSql database - java

I have a Spring Boot app and I created a PostgreSql database in a Docker container. I can connect to that database using tools, but when I run my Spring Boot app, it cannot create table even if the following log is seen on debug screen:
Hibernate: drop table if exists public.employees cascade
Hibernate: create table public.employees (id bigserial not null, email varchar(255), first_name varchar(255), last_name varchar(255), primary key (id))
Here is my properties file (I use PostgreSql 9+, database name is employee_management_system):
spring.datasource.url=jdbc:postgresql://localhost:5434/employee_management_system
spring.datasource.username=postgres
spring.datasource.password=******
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQL92Dialect
spring.jpa.properties.hibernate.default_schema = public
spring.jpa.hibernate.ddl-auto = create
spring.jpa.show-sql = true
I do not use a DOckerfile or docker-compose.yml (I think I do not need for this step as I already have a container for my PostgreSql database). Any idea?

Seem like your spring-boot application have access to your database. Otherwise, you won't have the logs you put and you would have an exception when you start your application.
Maybe you have to check the way your are connected to your database (identifiers, schema, etc ..)?

Related

Spring boot application not able to find sequence in AWS Postgresql

In existing spring boot application I have created new entity and for the primary key for this entity I have use annotation #GeneratedValue.
Based on entity and annotation I have created required table and sequence in the Postgresql database.(Note, Spring boot and database are existing)
However, while starting spring boot application I am getting error "Unable to build Hibernate SessionFactory Schema-validation: missing sequence", and sequence are created in database under the required schema.
Spring boot application to identify the sequence created in Postgresql and start the application.
In Postgresql DB table, we need to provide privilege (like, insert, delete update) to the user through which spring boot application will connect with DB. So that spring boot application can performed actions like read/write in table/sequence.
For example: DB user mention in application.properties file is dbuser/password, in spring boot application. Then make sure in Postgresql DB side this user, dbuser, should be owner of the the table and have access like, insert, update, delete.

Cannot add initial data to database with Spring Boot (Hibernate)

I use Hibernate in my Spring Boot app and populate the tables without any problem. However, I am trying to add 2 record to one of the populated table using import.sql on the classpath.
INSERT INTO role(id, role_type) VALUES(1, 'ADMIN');
INSERT INTO role(id, role_type) VALUES(2, 'USER');
I also set the following properties:
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.defer-datasource-initialization=true
spring.sql.init.mode=always
When I run the app, tables are created and the data is populated, and tables are created. However, I got "Error executing DDL : alter table user_role drop foreign key" error.
When I update spring.jpa.hibernate.ddl-auto as shown below, there is no error and tables are created. BUT, records are not inserted to the role table:
spring.jpa.hibernate.ddl-auto= update
So, how can I insert data when I run the app without any problem?
import.sql will only be executed provided that spring.jpa.hibernate.ddl-auto is set to create or create-drop.
Alternatively you can use data.sql it will be executed irrespective of hibernate.ddl configuration.
Using spring.jpa.hibernate.ddl-auto= update and data.sql should solve both of your error.
spring.sql.init.mode=always
spring.jpa.defer-datasource-initialization=true
spring.jpa.hibernate.ddl-auto= update
Use defer-datasource

Cannot add foreign key through JPA Spring boot by MyISAM

im learning JPA Spring boot and create relationship between many table in MySQL through JPA by annotation
#ManyToMany and #OneToMany. But when i launch MySQL to check foreign key i saw that not.
Note: foreign keys can only be defined for certain storage engines (like InnoDB). The server accepts foreign key definitions for other storage engines but silently ignores them. Switch your table engine to one that supports foreign keys to allow adjustments here.
Please help me to fix this !!! Thanks you so much.
You can change to:
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL55Dialect

Cannot populate database with liquibase

I'm building a small spring-boot application. I'm using spring-data-jpa to create the database schema and liquibase to populate it with test data.
application.properties:
spring.datasource.url=jdbc:postgresql://localhost:5432/book-db
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.username=admin
spring.datasource.password=lTIDDYz3n3jD3BeYaAJz
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
According to the documentation no configuration for liquibase is required if I have a gradle dependency and master changeLog under the default path.
db.changelog-master.yaml:
databaseChangeLog:
- changeSet:
id: 1
author: jb
changes:
- sqlFile:
path: db/migration/insert-books.sql
insert-books.sql:
--liquibase formatted sql
--changeset admin:1
delete from book;
insert into book (id, title)
values (nextval('seq'), 'Functional Programming for Mortals');
commit;
I have tried it with and without commit. The tables databasechangelog and databasechangelog are created successfully and contain the migration (insert-books).
The migration goes through, because if I add an invalid insert (to some table that does not exist), I get the exception:
ERROR: relation "xxx" does not exist
How to populate the database with data in insert-books.sql script using liquibase?
Don't use both, liquibase and JPA, to manage the DB structure. If you want to use liquibase, set JPA (Hibernate) to just validate the schema and manage the schema within liquibase.
spring.jpa.hibernate.ddl-auto=validate
The problem with your solution is in the order of operations. When your application starts, it first runs liquibase, which inserts the data, then JPA is started and the schema is created from scratch.
Try dropping the schema before running the app, I bet the migration (liquibase) will fail.
Liquibase has to be in charge of the schema, there is a way to add liquibase to an existing database, but it again makes liquibase the owner of the schema:
Using liquibase on the existing database

How to run DDL after Spring auto create db?

My question is simple. How to run any DDL statements after Spring's automatic schema creation?
I have set the following in my application-test.properties to auto create the database schema from the entity Java classes.
spring.jpa.hibernate.ddl-auto=create-drop
I'd like to change the data type of one column in one table after the schema is auto created.
I tried having a schema.sql file in the classpath, but that didn't help.
Any help please.
Scripts which you specify in script will run before it create database.
So when you fire Alter table command, you will get Table not found kind of error.
What I would suggest is to create database via SQL file only and set
spring.jpa.hibernate.ddl-auto=none so system wont create auto database.
Now to make your SQL file run, You can create the schema and initialize it based on the platform. Platform value is of spring.datasource.platform. Now you can create files schema-${platform}.sql and data-${platform}.sql which is processed by Spring Boot. It allows you to choose the database specific scripts if necessary. You can choose the vendor name of database(platform) like hsqldb, h2, oracle, mysql, postgresql, and so on.
I have tested with postgres and It worked for me. Sql file name was schema-postgres.sql
Set below properties in application.properties file
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/poc
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=none
spring.datasource.initialization-mode=always

Categories

Resources