Вы находитесь на странице: 1из 14

Hibernate in Eclipse

Object relational mapping (ORM) is a way to virtually map Java objects with
relational database to aid in object-oriented programming. Visual Paradigm Eclipse
integrationprovides you with an all-in-one modeling plugin for Eclipse. You can
design database with the ERD tool, and design system with UML diagrams. You
can also generate database and source code. VP-UML uses Hibernate as ORM
framework, providing Java developers with the most popular and easy-to-use
database programming solution. Here are some of the benefits of using VP-UML's
Eclipse integration for ORM:

In this tutorial, we will try out object relational mapping by first drawing an ERD,
then generate a class diagram, generate the database and code, and finally to
program the application. The tutorial is aimed at describing the use of ORM
with VP-UML in brief. Hence, the sample being used will be in minimum scale.
February 2, 2011
User Rating:
Views: 31,938
PDF Link

/ 26
Add comments

Edition: Standard or above (Edition comparison)

Setup your database


MySQL will be used as database software throughout this tutorial. You can,
however, use any other types of database products that we support. As long as you
are familiar with the database software you are using, there will be no problem in
completing this tutorial.
Before we start, create a database. Name it myshop.

Configure your database


1. In Eclipse, create a Java project My Shop.
2. Open your model from Eclipse. Right click on the project node of My
Shop in Package Explorer and select Open VP-UML from the popup menu.
3. When you are asked to select the project path, keep Create in default path selected
and click OK.

4. Select Modeling > ORM > Database Configuration... from the main menu.

5. In the Database Configuration dialog box, select the database software you use
from the list. In this tutorial, MySQL has to be selected.

6. In the Database Setting panel, press on the down arrow button next to Driver
file field. If your database software does not support downloading driver file, try to
look for it from its official website.

7. Fill in the connection information to connect to database. Make sure myshop is


entered as database name. For MySQL DB users, please select InnoDB as engine
to let the relationships between entities be maintained when generating database.

8. Click on Test Connection to verify the connection. Click OK to close the


configuration dialog box.

Design database
1. Right click on Entity Relationship Diagram in Diagram Navigator and select New
Entity Relationship Diagram from the popup menu.

2. Select Entity from diagram toolbar.

3. Click on the diagram to create an entity. Name it Shop.

4. Right click on the entity and select New Column from the popup menu.

5. Enter +id to create a primary key with id as name. Press Enter to confirm and
create one more column. Enter name : varchar(20) as column name. Click on the
diagram background to confirm editing.

6. Move the mouse pointer over the Shop entity. Press on the resource icon One-toMany Relationship -> Entity and drag it out.

7. Release the mouse button to create the entity. Name it as Product. Create two
columns +id and name : varchar(50).

8. Right click on the background of diagram and select Synchronize to Class


Diagram from the popup menu.

9. Click OK in the Synchronize dialog box.

10. Click OK in the Synchronize to Class Diagram dialog box. This produces a class
diagram.

11. Double click on the package header and enter myshop as name

Database and code generation


1. Select Modeling > ORM > Generate Database... from the main menu.

2. Check Export to database in the Database Code Generation dialog box.

3. Click OK to generate the database. When finished, you can optionally check in
MySQL whether the database has been exported or not.

4. Go back to Eclipse. Select Modeling > ORM > Generate Code... from the main
menu.
5. Change the drop down menu to generate only code.

6. Click OK to generate code. Check the Package Explorer. You can find the
generated code files there.

Programming with the generated Hibernate code

1. Open ormsamples.CreateMyShopData class in code editor. This is a sample file for


you to create data in database. Let's edit it.

2. Replace the first TODO with a setName call to the shop object.

3. Replace the second TODO with a setName call to the product object. Besides, set
also the shop of product to be the shop object above.

4. Save the changes.

5. Run this class by selecting Run > Run As > Java Application from the main
menu.

6. Let's list out the data from database. Open the ormsamples.ListMyShopData class.

7. Edit the print statements in listTestData() to print the names of shops and products.

8. Check the console output. You can see the id and name of shop and product are
printed.

Rate this Article

Вам также может понравиться