- The Blueprint (Your Code)
- You have a
Userclass in your project (e.g., a Java class with@Entityannotations). This class defines the fields:id,username,email, etc. This is the ultimate source of truth.
- You have a
- The Robot Carpenter (Framework on Startup):
- You start your application.
- Your framework (Spring/Hibernate) wakes up and reads your
application.propertiesorapplication.ymlfile. It seesspring.jpa.hibernate.ddl-auto = update. - It then scans all your code for blueprints (classes marked with
@Entity, like yourUserclass). - It connects to your PostgreSQL database and compares the blueprints to the existing tables.
- If it’s the 1st time running (no database)
- The tables are missing, so it generates the
CREATE TABLE users (...)andCREATE TABLE products (...)SQL commands from scratch and runs them
- The tables are missing, so it generates the
- If it’s NOT the 1st time running (db alr exists)
- Since it’s set to
update, it generates and runs the necessary SQL commands (CREATE TABLE ...,ALTER TABLE ...) to make the database schema match your code.
- Since it’s set to
- The API Call (Postman):
- You send a
POSTrequest to/userswith Postman to create a new user. - Your application’s controller code receives this request.
- Your service/business logic takes the JSON data from Postman, creates a new
Userobject, and tells the framework, “Hey, please save this object to the database.” - The framework automatically translates that
Userobject into anINSERT INTO users (...) VALUES (...)SQL statement and sends it to PostgreSQL.
- You send a
- The Database (PostgreSQL):
- PostgreSQL receives the
INSERTcommand and adds a new row to theuserstable. The data is now permanently stored.
- PostgreSQL receives the
So, what about your schema.sql file?
You have two main ways to manage your schema:
- Automatic (The Robot): Use
ddl-auto: updateand let the framework manage everything based on your entity classes. This is great for fast development. - Manual (The Hand-drawn Blueprint): Create a
schema.sqlfile with the exactCREATE TABLEstatements you want. You run this file yourself to set up the database. In this case, you would setddl-auto: validateorddl-auto: noneto prevent the robot from interfering with your manual work.