Different Ways to Configure in Hibernate 4.3

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()

.addResource("com/hibexamples/Sample.hbm.xml")

.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()

.addClass(com.metaarchit.bookshop.Book.class)

.setProperty("hibernate.dialect",…

 

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"

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

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

<id name="sid">

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

</id>

<property name="name">

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

</property>

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

</class>

</hibernate-mapping>

 

Note: Each persistent object must have an identifier.

Tags: 

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!