Today, I wanted to auto generate JPA Entities from tables in a H2 database using JBoss Tools in Eclipse. According to the documentation this should be straight forward. There are also a youtube videos explaining the process: http://www.youtube.com/watch?v=IU7cwWMDdjU, http://www.youtube.com/watch?v=IU7cwWMDdjU
Like most simple things this was not that easy at all since it looks like the tools used in the JPA and Hibernate perspectives can read the meta data of the H2 database after having configured it BUT the wizard for building the entities located under JPA Tools -> Generate Entities from Tables can’t do this.
Looking for a solution I found the menu option New -> JPA Entities from Tables. This opens the wizard Create Custom Entities. In this wizard I could select the configured database but no schema or tables gets loaded. I learned that http://www.eclipse.org/webtools/dali/ is responsible for providing the wizard and the functionality. To be able to do so it must have some kind of meta knowledge of the database for reading its configuration. Since I’ve generated a generic JDBC connection and provided a custom name Dali could not do its job.
- Ensure that H2 is running in server mode. Otherwise, you will run into DB locking troubles.
- Go to the JPA perspective and create a new connection profile in the Data Source Explorer
- Connection Profile Type ist Generic JDBC
- Accept the default connection profile name (Maybe it would work with a custom name. I haven’t tried because I spent to much time on this issue)
- Click Next
- Don’t use an entry in the Drives drop-down. Create a new one
- Accecpt all default names
- The driver is located in the JBoss runtime in the folder modules/com/h2database/h2/main/
- Provide the JDBC connection string and you credentials
- Check if the DB meta data is accessible in the data source explorer
- Change to the Java EE perspective
- Right click on you project
- Select Properties
- Go to JPA
- Provide the connection configured above
- Check Override default catalog from connection and select the catalog your tables live in
- Check Override default schema from connection and select the schema you tables live in
- click ok
- JPA Tools -> Generate Entities from Tables still won’t work BUT the wizard in the menu New -> JPA Entities from Tables
- In this wizard you can see your tables, select them for entity creation and even provide information for further annotations. I assume that this is even more than you can do with JPA Tools -> Generate Entities from Tables
It is possible that the process wipes your data from the tables. So better be prepared!