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

Configuring hibernate programmatically

In the preceding section, we understood XML and the properties-based configuration. Hibernate also supports the programmatic configuration. To configure hibernate using this method, we have to work on a Java code and create an instance of the org.hibernate.cfg.Configuration class. There are multiple ways to configure hibernate.

How to do it…

First, write the following code:

Configuration configuration = new Configuration();

This will create an instance of the Configuration class using hibernate.cfg.xml or hibernate.properties, whichever is found in the classpath.

Provide the following mapping files to the configuration:

configuration = configuration.addResource("Employee.hbm.xml");
configuration = configuration.addResource("Department.hbm.xml");

You can use an alternate way, as shown in the following code:

Configuration configuration = new Configuration().addResource("Employee.hbm.xml").addResource("Department.hbm.xml");

We can also provide a direct mapping using the class, as shown in the following code:

configuration = configuration.addClass("Department.class");

This will also look for Department.hbm.xml.

We can also set a custom property. To set up the custom property, use the following method:

configuration.setProperty(propertyName, value);

For example, consider the following code:

configuration.setProperty("show_sql", true);

To set up multiple properties using the properties object, execute the following code:

configuration.setProperties(java.util.Properties properties);

Here is an example:

Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
properties.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
properties.put("hibernate.connection.url", "jdbc:mysql://localhost:3306/kode12");
properties.put("hibernate.connection.username", "root");
properties.put("hibernate.connection.password", "root");
properties.put("show_sql", "true");
properties.put("hbm2ddl.auto", "update");
configuration.setProperties(properties);

To read the mapping from the URL, you can use the following code:

configuration = configuration.addURL(java.net.URL url);

To read the mapping from the XML file, you can use the following code:

configuration = configuration.addXML(String xml);

How it works…

When we select the programmatic configuration option, the Configuration class is very important. Using the instance of the Configuration class, we will build a SessionFactory object, as shown in the following code:

SessionFactory sessionFactory = new Configuration().buildSessionFactory();

When the preceding code is executed, it creates a SessionFactory object using a .properties or .cfg file or whichever source is provided to create the configuration.

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