Generate simple classes from database objects in DataGrip

Maksim Sobolevskiy

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.

Comments below can no longer be edited.

13 Responses to Generate simple classes from database objects in DataGrip

  1. Stephen Senkomago Musoke says:

    February 13, 2018

    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:

      February 13, 2018

      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:

    February 13, 2018

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

    • Maksim Sobolevskiy says:

      February 15, 2018

      What classes do you want to be generated?

  3. Oleg says:

    February 14, 2018

    >>> 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:

    March 29, 2018

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

  5. Ladislav Jech says:

    January 25, 2019

    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:

    March 5, 2019

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

    • Maksim Sobolevskiy says:

      March 5, 2019

      And you get POJO classes!

  7. love says:

    March 14, 2019

    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:

    July 27, 2019

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

    • Maksim Sobolevskiy says:

      July 29, 2019

      Not yet, but you can write your own extractor if you know Groovy or JavaScript

  9. Cups says:

    March 24, 2020

    unable to resolve class com.intellij.database.util.DasUtil
    @ line 3, column 1.
    import com.intellij.database.util.DasUtil
    unable to resolve class com.intellij.database.model.DasTable
    @ line 1, column 1.
    import com.intellij.database.model.DasTable
    unable to resolve class com.intellij.database.util.Case
    @ line 2, column 1.
    import com.intellij.database.util.Case

    Give some details about groovy scripts, simply clicking the button will lead to said error, and I can’t find any info on the web whatsoever!! I have tried to manually install groovy sdk, without luck, I have tried to run the script via cmd and groovy sdk, with the same result.

    This is killing my nerves really!!!!!!!!