|
Modifying the Hibernate Configuration File
In this exercise you will edit the default properties specified in hibernate.cfg.xml to enable debug logging for SQL statements.
Open hibernate.cfg.xml in the Design tab. You can open the file by expanding the Configuration Files node in the Projects window and double-clicking hibernate.cfg.xml. Expand the Configuration Properties node under Optional Properties. Click Add button to open the Add Hibernate Property dialog box.
In the dialog box, select the hibernate.show_sql property and set the value to true. Click OK. This enables the debug logging of the SQL statements.
|
Click Add under the Miscellaneous Properties node and select hibernate.query.factory_class in the Property Name dropdown list.
Manually, type org.hibernate.hql.classic.ClassicQueryTranslatorFactory as the Property Value. Click OK.
If you click the XML tab in the editor you can see the file in XML view. Your file should look like the following:
The following is the XML file content.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<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.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
</session-factory>
</hibernate-configuration>
The following screenshot shows the content of the real hibernate.cfg.xml file.
---------------------------------------------------------------------
Save your changes to the file.
When you run your project you will be able to see the SQL query printed in the IDE's Output window. We will see this later.
To use Hibernate you need to create a helper class that handles startup and that accesses Hibernate's SessionFactory to obtain a Session object. The class calls Hibernate's configure() method, loads the hibernate.cfg.xml configuration file and then builds the SessionFactory to obtain the Session object.
In this section you use the New File wizard to create the helper class HibernateUtil.java.
Select and right-click the Source Packages node > select New > select Other to open the New File wizard.
Select Hibernate from the Categories list and HibernateUtil.java from the File Types list. Click Next.
Type HibernateUtil for the class name and sakila.util as the package name. Click Finish.
When you click Finish, HibernateUtil.java opens in the editor. You can close the file because you do not need to edit the file.