Generate simple classes from database objects in DataGrip

twitter_post_feb_2018_blog

DataGrip lets you run scripts against the selected schema objects. For example, you can generate Java classes from tables or, if you want, classes in any other language: C#, Python, etc. You can alter or extend the default functionality, for instance, generate an HTML or Markdown report.

Out of the box, DataGrip can generate Java classes. To do this, go to the context menu of a table (or several tables) and press Scripted Extension → Generate POJO.groovy.

ContextMenu

Choose a destination folder, and that’s it! The class-file is ready.

ScriptItself

Generate POJO.groovy is just a script written in Groovy. It means that you can modify it and create a new script based on it, for example, for generating classes for other languages. From the same context menu, click on Go to scripts directory to see where this file is placed.

ActorJava

You can edit the existing one or create new scripts. Just place your new script into this folder, and it’ll be available from the context menu.

Some examples from our users:
Improvement of default script: comment support and replacing underscores in names.
POJO Generators for Entity
Generator of classes for Kotlin Squash
Export structure as markdown into the clipboard

Groovy is not the only language you can use, JavaScript is also available.

Before we also had Clojure engine and some people used it for scripting. For instance, here is the script for generating C# classes. If you strongly wish to use Clojure for scripting, please download Clojure runtime and scripting engine files here and place them in the lib folder inside the installed DataGrip folder.

We hope this post helps, feel free to ask questions if any.

This entry was posted in Tutorial, Uncategorized and tagged . Bookmark the permalink.

12 Responses to Generate simple classes from database objects in DataGrip

  1. Can some samples also be provided for other framework models like Django, PHP Doctrine, Laravel Illuminate models?

    Then the feature would be renamed Generate Model classes

    • Maksim Sobolevskiy says:

      Sorry, Stephen, we have no samples now. If you’ll develop yours, or get them from other users, we’ll be happy to add them to the list provided in this blog-post.

  2. D El says:

    This is very misleading, quite excited about what this was telling me, to find only Java is included as standard.

  3. Oleg says:

    >>> Before we also had Clojure engine and some people used it for scripting. For instance, here is the script for generating C# classes.

    Thank you for mentioning the repo. Rewrote the script to Groovy.

  4. Martijn says:

    Nice feature, though it unfortunately doesn’t work for views.

  5. Ladislav Jech says:

    I would be happy to see POJO classes being generated without any setters and getters and just use Lombok annotation @Data or @Value, i but I can do this small change myself…

    Thx anyway for this tool.

  6. Maxim says:

    What? “groovy script”? I need POJO classes, not groovy script !

  7. love says:

    I want to generate java classses
    does it can generate Java pojo classes from tables which has relationship like one to one,
    one to many, many to one, many to many??????

    Apart from Datagript, Eclipse, HIBERNATE, JPA please suggest there are any other tools that generate pojo/model classes from db tables that has relationships like one to one,
    one to many, many to one, many to many with annotations ?????????
    please notify me……

    is there are any tools that generate config files for other than eclipse, jetbrains, to generate
    config files like pom.xml, hibernate.cfg, jpa.cfg etc based on requirements………….

  8. shubham says:

    is there any way to create the .hbm.xml mapping files too?

Leave a Reply

Your email address will not be published. Required fields are marked *