hibernate in eclipse.docx

15
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

Upload: jennyanccori

Post on 18-Jul-2016

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Hibernate in Eclipse.docx

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, 2011User Rating: / 26

Views: 31,938PDF Link Add comments

Edition: Standard or above (Edition comparison)

Page 2: Hibernate in Eclipse.docx

Setup your databaseMySQL 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 database1. 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.

Page 3: Hibernate in Eclipse.docx

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

Page 4: Hibernate in Eclipse.docx

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.

Page 5: Hibernate in Eclipse.docx

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

configuration dialog box.

Design database1. 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.

Page 6: Hibernate in Eclipse.docx

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-to-Many 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.

Page 7: Hibernate in Eclipse.docx

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 generation1. Select Modeling > ORM > Generate Database... from the main menu.

Page 8: Hibernate in Eclipse.docx

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.

Page 9: Hibernate in Eclipse.docx

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

generated code files there.

Programming with the generated Hibernate code

Page 10: Hibernate in Eclipse.docx

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.

Page 11: Hibernate in Eclipse.docx

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.

Page 12: Hibernate in Eclipse.docx

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

menu.

Page 13: Hibernate in Eclipse.docx

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

Page 14: Hibernate in Eclipse.docx

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