39

How can I generate JPA2 compliant @Entity from existing Databases?.

I found this: Question

Still its not clear if JBoss will generate compliant JPA2 and also I would like to know if there is a vendor independent way to do this.

Community
  • 1
  • 1
will824
  • 2,203
  • 4
  • 27
  • 29

9 Answers9

23

You can use a plugin like Eclipse Dali to do the trick for you. You can refer to the documentation, section 3.11 Generating Entities from Tables.

I do not know of any specific vendor independent tool to do this, though.

Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
  • 1
    I have checked Eclipse Dali, but it seems to be compatible only with JPA1 (EclipseLink 1.1.x) and when I tried to generate the entities (seccion 3.11) list of tables where empty even selecting correct schema and current DB driver... :( – will824 Apr 29 '11 at 15:48
  • @Will824 I am using Eclipse Dali with JPA 2.0 with Hibernate 3.6 implementation in this moment. – Edwin Dalorzo Apr 29 '11 at 16:11
  • How do you manage to use Eclipse Dali with JPA2.0?, the version available in Eclipse webpage only asks for 1.1.x libraries. – will824 May 03 '11 at 13:32
  • @will824 Which version of Dali did you installed? I have Dali Persistence Tools 2.3.3. which I got from the [Eclipse Update Site](http://download.eclipse.org/webtools/updates/) – Edwin Dalorzo May 03 '11 at 17:28
  • @Edalorzo: I am using version 2.2.2, which is the one that appears when I tried to install from Eclipse Webtools update: http://download.eclipse.org/webtools/updates I might then have to install from other location??? – will824 May 03 '11 at 22:17
  • 1
    @will824 You can give it try to [this](http://download.eclipse.org/webtools/repository/helios) – Edwin Dalorzo May 03 '11 at 23:53
  • @Edalorzo Thanks!, it seems it cannot be isntalled in Eclipse previous version, I just realized I am using Galileo while Dali new version is for Helios... – will824 May 05 '11 at 20:26
  • 1
    Dali 3.3 has been released as a part of the [Eclipse Kepler](http://www.eclipse.org/kepler) release. – AzizSM Jul 22 '13 at 09:15
  • @EdwinDalorzo - I have a related question. Please help me. https://stackoverflow.com/questions/24988901/convert-dynamic-web-project-to-jpa-project – james Jul 28 '14 at 05:41
17

Try using OPENJPA Reverse mapping tools. They offer lot more facility and are easy to configure. This example would clarify.

If you are using maven as your build tool, add this entry to your pom.xml

    <plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2</version>
<configuration>
<mainClass>org.apache.openjpa.jdbc.meta.ReverseMappingTool</mainClass>
<commandlineArgs>
    -directory src/main/java -accessType fields
    -useGenericCollections true -package org.yourproject.model
    -metadata none -annotations true
    -innerIdentityClasses false -useBuiltinIdentityClass false
    -primaryKeyOnJoin false
    </commandlineArgs>
<includePluginDependencies>true</includePluginDependencies>
</configuration>
<dependencies>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.CR3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.openjpa</groupId>
        <artifactId>openjpa-all</artifactId>
        <version>2.0.1</version>
    </dependency>
</dependencies>
    </plugin>

Also add following properties in the persistence.xml which lies in your META-INF folder of your resources. These would be harnessed by openjpa tool to establish connection to the database.

    <properties>
<property name="openjpa.ConnectionUserName" value="${db.username}"/>
<property name="openjpa.ConnectionPassword" value="${db.password}"/>
<property name="openjpa.ConnectionURL" value="${db.url}"/>  
<property name="openjpa.ConnectionDriverName"  value="${db.driver.class}"/>     
   </properties>

To generate the Entity files simply launch the maven goal in the project directory using mvn org.codehaus.mojo:exec-maven-plugin:java and it will generate the files at the desired location.

  • I also had to add the classpath using `.` and put the persistence in META-INF/persistence.xml – Pool Jun 13 '14 at 09:01
  • To execute it in the project directory you need to do mvn org.codehaus.mojo:exec-maven-plugin:java – user467257 May 13 '15 at 14:39
  • 1
    @hussian-pithawala I getting this error ` org.apache.openjpa.util.UserException: The persistence provider is attempting to use properties in the persistence.xml file to resolve the data source. A Java Database Connectivity (JDBC) driver or data source class name must be specified in the openjpa.ConnectionDriverName or javax.persistence.jdbc.driver property. The following properties are available in the configuration: "org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl@ce8c062f"` – Beto Neto Oct 30 '17 at 11:31
5

There is an option in the Netbeans IDE to generate entities from a database.

Ryan
  • 7,499
  • 9
  • 52
  • 61
4

Telosys is probably the simplest way for this kind of code generation.

It generates the JPA entities (of course) and if you want a fully operational web app

See the web site : http://www.telosys.org

John T
  • 683
  • 8
  • 18
4

You should have a look at minuteproject on this track reverse-engineering for JPA2. It is a vendor independent tool. In release 0.5.5 it generates:

  • JPA2 entities (for table and views) and associated metamodel
  • persistence.xml (only hibernate configuration is available in 0.5.5) but you can overwrite it.
  • maven pom with querydsl integration
  • Enum classes (if specified at enrichment i.e. in config file).

I have not tried it with H2 but to do this you need to

  • add the H2 jdbc driver in $MP_HOME/application/lib/extra
  • add the driver class and your connection parameters in MinuteProject configuration.
xflorian
  • 367
  • 2
  • 5
  • 1
    Minute Project - doesn't support composite keys (primary nor foreign), not entirely configurable, adds some packages of its own. It is good for basic relationship and limited amount of tables. Good thing about it is that it supports bunch of technologies not only JPA... – Peter Apr 12 '16 at 09:06
3

Eclipse provides this function for some time now (origination from Dali, included since Kepler version). Just right-click on your JPA project -> JPA Tools -> Generate Entities from Tables:

Screenshot of the menu entry

SebastianH
  • 2,172
  • 1
  • 18
  • 29
2

MinuteProject is awesome. I generated Java classes from Oracle schema for JPA2. And all of it with lot of ease. Follow th Steps given on the minuteproject website here is the url : go here

  • 1
    Hello. It is good to include the code as well, instead of just a link to the possible answer. – Hanlet Escaño Jun 23 '13 at 06:42
  • There was no code for this example. Just describing the use of a code generation tool. We should all know what a JPA entity is. But I definitely agree with you if it was a question that included code itself. – DtechNet Oct 05 '15 at 05:57
0

If you are a netbeans user, I get an excellent plugin in neatbeans, http://plugins.netbeans.org/plugin/53057/jpa-modeler

This video demonstrate how to use it https://www.youtube.com/watch?v=TDbZ5EGpvoY

It can be installed from NetBeans' menu Tools->Plugins

It works fine for me.

zahid9i
  • 596
  • 1
  • 8
  • 17
0

Does "generate entities from table" option works for H2 database?, I always find the schema list to be empty in the Select tables step even though I have a valid connection setup with H2 database using Generic JDBC driver.

The same connection is showing tables in hibernate-tools. For example when i create a reverse engineering xml in its editor, I can refresh the tables tab and see a full list. I dont know if hibernate tools is relevant as it might be using console configuration instead.

samarjit samanta
  • 1,285
  • 2
  • 16
  • 29
  • I dont recall seeing H2 in the list of connections within Dali, but if you want to try it, install Helios and then upgrade Dali to the latest version, they might have included more support, because what you are describing was happening to me with a lower version of Dali. – will824 May 20 '11 at 13:53
  • 1
    It apparantly does not work well with openjpa with the current version , I managed to get it working after tweaking openjpa source files. for H2Dictionary.java they have set supportsNullTableForGetPrimaryKeys=true which ends up throwing some errors. I don't know if I did any mistake in configuration, but i believe its a bug. Anyways what I want to stress is that the java classes that gets generated are half as good as the ones generated from hibernate tools. Reason being it does not name referenced columns properly. Also does not generate annotations. So i would prefer hibernate tools – samarjit samanta May 21 '11 at 11:42