Thursday, March 19, 2015

How to generate JPA entity from table by using Eclipse?

We've created tables in our database. We are going to use Eclipse to generate JPA entities from those existing tables.

1. Create or convert to a JPA project.
In Eclipse, if this is a new project, make it as a JPA project. If a project has been created and it is not a JPA project, we need to convert it into a JPA project.
To convert a current project to JPA project, you can just right click the project name, and choose Configure, then choose Convert to JPA Project.




Then click finish button.

2. Create a database connection under Data Source Explorer 

First click on the Window menu on top. Choose Show View, then click other.


Choose Data Source Explorer. Then click OK.


Now Data Source Explorer will show on the lower tab. Choose it, and right click on the Database Connections, you can create a new db connection here.







After a new connection is created, when we click on the name, the database will be connected. We can see tables here.

3. Generate JPA entities.

Right click on the project name, choose new, then Other, if JPA Entities from Tables not show.



Choose a connection and table names you would like to generate JPA entities.




Write down your package name.


Write down your class name.Then Finish.








































4. Check your generated entity code to make sure if you need to modify anything.




No comments:

Post a Comment