Saturday, December 15, 2012

How to generate entities from tables with Eclipse JPA Tools

The purpose of this post is to show detail steps on how to setting up JPA project and generate entities for the JPA Project

Required Software/Tool
1. Eclipse
2. JDBC jar file for the target database

1. Create JPA Project
    p/s: if JPA Project previously created by other team member, step 1 can be skipped.
    a. File > New > Other > JPA > JPA Project, Next
    b. Fill in project details > Finish
create new JPA project
    c. project created.
new JPA project

2. Setting up JPA Connection
    a. open the JPA perspective, if it is not open yet. => Windows > Perspective > JPA
    b. R-click on Database Connections > New
new database connection
    c. Choose you database, Enter Database Name > Next
choose database
    d. Enter connection details
enter database connection details
    e. configure jdbc driver by clicking on the red highlighted box
        - choose appropriate driver version 
choose jdbc driver for chosen database
    f. locate jdbc jar file
       - click on the jar name > Edit JAR/Zip
       - browse the jar file
       - OK
locate the jdbc driver
    g. Test Connection, if following screen appears, the connection is properly configured. > Finish.
test the database connection
    h. connected datasource
connected database

3. Generate entities
    a. R-click on JPA Project > JPA Tools > Generate Entities From Tables
    b. choose Connection
    c. choose Schema
    d. choose Tables
choose tables to generate entities
    e. Next > Next
    f. Enter Pakage, Superclass, Interfaces (Optional)
enter entity package name
    g. Configure Key Generator for selected tables
        - Click on 1st table, select Key Generator, sequence (if required)
        - repeat the above step for all tables that are having Key Generator
sequence generator
    h. Finish
    i. Verify the generated entities in the defined package.

At this stage, Eclipse will still shows us a lots of compilation errors.
Follow NEXT STEP to configure the JPA project.



Related Posts Plugin for WordPress, Blogger...