Jump to Menu

NetBeans Tutorial: How to Access Database WITHOUT SQL?

Object relational Mapping (ORM) is a way to virtually map Java objects with relational database to aid in object-oriented programming. 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.

Compatible edition(s): Enterprise, Professional, Standard

  • April 22, 2016
  • Views: 49,340
  • PDF

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, 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. Login your SQL Server via Microsoft SQL Server Management Studio.
    Login MS SQL server
  2. Right-click on the Databases node in Object Explorer and select New Database... from the popup menu.
    New database
  3. Enter the name of the database. In this tutorial, we name the database as AutoPartsStore.
    Entering database name
  4. Click OK to create the database.

Creating Java project in NetBeans

  1. Start the NetBeans IDE.
  2. Click the New Project button at the toolbar to open the New Project window.
    New Java project in NetBeans
  3. In the New Project window, select Java category and choose Java Class Library as the project type. Click Next.
    Select Java class library
  4. Enter Auto Parts Store in the Project Name field. Leave other settings as default and click Finish to create the project.
    Entering project name

Starting Visual Paradigm in NetBeans

Now, we have to start up Visual Paradigm in NetBeans.

  1. Right-click on your Java class library project and select Open Visual Paradigm from the popup menu.
    Open Visual Paradigm
  2. If you see the Memory Requirement dialog box, please keep the option Configure "etc/netbeans.conf" only and click OK to restart NetBeans, and then re-perform the previous step to open Visual Paradigm.
    Adjust memory settings
  3. You may be prompted to specify the location of your Visual Paradigm project. In this case, simply select Create in default path and click OK to proceed.
    Project Structure

Configure database for your NetBeans project

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

  1. Select Modeling > ORM > Database Configuration... from the main menu
    Open database configuration
  2. Select MS SQL Server from the database list.
    Select MS SQL Server
  3. Select 2008 or higher in the Version field.
    Select MS SQL Server version
  4. Leave the Driver field unchanged (selecting jDTS Driver), then press the green down arrow button to download the required driver. You may need to specify your proxy server for accessing Internet and for downloading the driver.
    Download JDBC driver
  5. Next, fill in the hostname, port number, the Database name as well as your User name and Password to access for the database.
    Connection settings entered
  6. Click Test Connection 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 ERD

We can start create data model for our project.

  1. Right-click on Entity Relationship Diagram in Diagram Navigator and select New Entity Relationship Diagram from the popup menu.
    New Entity Relationship Diagram
  2. Select Entity in the diagram toolbar and click on the diagram to create an entity. Name it Category.
    New entity
  3. Right-click on the entity and select New Column.
    New column
  4. Enter +ID : int to create a primary key with ID as name and int as type.
    Entering column 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.
    Columns created
  6. Move the mouse pointer over the Category entity. Press and drag out the Resource Catalog icon at top right.
    Using Resource Catalog
  7. Release the mouse button and select One-to-Many Relationship -> Entity from Resource Catalog.
    Create entity with one-to-many relationship
  8. Name the entity Parts.
  9. Create three columns to Parts, +ID : int as primary key, name : varchar(100) for the name of the parts, and stockQTY : int for the availability of the parts.
    Parts entity created

Creating sample data

We can fill in some sample data for our data model in ERD. This will allow those sample data to be generated to database along with your database schema, which can be very useful in system testing.

  1. Right-click on the blank area of the ERD and select Show Table Record Editor or View Editor from the popup menu.
    Show table record editor
  2. Select the entity Category in diagram and enter two sample categories in the editor - Brake and Engine.
    Sample records entered
  3. Now, select the entity Parts and create two sample parts - the Brake Pads with quantity 250, which is under the Brake category; and the Oil Filter with quantity 500, which is under the Engine category. You can click the ... button in the foreign column (i.e. Category) to open the selected category which you have defined instead of manually entering its value.
    Sample records entered

Generate class model from ERD

We have finished designing the database with ERD. Now, we can generate a class model from it. To generate the class model out of your ERD:

  1. Select Modeling > ORM > Synchronize to Class Diagram from the main menu.
    Synchronize ERD to class diagram
  2. Click OK in the Synchronize from Entity Relationship Diagram to Class Diagram window.
    Synchronize to Class Diagram window
  3. Click OK again in the Synchronize to Class Diagram window.
    Confirm column mapping
  4. The class diagram is generated. Click the package header <default package> and enter autopartstore as the package our class model. Without specifying a package, all the classes will be placed to the project root, which may make it hard to manage.
    Enter package header

The data models are ready and we can proceed to generating Hibernate source code as well as the database.

Generate code and database

To generate Hibernate source code and the database:

  1. Select Modeling > ORM > Generate Code... from the main menu.
    Generate code
  2. Make sure we have selected Code and Database in the Generate field.
    Selected code and database
  3. In the Code tab, select Hibernate XML as the persistence Framework.
    Selected Hibernate XML
  4. Select DAO as the Persistent API.
    Selected DAO
  5. Switch to the Database tab and select Export to database to have the database schema directly generated to database.
    Select Export to Database
  6. In the Generate Sample Data field, select Yes (With Auto Generated PK) for having sample data automatically inserted into database.
    Selected generate sample data
  7. Click OK to proceed.

After that, the tables with their sample data will be generated directly to the database. You can view them in the SQL Server Management Studio.

Database exported to MS SQL

Programming with generated Hibernate code

Here comes the core part of this tutorial - to use the generated Hibernate code.

Inserting records to database

  1. Open the ormsamples.CreateAutoPartsStoreData.java.
    Opening code sample
  2. Let's comment out the code that inserts sample data into the database, and write our own code.
    Code commented
  3. The sample code already has the basic template for objects creation. Let's modify them to insert our own data. Let's define the name for the instance lautopartstoreCategory as Air-Con by using the setter method.
    PersistentTransaction t = autopartstore.AutoPartsStorePersistentManager.instance()
    try {
        autopartstore.Category lautopartstoreCategory = autopartstore.CategoryDAO.createCategory();
  4. For the lautopartstoreParts, specify its name as AC Filter with quantity 100.
    autopartstore.Parts lautopartstoreParts = autopartstore.PartsDAO.createParts();
    lautopartstoreParts.setName("AC Filter");
  5. Next, we are going to associate the lautopartstoreCategory with lautopartstoreParts. Since one 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 collection is generated based on the role name in the association, in this case, it is the parts variable in Category class.

    Your code should look like the following.
    Code modified
  6. Now, let's try out the program. Right-click on the blank area of the code editor and select Run File from the popup menu.
    Run file
  7. Open SQL Server Management Studio again and you will find that the Air-Con category and the AC Filter parts being inserted into database.
    Result updated

Retrieving records from database

Let's retrieve data using the Hibernate code.

  1. Open the ListAutoPartsStoreData.java.
    Opening code sample
  2. By default, the sample code will perform query on object type one by one. But since our Category class and Parts class are related to each other, we can make use of the association to retrieve them from database instead of query them in individual request. Let's comment on the section of querying the parts in the sample.
    Code commented
  3. Edit the sample code on querying the category and append the code for printing its name.
    int length = Math.min(autopartstoreCategorys.length, ROW_COUNT);
    for (int i = 0; i > length; i++) {
    	System.out.println("Category: ");
    	System.out.println("     " + autopartstoreCategorys[i].getName());
  4. Next, we retrieve the parts collection from category and then convert it to an array of Parts objects.
    Parts[] parts = autopartstoreCategorys[i].parts.toArray();
  5. Now, loop through the array to printout the information of the Parts which is associated with the Category.
    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 try out the sample.

You can see the details of the Category as well as its containing Parts listed out in the Output window.

Records listed in console

What this tutorial on YouTube

创造美好 共同成长

We use cookies to offer you a better experience. By visiting our website, you agree to the use of cookies as described in our Cookie Policy.