Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 independent way to do this.

like image 368
will824 Avatar asked Apr 29 '11 14:04

will824


People also ask

How eclipse generate entities from an existing database using JPA?

Right-click the JPA project in the Project Explorer and select JPA Tools > Generate Entities from Tables. On the Select Tables page of the Generate Entities from Tables wizard, select your database connection and schema. To create a new database connection, click Add connection.

How do I generate entities from an existing database in Intellij?

Generate a database entityClick Code | Generate or press Alt+Insert . Select the entity that you want to generate and press Enter .

Can we create JPA entity without ID?

If your object does not have an id, but its' table does, this is fine. Make the object an Embeddable object, embeddable objects do not have ids. You will need a Entity that contains this Embeddable to persist and query it.


2 Answers

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.

like image 123
Edwin Dalorzo Avatar answered Oct 05 '22 00:10

Edwin Dalorzo


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.

like image 31
Hussain Pithawala Avatar answered Oct 05 '22 01:10

Hussain Pithawala