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.
3 Answers
UPDATE:
In IntelliJ 16 this feature in now implemented.
The steps to do it are:
- Database view context menu
- Scripted Extensions
- 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)
- Go to "Project structure" (alt+ctrl+shift+s)
- In "Project settings" select "Modules"
- Press + and add the Hibernate facet in your module.
Now that you have set up your hibernate configuration facet, you can extract your POJOs.
- 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)
- There you can right-click on the hibernate icon named like your module
- Go to "Generate Persistence Mapping"-"by database schema"
- Now I guess you can find your way...
- In general, settings select the datasource that you want to use and now you can see all the tables in your datasource object
- 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!

- 5,445
- 2
- 37
- 41
-
5Also JPA facet can be used as well – nahab Nov 20 '13 at 13:43
-
@zpontikas I dont get hibernate or jpa facet option in intellij idea 12.1.3. Do I need to do anything to get that ? – Rips Apr 03 '14 at 12:42
-
13These facets are available in the Ultimate version only – zpontikas Apr 03 '14 at 13:17
-
4Many thanks. All I can add: if you can't find Persistence tab or panel mentioned in step 1, you may show it by choosing View > Tool Windows > Persistence. – Andrei Rykhalski Feb 02 '15 at 11:14
-
@AndreiRykhalski thanks. I added that to the answer just in case some1 gets lost – zpontikas Feb 02 '15 at 11:57
-
This approach gets you a hibernate dependency. The question was how to create a POJO, not how attach your project to a framework! – foo Feb 06 '18 at 22:40
-
This WAS the easiest way to generate a POJO up to version 16(15?). Now with version 16 onwards you can use the "Generate POJOs" script descriped in the "UPDATE" section – zpontikas Feb 07 '18 at 09:37
-
1i click on it and nothing happens – Kalpesh Soni May 17 '18 at 16:17
-
4It is after "now I guess you can find your way" that It fails for me. I select the source and the tables. Then it wants "mapping XML" or "JPA annotations". When I select mappings XML I cannot enter a directory. When I select Annotation it wants a session factory, when I try to add a session factory it complains there is no source root, but there is one. It also says it wants a "persistence unit" whatever that is. If I select separate XML per entity it says it does the OR generation but then nothing gets generated. – Florian F Oct 26 '18 at 11:52
The default Scripted Extensions Generate POJOs.groovy
is not very good when dealing with tables with underscore(which is very common).
So I make some modifications.
The main code
def calcFields(DasObject table) {
DasUtil.getColumns(table).reduce([]) { fields, col ->
def spec = Case.LOWER.apply(col.dataType.specification)
def typeStr = typeMapping.find { p, t -> p.matcher(spec).find() }.value
fields += [[
name : javaName(col.name, false),
type : typeStr,
annos: """
/**
* $col.comment
*/"""]]
}
}
static String javaName(String str, boolean capitalize) {
def s = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, str);
capitalize || s.length() == 1 ? s : Case.LOWER.apply(s[0]) + s[1..-1]
}
You can find the whole gist here https://gist.github.com/aristotll/ad799a7462e8b705b26103944cca24a6

- 8,694
- 6
- 33
- 53
If you have Intellij Idea and Jpa Buddy installed you can do this by
Database -> table -> right clicking -> Generate Persistence Mapping
choose table and package path ok

- 99
- 4