JPA Tools -> Generate Entities from Tables does not work with H2 database

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=IU7cwWMDdjUhttp://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.

Solution

  1. Ensure that  H2 is running in server mode. Otherwise, you will run into DB locking troubles.
  2. Go to the JPA perspective and create a new connection profile in the Data Source Explorer
  3. Connection Profile Type ist Generic JDBC
  4. 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)
  5. Click Next
  6. Don’t use an entry in the Drives drop-down. Create a new one
  7. Accecpt all default names
  8. The driver is located in the JBoss runtime in the folder modules/com/h2database/h2/main/
  9. Provide the JDBC connection string and you credentials
  10. Check if the DB meta data is accessible in the data source explorer
  11. Change to the Java EE perspective
  12. Right click on you project
  13. Select Properties
  14. Go to JPA
  15. Provide the connection configured above
  16. Check Override default catalog from connection and select the catalog your tables live in
  17. Check Override default schema from connection and select the schema you tables live in
  18. click ok
  19. JPA Tools -> Generate Entities from Tables still won’t work BUT the wizard in the menu New -> JPA Entities from Tables
  20. 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!

3 Gedanken zu „JPA Tools -> Generate Entities from Tables does not work with H2 database“

  1. I have no idea why all of this is rewuired but I think you for solving this problem and taking the time to post the solution. Many thanks.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Time limit is exhausted. Please reload the CAPTCHA.