Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Java Hibernate Cookbook

You're reading from   Java Hibernate Cookbook Over 50 recipes to help you build dynamic and powerful real-time Java Hibernate applications

Arrow left icon
Product type Paperback
Published in Sep 2015
Publisher
ISBN-13 9781784391904
Length 250 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

Table of Contents (10) Chapters Close

Preface 1. Setting Up Hibernate FREE CHAPTER 2. Understanding the Fundamentals 3. Basic Annotations 4. Working with Collections 5. Working with Associations 6. Querying 7. Advanced Concepts 8. Integration with Other Frameworks 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
You have been reading a chapter from
Java Hibernate Cookbook
Published in: Sep 2015
Publisher:
ISBN-13: 9781784391904
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image