Using Hibernate in a Web Application 2

(With NetBeans & MySQL)

 

 

 

 

Creating the Web Application Project

 

In this exercise you will create a web application project and add the Hibernate libraries to the project. When you create the project, you will select Hibernate in the Frameworks panel of the New Project wizard and specify the database.

 

  1. Choose File > New Project (Ctrl-Shift-N). Select Web Application from the Java Web category and click Next.

 

Creating new Java web application

 

 

 

 

 

  1. Type DVDStore for the project name and set the project location if needed.

 

  1. De-select the Use Dedicated Folder option, if selected. Click Next.

For this tutorial there is little reason to copy project libraries to a dedicated folder because you will not need to share libraries with other users.

 

Providing the Java web application project name and location

 

  1. Set the server to GlassFish v3 and set the Java EE Version to Java EE 6 Web. Click Next.

 

Selecting GlassFish web server for Java web application project in NetBeans

 

  1. Select the JavaServer Faces checkbox and use the default JSF 2.0 libraries.

  2. Select the Hibernate 3.2.5 checkbox.

  3. Select the sakila database from the Database Connection drop down list. Click Finish.

 

Adding JavaServer Faces (JSF) and Hibernate frameworks into the Java web application

 

Note: If the sakila database is not available as an option in the Frameworks panel in the wizard, check to see if the connection is listed under the Databases node in the Services window. If the connection is not there, you need to create the database connection.

When you click Finish, the IDE creates the web application project and opens the hibernate.cfg.xml file and index.xhtml in the editor.

If you expand the Libraries node in the Projects window, you can see that the IDE added the Hibernate libraries to the project.

 

 

 

 

 

 

---------------------------------------------------------------------

 

The Hibernate libraries for Java web application seen in NetBeans

 

Modifying the Hibernate Configuration File

 

When you create a new project that uses the Hibernate framework, the IDE automatically creates the hibernate.cfg.xml configuration file at the root of the context classpath of the application (in the Files window, src/java). The file is located in the <default package> under the Source Packages node in the Projects window. The configuration file contains information about the database connection, resource mappings, and other connection properties. You can edit the file using the multi-view editor, or edit the XML directly in the XML editor.

In this exercise you will edit the default properties specified in hibernate.cfg.xml to enable debug logging for SQL statements and to enable Hibernate's session context management.

 

  1. Open hibernate.cfg.xml in the Design tab. You can open the file by expanding Source Packages > <default package> in the Projects window and double-clicking hibernate.cfg.xml.

 

The Hibernate configuration file in XML format seen in NetBeans

 

  1. In the multi-view XML editor, expand the Configuration Properties node under Optional Properties.

 

Changing some of the  Hibernate properties for Java web applications

 

  1. Click Add to open the Add Hibernate Property dialog box.

 

Adding Hibernate property page - adding hibernate.show_sql

 

  1. In the dialog box, select the hibernate.show_sql property and set the value to true. This enables the debug logging of the SQL statements.

 

Do some settings for hibernate.show_sql

 

 

 

 

  1. Expand the Miscellaneous Properties node and click Add.

 

Settings miscellaneous Hibernate properties

 

  1. In the dialog box, select the properties hibernate.current_session_context_class and set the value to thread to enable Hibernate's automatic session context management.


Adding another hibernate properties - hibernate.current_session_context_class

 

If you click the XML tab in the editor you can see the file in XML view. Your file should look like the following:

 

<hibernate-configuration>

    <session-factory name="session1">

        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sakila</property>

        <property name="hibernate.connection.username">root</property>

        <property name="hibernate.connection.password">######</property>

        <property name="hibernate.show_sql">true</property>

        <property name="hibernate.current_session_context_class">thread</property>

    </session-factory>

</hibernate-configuration>

 

The hibernate config file seen in XML format

 

  1. Save your changes to the file.

 

You can close the file because you do not need to edit the file again.

 

 

 

 


Java, ORM & Hibernate 1 | Back to Main | Java, ORM & Hibernate 3