Book Image

Java Hibernate Cookbook

Book Image

Java Hibernate Cookbook

Overview of this book

This book will provide a useful hands-on guide to Hibernate to accomplish the development of a real-time Hibernate application. We will start with the basics of Hibernate, which include setting up Hibernate – the pre-requisites and multiple ways of configuring Hibernate using Java. We will then dive deep into the fundamentals of Hibernate such as SessionFactory, session, criteria, working with objects and criteria. This will help a developer have a better understanding of how Hibernate works and what needs to be done to run a Hibernate application. Moving on, we will learn how to work with annotations, associations and collections. In the final chapters, we will see explore querying, advanced Hibernate concepts and integration with other frameworks.
Table of Contents (15 chapters)
Java Hibernate Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Providing a hibernate configuration using an XML file


In the preceding discussion, you learned how to create a class and provide a mapping to hibernate. These mappings show the relationship between the Java class and the database table.

Still, hibernate requires some information about the database, host, and port, on which the application is running. It also requires information about the username and password to access the database. Hibernate uses this set of configurations to connect to the database.

This is a traditional way to provide the hibernate configuration; however here, we need to create an XML file, generally called hibernate.cfg.xml, in the classpath. There is no strict rule to name it hibernate.cfg.xml; we can give it a custom name instead of hibernate.cfg.xml, in which case, we need to instruct hibernate to load the configuration from the particular file. Otherwise, hibernate looks for the file named hibernate.cfg.xml in the classpath.

How to do it...

Now, we will create the XML file that shows the configuration for MySQL:

  1. Enter the following code in hibernate.cfg.xml to show the configuration for the applications:

    ...
    <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/kode12
      </property>
      <property name="hibernate.connection.username">
        root
      </property>
      <property name="hibernate.connection.password">
        root
      </property>
      <property name="show_sql">true</property>
      <property name="hbm2ddl.auto">update</property>
    
      <!-- List of XML mapping files -->
      <mapping resource="Employee.hbm.xml"/>
      <mapping resource="Department.hbm.xml"/>
    
      </session-factory>
    </hibernate-configuration>

How it works...

Here, we will take a look at only the basic configuration parameters. Let's understand the meaning of each property:

  • <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>: This property helps hibernate to generate database-specific SQL statements. This is an optional property. According to hibernate documentation, hibernate will be able to choose the correct implementation of dialect automatically using the JDBC metadata returned by the JDBC driver.

  • <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>: Using this property, we can provide the Fully Qualified Name (FQN) of the java driver name for a particular database. The driver class is implemented using Java and resides in the JAR file and contains the driver that should be placed in our classpath.

  • <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/kode12</property>: Using this property, we can provide the physical location of the database; however, the connection URL may vary from database to database. Here, we will use the MySQL database, so the URL shows jdbc:MySQL://<host/computer-name/ip>:<port>/<database name to connect>.

  • <property name="hibernate.connection.username">root</property>: Using this property, we can provide the username to access a particular database.

  • <property name="hibernate.connection.password">root</property>: Using this property, we can provide the password to access a particular database.

  • <property name="show_sql">true</property>: The possible value for this property is either true or false. This is an optional property. Hibernate logs all the generated queries that reach the database to the console if the value of show_sql is set to true. This is useful during basic troubleshooting. Hibernate will use the prepared statement so that it does not display the parameter in the output window. If you want to see this parameter as well, you will have to enable the detailed log. Log4j is preferred for the detailed log.

  • <property name="hbm2ddl.auto">create</property>: The possible values are validate, update, create or create-drop. This is also an optional property. Here, we will set value=create so that it will remove all the schemas and create a new one using the hibernate mapping on each build of sessionfactory. For value=update, hibernate will update the new changes in the database.

    Note

    Do not use the hbm2ddl.auto property in the production environment because it may remove all of the data and schema. So, it's best practice to avoid it in the production environment.

  • <mapping resource="Employee.hbm.xml"/>: All of the mapping file is declared in the mapping tag, and the mapping file is always named xx.hbm.xml. We can use multiple mapping tags for multiple mapping files.

    Here is an example:

    <mapping resource="Employee.hbm.xml"/>
    <mapping resource="Department.hbm.xml"/>

There's more…

Here are some useful properties used in hibernate:

  • hibernate.format_sql:

    • The possible values are true and false

    • It shows the hibernate-generated queries in the pretty format if set as true

  • hibernate.connection.pool_size:

    • The possible value is always greater than 1 (value >= 1)

    • It limits the maximum number of pooled connections

  • hibernate.connection.autocommit:

    • The possible values are true and false

    • It sets the autocommit mode for JDBC