
Now it is the time to create our Hibernate configuration file. This file will contain the hibernate session configuration like the database driver,user name and password or a JNDI data source if you would like to use one,cache provider ad other session parameters. Also this file contains the entries for Object to Relation mapping that we will create later. Initially let's create the file with minimum required information and understand what does that information mean.
Create a new file hibernate.cfg.xml in src folder and add the following contents to this file.
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:XE
</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.username">
SYSTEM
</property>
<property name="connection.password">
manager
</property>
<!-- Set AutoCommit to true -->
<property name="connection.autocommit">
true
</property>
<!-- SQL Dialect to use. Dialects are database specific -->
<property name="dialect">
org.hibernate.dialect.OracleDialect
</property>
<!-- Mapping files -->
<mapping resource="com/visualbuilder/hibernate/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
As you can see,the session factory configuration is set of few properties required for a database connection. connection.url is the JDBC URL required to connect to the database. connection.driver_class is the JDBC Driver class which we normally use in Class.forName while establishing a connection to the database. connection.username and connection.password are the database user name and password respectively. connection.autocommit sets the Autocommit property of the underlying connection. We have set it to true so that we don't need to commit the transactions ourselves unless we want to do it intentionally.
This configuration is for an Oracle database at my machine. If you are unaware of how to build a URL for an Oracle database,note that the you need to replace XE with an appropriate SID of your Oracle database,localhost with the IP address of the machine where Oracle is running if not on local machine,and 1521 with the port. In most cases,you will only change the SID and rest of the parameters will remain the same.
If you are using MySQL,you should put the appropriate URL,Driver class name,user name,password and the Dialect name. The Dialect for MySQL is org.hibernate.dialect.MySQLDialect. Driver class for the MySQL is com.mysql.jdbc.Driver and the URL for MySQL is jdbc:mysql://<server>:<port>/<database-name>. The default port for MySQL is 3306.
The next important step is to add the JDBC driver jar file to the project libraries just like we added the struts libraries. For oracle,it is ojdbc14.zip or ojdbc14.jar and can be found under Oracle installation/jdbc/lib. For example,if Oracle XE is installed in C:\oraclexe,the ojdbc14.jar can be found on the following path. C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib. You can also download it from http://www.minq.se/products/dbvis/drivers.html freely.You can download the MySQL JDBC driver freely from http://dev.mysql.com/downloads/connector/j/5.0.htmll
Java Discussion
- - Difference between BMT an
- - Replace getParameterValue
- - Interviewing Next week -
- - Sudoku solver
- - Setting tab order in swin



