Java Spring Boot - JPA - Hibernate - H2 - Database Initialization using data.sql and schema.sql
Вставка
- Опубліковано 6 гру 2024
- In this video we will learn how to initialize database using Java Spring Boot mechanism. We will use schema.sql file for DDL scripts and also data.sql for DML scripts. Using those files we will create database table and insert initial data. Later on we will also learn how to configure custom paths for those files, so they will be visible on classpath. Later on we will also create example entity together with CRUD repository to check out if we will be able to persist our objects into the database. We will use H2 Database and we will track everything web console. Everything exaplained step by step. Enjoy coding!
Subscribe now for more videos: / @codeforgeyt
Follow //CodefForgeYT on:
FB: / codeforgeyt
Twitter: / codeforgeyt
GitHub: github.com/cod...
Instagram: / codeforgeyt
hey thanks man, i did multiple projects with h2 integration after 6 months gap I forgot what I have done, wasted 2 hrs issue with inserting data. thanks man you saved my life.
I am glad that video helped you! Keep Coding!
Thank you! Very helpful! My only regret is I have not found your channel sooner
Great to hear that!
thanks for video! helped me with property "spring.datasource.data" to validate data.sql
Great to hear that! You are welcome!
Thanks a lot, man! It was really helpful.
You're welcome!
very good video! mega helpful
If we have 2 schema how can we set the schema and data?
can you import from a CSV using data.sql ? If so, how do I indicate the CSV path?
Will this work?
COPY table_schema.table FROM '~/src/main/resources/database/csv/table.csv' WITH (FORMAT csv);
Bro one doubt no need to set spring.datasource.initializationmode?
Depends, by default it is set to embedded.
@@CodeForgeYT Thanks
i followed all the steps but my schema.sql file still not executed. could you help me please
?
Hi! Check if it is in correct location, otherwise spring will be not able to find it.
@@CodeForgeYT I am facing same issue and file is at right location.
I am facing same issue and file is at right location.
resolved it , jdbc url was wrong so had to remove DB_CLOSE_DELAY=-1 and it worked
update the classpath to spring.sql.init.schema-locations=classpath:db/schema.sql
what is the difference between data.sql and import.sql?
Hey! data.sql can be used for inserting data to your database, schema must be already created. In case when you don't want to use default schema creation mechanism, you can create your own schema using schema.sql. Import.sql is specific for hibernate implementation and allows to load sql scripts. First two are spring specific and the last one is hibernate specific.