> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/spring/spring-features/spring-persistence/spring-data-jpa/entity-to-table-mapping.md).

# Entity-to-Table Mapping

## About

* In JPA, **an Entity** (`@Entity` annotated class) **represents a table** conceptually.
* However, **just creating a Java entity class does not&#x20;*****by itself*****&#x20;create a table in the database** unless certain **conditions** are met.

It depends on:

* How our **JPA Provider** (Hibernate, EclipseLink, etc.) is configured.
* What **DDL (Data Definition Language) generation settings** we have.
* Whether the **database** already has tables manually created or not.

<table data-full-width="true"><thead><tr><th width="281.3828125">Step</th><th>Behavior</th></tr></thead><tbody><tr><td>We create an Entity class</td><td>It defines how a row of a table should look like (the <em>mapping</em>)</td></tr><tr><td>JPA provider (Hibernate) sees our Entity</td><td>It <em>can</em> generate a table based on our entity <em>if configured</em></td></tr><tr><td>Otherwise</td><td>JPA just expects the table already exists in the database</td></tr></tbody></table>

## Behind the Scenes in Spring Data JPA

When our Spring Boot application starts:

1. **Spring Boot Auto-configures Hibernate** as the default JPA provider.
2. Hibernate **scans all `@Entity` classes**.
3. Based on Hibernate settings, it decides whether to:
   * **Validate** against existing tables (check structure).
   * **Create** tables from scratch.
   * **Update** existing tables (alter, add missing columns).
   * **Do nothing** and assume tables are already correctly created.

## The Key Setting: `spring.jpa.hibernate.ddl-auto`

This property controls **whether Hibernate will create/update/validate the schema**.

<table data-full-width="true"><thead><tr><th width="185.9609375">Value</th><th>What Happens</th></tr></thead><tbody><tr><td><code>none</code></td><td>Do nothing - Hibernate does not touch database schema.</td></tr><tr><td><code>validate</code></td><td>Validate the schema - check if tables/columns match the Entities. Throws error if mismatch.</td></tr><tr><td><code>update</code></td><td>Update the schema - create new tables or columns as necessary.</td></tr><tr><td><code>create</code></td><td>Drop all existing tables and create new ones from Entity mappings on every application start.</td></tr><tr><td><code>create-drop</code></td><td>Same as create, but drops tables when the app stops. Useful for testing.</td></tr></tbody></table>

#### Example application.yml file

```yaml
spring:
  jpa:
    hibernate:
      ddl-auto: update
```

Result: Hibernate will **create** missing tables and columns if needed.

## What If We Don't Set `ddl-auto`?

* Spring Boot will **by default** use `create-drop` for an **in-memory database** like H2.
* For **production databases** (like MySQL, Postgres), we must set it manually or it might **assume** `none` or throw warnings.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/spring/spring-features/spring-persistence/spring-data-jpa/entity-to-table-mapping.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
