Different Ways to Configure in Hibernate 5

Before working with hibernate in your code, you will need to configure Hibernate with information regarding your database such as host, port, credentials etc. You also need to create mappings that tell hibernate how to map the state of a Java entity to the columns of its corresponding table.

We can configure various hibernate features either through annotations or through xml files (hbm files). Preferred approach currently is annotations.


You can configure Hibernate mainly in three ways:

  • Use the APIs (programmatically) to load the hbm file along with the database driver providing connection details.

  • By specifying the database connection details in an XML configuration file that’s loaded along with the hbm file. The default file name is hibernate.cfg.xml. You may aslo use another name by specifying the name explicitly.

  • By using a .properties file. The default name of this file is hibernate.properties.


Programmatic Configuration

The use of annotations is always preferred over this approach whenever possible.

The Configuration class and its API can be used to load the hbm files, to specify the database driver, and even to provide other connection details:

Configuration configuration = new Configuration()


.setProperty("hibernate.dialect", "org.hibernate.dialect.DerbyTenSevenDialect")

.setProperty("hibernate.connection.driver_class", "org.apache.derby.jdbc. MySQL5Dialect")

Similarly you can also set other properties like hibernate.connection.url, hibernate.connection.username, hibernate.connection.password.


Now you may create a ServiceRegistry and then create a SessionFactory using this ServiceRegistry. Finally you can create Session from this SessionFactory. You usually look up the SessionFactory from a JNDI context in an ApplicationServer or any other location.


As an alternative to addResource(), you can also use addClass() to add a persistent class and Hibernate will load the mapping definition for this class:

Configuration configuration = new Configuration()




You can also pack all your mapping definitions (hbm files) into a JAR file and add it to the Hibernate configuration using the addJar method.

Configuration configuration = new Configuration()

.addJar(new File("mapping.jar"))


XML file based configuration

We can also configure hibernate by specifying the database connection details in an XML configuration file. The default file name is hibernate.cfg.xml. You may also use another name by specifying the name explicitly.

The following example uses the default hibernate.cfg.xml file to configure: http://javajee.com/lab-your-first-hibernate-43-program.

The method .configure() can be called on the Configuration object to load this default file:

Configuration configuration = new Configuration().configure();

The constructor Configuration() loads the hibernate.properties file, and the configure() method loads the default hibernate.cfg.xml file if hibernate.properties file isn’t found.

You can load another config file using the overloaded version of configure:

new Configuration().configure("/config/sample.cfg.xml")


Property file based configuration

Similarly you may also create the hibernate.properties file in classpath to load configuration properties. As mentioned before, the constructor Configuration() loads the hibernate.properties file.


Reference: Hbm file sample

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"



<class name="com.Sample" table="SAMPLE" lazy="false">

<id name="sid">

<column name="SID" sql-type="varchar(50)" not-null="true"/>


<property name="name">

<column name="NAME" sql-type="varchar(100)" not-null="true" unique="true"/>


<many-to-one name="field1" column="COL2" cascade="all"/>




Note: Each persistent object must have an identifier.


Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)