Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ IDEA 10 generate entity (POJO) from DB model

How can I generate entity (POJO) from database model using IntelliJ IDEA 10. I create "Data source" in IntelliJ but I have not any idea how can I generate the POJO.

like image 778
pierre Avatar asked Mar 10 '11 11:03

pierre


People also ask

How do I generate entities from an existing database in IntelliJ?

Generate a database entityClick Code | Generate or press Alt+Insert . Select the entity that you want to generate and press Enter .

How do I generate an entity class in IntelliJ?

Create new entities Use the New action in the Persistence tool window to create XML configuration files, persistence units, entity classes and fields with their getters and setters, embeddable objects, mapped superclasses, entity listeners, object-relational mappings, and so on. Select an item and press Alt+Insert .


1 Answers

UPDATE:
In IntelliJ 16 this feature in now implemented. The steps to do it are:

  1. Database view context menu
  2. Scripted Extensions
  3. Generate POJOs

You can read more here:
Feature request: allow "generate classes from database schema" for plain-JDBC developers


Note: The following information is about version 15 and earlier:

First, you need to tell IntelliJ that you are using Hibernate (I guess you are if you need the orm POJO of the table)

  1. Go to "Project structure" (alt+ctrl+shift+s)
  2. In "Project settings" select "Modules"
  3. Press + and add the Hibernate facet in your module.

Now that you have set up your hibernate configuration facet, you can extract your POJOs.

  1. At your bottom right horizontal panel, you will now see a tab called "Persistence" (ιf you can't find Persistence tab you may show it by choosing View > Tool Windows > Persistence)
  2. There you can right-click on the hibernate icon named like your module
  3. Go to "Generate Persistence Mapping"-"by database schema"
  4. Now I guess you can find your way...
  5. In general, settings select the datasource that you want to use and now you can see all the tables in your datasource object
  6. Now you can do many things, add relationships with the + sign, change the name and type of the POJO's properties etc. note: if you get an error and the "OK" is disabled its probably because the data type that IntelliJ found for your POJO is invalid. Just change it to the one you need and you are ready to go!
like image 151
zpontikas Avatar answered Sep 20 '22 11:09

zpontikas