开发者

Generate JPA 2 Entities from existing Database

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 inde开发者_如何转开发pendent way to do this.


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.


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.


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


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.


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


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:

Generate JPA 2 Entities from existing Database


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜