If JPA is your persistence layer in your project and you have tables already defined in database. You can use JPA tools in Eclipse to automatically create corresponding Java Entity classes from those tables in database.
In this article, Java JPA entities will be created from an oracle database on LAN.
1. what you need
- Eclipse 4.3 + ( In this article, Eclipse Java EE version 4.3.2 kepler is used )
- Database connection configuration in Eclipse. In eclipse, configure a database connection to the oracle database. If you don't know how to do that, read this article first How to add oracle DB connection in Eclipse.
2. Make sure you have JPA support enabled in your projects
Righ click on the project name and select "Properties"
Use project facets if project facets not enabled
Click the "JPA" if it's not selected, then click "OK" to close the project properties window.
3. create Java classes from tables
After enabling JPA in project properties, now right click you eclipse project name again, you should see a new context menu item "JPA tools" appears. Choose "Generate Entities from tables"
If you see the "JPA tools" menu but the sub menu don't have "Generate Entities from tables" item, make sure you right click on the project name instead of a package or a class file.
Select a database connection to let Eclipse get the tables used to generated class from. (See another tutorial on how to configure a database connection in Eclipse)
After connected to the database, schema and tables will be listed below, choose the tables you need in your Java project and click "Next"
In this tutorial 2 tables are selected. There is a foreign key between 2 tables. Logically one platform can have more than one devices. It's a "1 to many" mapping.
Next Eclipse automatically create associations between tables you select. Click each association to see the details.
In the final step, Eclipse offers the chance to change generated Java class name and the field name inside the generated java class.
Click "finish" and the Java classes will be created. For example one of the Entity class created looks like:
The created Java Entity class is well annotated and ready for use!
Thank you so much!!!
ReplyDeleteFantastic and detailed post !!
Just what I needed :)
Hi.
ReplyDeleteIs there some way to do this in Eclipse Luna with a postgre db?
Thanks!
Although I haven't try this on Postgre, ( worked with Oracle and MySQL before), I think the content of this article works for it, the only thing you need to change is create a db connection to Postgre instead oracle. The rest part should be same.
Deletethis only think which eclipse is worse than netbeans, i'm using eclipse but for generating entities i like to use netbeans, for that is much better
ReplyDeleteThis is an excellent example that demonstrates the use of JPA tools options
ReplyDeleteOne of the finest way to do and well explained.
ReplyDeleteThank bro you saves a lot of mine effort.
Awesome post
ReplyDeletevery nice article.. simple and understandable... thanks
ReplyDeletebut when i click JPA a dialog box but.did not showing the OK button?
ReplyDeletethank a lot
ReplyDeleteStill useful today. Thank you
ReplyDeleteStill useful today. Thank you
ReplyDelete