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.
Choose File > New Project (Ctrl-Shift-N). Select Web Application from the Java Web category and click Next.
Type DVDStore for the project name and set the project location if needed.
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.
Set the server to GlassFish v3 and set the Java EE Version to Java EE 6 Web. Click Next.
Select the JavaServer Faces checkbox and use the default JSF 2.0 libraries.
Select the Hibernate 3.2.5 checkbox.
Select the sakila database from the Database Connection drop down list. Click Finish.
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.
---------------------------------------------------------------------
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.
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.
In the multi-view XML editor, expand the Configuration Properties node under Optional Properties.
Click Add to open the Add Hibernate Property dialog box.
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.
Expand the Miscellaneous Properties node and click Add.
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.
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>
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