16
Visual Paradigm Working with Hibernate in NetBeans Tutorial http://www.visual-paradigm.com/tutorials/hibernateinnetbeans.jsp Page 1 of 16 Working with Hibernate in NetBeans Written Date : November 21, 2014 Object relational Mapping (ORM) is a way to virtually map Java objects with relational database to aid in object-oriented programming and Hibernate is one of the most popular ORM framework on the market. With Visual Paradigm NetBeans Integration , one of the most popular IDE on the market, you can have an all-in-one modeling plugin for your NetBeans IDE . You can design database with ERD tool and design system with UML diagrams . You can also generate database and executable Java Hibernate source code out of your diagrams. In this tutorial, we will show you the step-by-step procedure to try out object-relational mapping by first, define the data model in ERD with sample data, then generate class diagram from ERD, generate database and hibernate code and finally use the generated hibernate code to insert data to database and retrieve data from database. As the focus of this tutorial is how you can work with hibernate together with NetBeans and Visual Paradigm, a minimal example will be used. Visual Paradigm Standard Edition , NetBeans 8.0.1 and Microsoft SQL Server will be used in this tutorial. Getting started We assume you have the SQL Server, NetBeans and Visual Paradigm being installed. Now let's get started. We first create a database in our SQL Server. Setup your database To setup a database in SQL Server: 1. Logon to your SQL Server via Microsoft SQL Server Management Studio.

Hibernate in Net Beans

  • Upload
    tertert

  • View
    234

  • Download
    0

Embed Size (px)

DESCRIPTION

hibernate manual

Citation preview

Visual ParadigmWorking with Hibernate in NetBeans

Tutorial

http://www.visual-paradigm.com/tutorials/hibernateinnetbeans.jsp Page 1 of 16

Working with Hibernate in NetBeansWritten Date : November 21, 2014

Object relational Mapping (ORM) is a way to virtually map Java objects with relational database toaid in object-oriented programming and Hibernate is one of the most popular ORM framework on themarket. With Visual Paradigm NetBeans Integration, one of the most popular IDE on the market, youcan have an all-in-one modeling plugin for your NetBeans IDE. You can design database with ERDtool and design system with UML diagrams. You can also generate database and executable JavaHibernate source code out of your diagrams.

In this tutorial, we will show you the step-by-step procedure to try out object-relational mappingby first, define the data model in ERD with sample data, then generate class diagram from ERD,generate database and hibernate code and finally use the generated hibernate code to insert datato database and retrieve data from database. As the focus of this tutorial is how you can work withhibernate together with NetBeans and Visual Paradigm, a minimal example will be used. VisualParadigm Standard Edition, NetBeans 8.0.1 and Microsoft SQL Server will be used in this tutorial.

Getting startedWe assume you have the SQL Server, NetBeans and Visual Paradigm being installed. Now let's getstarted. We first create a database in our SQL Server.

Setup your database

To setup a database in SQL Server:

1. Logon to your SQL Server via Microsoft SQL Server Management Studio.

Visual ParadigmWorking with Hibernate in NetBeans

Tutorial

http://www.visual-paradigm.com/tutorials/hibernateinnetbeans.jsp Page 2 of 16

2. Right click on the Database node in Object Explorer and select New Database...

3. Enter the name of the database. In this tutorial, we name the database as AutoPartsStore.

4. Press OK to proceed.

After that, the database will be created and will be listed under the Databases node. Now, let's fire upyour NetBeans.

Creating Java project in NetBeans

After your NetBeans is being fired up, we can then move on to create a Java project in it.

1. Press the New Project... button in Application Toolbar.

2. In the New Project dialog, select Java in Categories selection, then choose Java ClassLibrary under Projects list. Press Next to proceed.

Visual ParadigmWorking with Hibernate in NetBeans

Tutorial

http://www.visual-paradigm.com/tutorials/hibernateinnetbeans.jsp Page 3 of 16

3. In the New Java Class Library dialog, enter Auto Parts Store in Project Name field.

4. Press Finish to create the project.

The Java class library project is now being created.

Starting Visual Paradigm in NetBeans

Now, we have to start up Visual Paradigm in NetBeans. You can right click on your Java class libraryproject and select Start Visual Paradigm SE to start Visual Paradigm.

You may be prompted to specify the location of your Visual Paradigm project. In this case, simplyselect Create in default path and click OK to proceed.

Configure database for your NetBeans project

Before the modeling start, we have to define the database configuration for our project first.

Visual ParadigmWorking with Hibernate in NetBeans

Tutorial

http://www.visual-paradigm.com/tutorials/hibernateinnetbeans.jsp Page 4 of 16

1. Go to Modeling > ORM > Database Configuration...

2. Select MS SQL Server from the database list.

3. Select 2008 or higher in the Version field.

4. Leave the Driver field unchanged (selecting jDTS Driver), then press the green down arrowbutton to download the required driver. You may need to specify your proxy server for accessingInternet and for downloading the driver.

Visual ParadigmWorking with Hibernate in NetBeans

Tutorial

http://www.visual-paradigm.com/tutorials/hibernateinnetbeans.jsp Page 5 of 16

5. Next, we fill in the hostname, port number, the Database name as well as your User nameand Password to access for the database.

6. Press the Test Connection button to make sure the connection setting we defined is correct.

Now, everything is ready and we can start creating our data model.

Create data model with ERDWe can start create data model for our project.

1. Right click on Entity Relationship Diagram in Diagram Navigator and select New EntityRelationship Diagram.

Visual ParadigmWorking with Hibernate in NetBeans

Tutorial

http://www.visual-paradigm.com/tutorials/hibernateinnetbeans.jsp Page 6 of 16

2. Select Entity in diagram toolbar and click on the diagram to create an entity. Name it asCategory.

3. Right click on the entity and select New Column.

4. Enter +ID : int to create a primary key with ID as name.

5. Press Enter to confirm and create one more column. Enter name : varchar(50) as column name.Press Enter again to confirm and press Esc to cancel editing.

6. Move the mouse pointer over the Category entity. Press and drag out the Resource Catalogicon at top right.

Visual ParadigmWorking with Hibernate in NetBeans

Tutorial

http://www.visual-paradigm.com/tutorials/hibernateinnetbeans.jsp Page 7 of 16

7. Release the mouse button and select One-to-Many Relationship -> Entity from ResourceCatalog.

8. Name the entity Parts.

9. Create three columns to Parts, +ID : int as primary key, name : varchar(100) for the name of theparts, and stockQTY : int for the availability of the parts.

Defining sample data

We can fill in some sample data for our data model in ERD. This will allow those sample data beinggenerated to database along with your database schema, which helps in perform testing of yoursystem.

1. Right click on the blank area of the ERD, and select Show Table Record Editor or ViewEditor.

Visual ParadigmWorking with Hibernate in NetBeans

Tutorial

http://www.visual-paradigm.com/tutorials/hibernateinnetbeans.jsp Page 8 of 16

2. Select the entity Category in diagram, and define 2 sample category in the editor, the Brake andEngine.

3. Now, select the entity Parts and define 2 sample parts, the Brake Pads with quantity 250, whichis under the Brake category; and the Oil Filter with quantity 500, which is under the Enginecategory. You can press the ... button in the foreign column (i.e. Category) to open the selectedcategory which you have defined instead of manually enter its value.

Generate class model from ERD

After defining the ERD, we can then generate a class model from it. To generate the class model outof your ERD:

1. Go to Modeling > ORM > Synchronize to Class Diagram.

Visual ParadigmWorking with Hibernate in NetBeans

Tutorial

http://www.visual-paradigm.com/tutorials/hibernateinnetbeans.jsp Page 9 of 16

2. Press OK in the Synchronize from Entity Relationship Diagram to Class Diagram dialog.

3. Press OK again in the Synchronize to Class Diagram dialog.

4. Now, the class diagram is being generated, click on the <default package> and enterautopartstore as the package for our class model. Without defining the package, then all theclasses will be generated to the root level which makes it hard to manage.

Now, the data models are ready and we can proceed to generate hibernate source code as well asthe database.

Generate code and database

To generate hibernate source code and the database:

Visual ParadigmWorking with Hibernate in NetBeans

Tutorial

http://www.visual-paradigm.com/tutorials/hibernateinnetbeans.jsp Page 10 of 16

1. Go to Modeling > ORM > Generate Code...

2. Make sure we have selected Code and Database in the Generate field.

3. In the Code tab, select Hibernate XML as the persistence Framework.

Visual ParadigmWorking with Hibernate in NetBeans

Tutorial

http://www.visual-paradigm.com/tutorials/hibernateinnetbeans.jsp Page 11 of 16

4. And select DAO as the Persistent API.

5. Switch to the Database tab and select Export to database to have the database schemadirectly generated to database.

6. In the Generate Sample Data field, select Yes (With Auto Generated PK) for having sampledata automatically inserted to database.

7. When everything is done, press OK to proceed.

After that, the tables with their sample data will be generated directly to the database. And you canreview them from the SQL Server Management Studio.

Visual ParadigmWorking with Hibernate in NetBeans

Tutorial

http://www.visual-paradigm.com/tutorials/hibernateinnetbeans.jsp Page 12 of 16

Programming with generated Hibernate codeNow, here come to the core part of this tutorial, to use the generated hibernate code.

Inserting records to database

1. Open the ormsamples.CreateAutoPartsStpreData.java.

Visual ParadigmWorking with Hibernate in NetBeans

Tutorial

http://www.visual-paradigm.com/tutorials/hibernateinnetbeans.jsp Page 13 of 16

2. Since we have specified to generate sample data to database, so the code sample will includethe code for inserting sample data. As the data is already in database, therefore, we can simplycomment them out.

3. The sample code already has the basic template for objects creation. Let's modify them to insertour own data. Let's define the name for the instance lautopartsstoreCategory as Air-Con byusing the setter method.

PersistentTransaction t = autopartsstore.AutoPartsStorePersistentManager.instance() .getSession().beginTransaction();try {autopartsstore.Category lautopartsstoreCategory =autopartsstore.CategoryDAO.createCategory();lautopartsstoreCategory.setName("Air-Con");...

4. For the lautopartsstoreParts, specify its name as AC Filter with quantity 100.

...autopartsstore.Parts lautopartsstoreParts =autopartsstore.PartsDAO.createParts();lautopartsstoreParts.setName("AC Filter");lautopartsstoreParts.setStockQTY(100);...

5. Next, we are going to associate the lautopartsstoreCategory with lautopartsstoreParts. Sinceone category contains many parts (remember that we defined it as one-to-many relationship?),we can associate them by using the collection in the generated code. The name of the collectionis generated based on the role name in the association, in this case, it is the parts variable inCategory class.

...lautopartsstoreCategory.parts.add(lautopartsstoreParts);...

Visual ParadigmWorking with Hibernate in NetBeans

Tutorial

http://www.visual-paradigm.com/tutorials/hibernateinnetbeans.jsp Page 14 of 16

6. Now, let's try out the program. Right click on the blank area of the code editor and select RunFile from the popup menu.

7. Open SQL Server Management Studio again and you will find that the Air-Con category andthe AC Filter parts are being inserted into database.

Retrieving records from database

Now, we are going to retrieve data using the hibernate code.

Visual ParadigmWorking with Hibernate in NetBeans

Tutorial

http://www.visual-paradigm.com/tutorials/hibernateinnetbeans.jsp Page 15 of 16

1. Open the ListAutoPartsStoreData.java.

2. By default, the sample code will perform query on object type one by one. But since ourCategory class and Parts class are related to each other, we can make use of the association toretrieve them from database instead of query them in individual request. Let's comment on thesection of querying the parts in the sample.

3. Edit the sample code on querying the category and append the code for printing its name.

...int length = Math.min(autopartsstoreCategorys.length,ROW_COUNT);for (int i = 0; i > length; i++) { System.out.println("Category: "); System.out.println(" " + autopartsstoreCategorys[i].getName()); ...

4. Next, we retrieve the parts collection from category and then convert it to an array of Partsobjects.

...Parts[] parts =autopartsstoreCategorys[i].parts.toArray();...

5. Now, loop through the array to printout the information of the Parts which is associated with theCategory.

Visual ParadigmWorking with Hibernate in NetBeans

Tutorial

http://www.visual-paradigm.com/tutorials/hibernateinnetbeans.jsp Page 16 of 16

...for (int j = 0; j >parts.length; j++) { System.out.println("Parts:" + parts[j].getName()+ ", QTY: " +parts[j].getStockQTY());} ...

6. Right click on the blank area of the code editor and select Run File from the popup menu to tryout the sample.

And you can see the details of the Category as well as its containing Parts are being listed out in theOutput window.

What this tutorial on YouTubeWorking with Hibernate in NetBeans

Related Links1. Tutorial - Perform UML Modeling in NetBeans

2. User's Guide - NetBeans Integration

Visual Paradigm home page(http://www.visual-paradigm.com/)

Visual Paradigm tutorials(http://www.visual-paradigm.com/tutorials/)