Configuring Hibernate
Now we get to the fun stuff, which is usually the most challenging but sometimes frustrating as well.
Create a folder called classes in your WEB-INF folder. In the classes folder, create a file called hibernate.cfg.xml with the following code:
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/springMVCHibernate</property> <property name="connection.username">root</property> <property name="connection.password">testuser</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property> <mapping class="com.springMVCHibernate.dt.RegistrationDT" /> </session-factory> </hibernate-configuration>
As you can see, this code sets up our database properties.
I'm using MySQL in this example, but there are a couple of ways to tell Hibernate about the classes that will be mapped to your database tables.
Method 1: One way is to create hibernate mapping files, one for each table. For example, if we have a table called Employee, we create a file called employee.hbm.xml and add elements to that file that map the Employee class to the Employee table. Furthermore, we list these mappings in the hibernate.cfg.xml file, using the property tag with the mappingResources attribute:
<property name="mappingResources"> <list> <value>./resources/employee.hbm.xml</value> </list> </property>
Method 2: The other way is to add annotations to your table class that will map the object properties to your database columns, thus eliminating the need for mapping files. In the hibernate.cfg.xml file, we would register a class as follows:
<mapping class="com.springMVCHibernate.dt.RegistrationDT" />
The mapping above tells Hibernate that I have a class called RegistrationDT that includes annotations that map the class to my database Registration table.
The RegistrationDT class could have just been called "Registration," but I add the DT suffix to my data transfer classes. These classes are also referred to as view objects (VOs) and simply contain your getters/setters for your table fields.
Before continuing, you should add your database driver JAR file to the WEB-INF\classes directory. Even though your application will find the connection in the WEB-INF\lib directory, Hibernate will not. Hibernate will find it in the WEB-INF\classes directory.
Hibernate should now be configured to work properly.