docs / guides/schema-driven-entitiesspring-cli

Generate JPA entities from a schema ​

The schema apply command turns a JSON entity definition into Java JPA entity classes. It is useful when you want the table and field structure described in data before filling in application behavior.

Create an entity schema ​

By default, the command reads schema.json from the project root. Create a file with an entities object keyed by each entity's logical name:

json
{
  "entities": {
    "category": {
      "tableName": "categories",
      "id": {
        "name": "id",
        "type": "long",
        "strategy": "identity"
      },
      "attributes": {
        "name": {
          "databaseColumn": "name",
          "type": "string",
          "required": true
        }
      }
    },
    "product": {
      "tableName": "products",
      "id": {
        "name": "id",
        "type": "long",
        "strategy": "identity"
      },
      "attributes": {
        "name": {
          "databaseColumn": "name",
          "type": "string",
          "required": true
        },
        "price": {
          "databaseColumn": "price",
          "type": "bigdecimal",
          "required": true
        }
      },
      "relations": {
        "category": {
          "kind": "ManyToOne",
          "target": "category",
          "joinColumn": "category_id",
          "required": true
        }
      }
    }
  }
}

Relation target values refer to keys under entities; a target that is not defined is rejected before generation.

Generate the classes ​

From the project directory, run:

bash
spring-cli schema apply

spring-cli writes JPA entities into the project's domain package. Generated classes include @Entity and @Table, an ID field, annotated attributes and relations, and JavaBean getters and setters. They are a starting point for your application; review them and add domain behavior as needed.

If a target entity file already exists, generation stops rather than overwriting it. Use --force only when you intend to replace existing generated entities:

bash
spring-cli schema apply --force

Choose a different schema file ​

Set project.schemaFile in spring-cli.json to another project-relative filename, such as models/domain.json. Absolute paths and paths that traverse above the project root are not allowed. See the entity schema reference for every field and supported value.

Two files named schema.json

The repository's published schema.json validates the spring-cli.json project config. The schema.json in your generated project is an entity definition consumed by spring-cli schema apply. They serve different purposes.