Skip to content

A Hybris-like entity generator, which could be used to generate database entity classes from a simple JSON definition.

License

Notifications You must be signed in to change notification settings

pateman/JSON-to-Entity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON to Entity

A Hybris-like entity generator, which could be used to generate database entity classes from a simple JSON definition. The code is fully documented and there are a few unit tests for you to have a look and learn how it works. The code is mostly finished, but I am leaving it here for someone to pick it up and adapt to their needs. The code is just an example and it could be extended however you like.

Currently it supports generating entities and relations between them. Freemarker is used for the actual source code generation, so you can easily customize the template (the default one is called defaultEntityTemplate.ftlh).

Oh, there is also a handy Gradle task for running the actual generation process. ;)

Example of JSON schema

  "entities": [
    {
      "name": "User",
      "deployment": "users",
      "classFile": "pl.pateman.entitygenerator.entity.UserEntity",
      "attributes": [
        {
          "name": "id",
          "type": "java.lang.Long",
          "flags": [
            "PRIMARY_KEY"
          ]
        },
        {
          "name": "username",
          "type": "java.lang.String"
        },
        {
          "name": "password",
          "type": "java.lang.String"
        }
      ]
    },
    {
      "name": "Customer",
      "deployment": "customers",
      "classFile": "pl.pateman.entitygenerator.entity.CustomerEntity",
      "root": {
        "name": "User",
        "extend": false
      },
      "attributes": [
        {
          "name": "username",
          "type": "java.lang.String",
          "reintroduce": true,
          "flags": [
            "UNIQUE"
          ]
        },
        {
          "name": "firstName",
          "type": "java.lang.String"
        },
        {
          "name": "lastName",
          "type": "java.lang.String"
        }
      ]
    }
    ...
  ],
  "relations": [
    {
      "source": {
        "entity": "Customer",
        "attributeName": "orders",
        "side": "ONE",
        "collectionType": "LIST"
      },
      "target": {
        "entity": "Order",
        "attributeName": "customer",
        "side": "MANY"
      },
      "joinColumn": "custId"
    }
    ...
  ]
}

Usage

Have a look at pl.pateman.entitygenerator.EntityGeneratorTest to learn how to use the generator. There is also a Gradle task called generateEntities which runs an executor to generate sources.

Third-party code and libraries

  • GSON - for parsing JSON schema
  • Reflections - for scanning the classpath for available JSON schemas
  • Apache Commons Lang3 - various utility methods used throughout the whole code
  • FreeMarker - for generating the actual source code files

About

A Hybris-like entity generator, which could be used to generate database entity classes from a simple JSON definition.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages