Friday 26 September 2008

Hibernate short summary part-I, page 2

Read the Article page-I here

The next step is to define the hibernate framework configuration file. This is a xml file where we essentially define the 2 things

1) Database details : which database we are going to use, whats are the connection urls, username password and all the database related configurations.

2) We tell the hibernate which are the mapping files. framework uses these mapping files to identify the mapping between the classes and the underlying database tables.

Let us name this configuration file as hibernate.cfg.xml
-----------------------------------------------------------------------
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/
hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>

<!-- Database connection settings -->
<property name="hibernate.dialect">
org.hibernate.dialect.OracleDialect
</property>
<property name="hibernate.connection.driver_class">
oracle.jdbc.OracleDriver
</property>
<property name="hibernate.connection.url">
jdbc:oracle:thin:@<host_name>:<port_number>:<database_name>
</property>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.password">tiger</property>


<!-- Mapping files -->
<mapping resource="com/entities/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
------------------------------------------------------------------------------

The properties are explained below ...

hibernate.dialect :
This is the database specific attribute , hibernate identifies using this dialect how it will translate the queries... some of the example dialects are

Database SQL Dialect
SQL Server org.hibernate.dialect.SQLServerDialect
MySQL org.hibernate.dialect.MySQLDialect
Oracle org.hibernate.dialect.OracleDialect

hibernate.connection.driver_class : Identifies the JDBC driver class
hibernate.connection.url : Identifies the JDBC connection URL and the next 2 attributes are the database usernames and passwords.

< mapping-resource="com/entities/User.hbm.xml"/>> : tells the hibernate to load the mapping definition file, did you note the path given here is com/entities ?
because this where the mapping xml file is w.r.t to classpath context.

Now the next question is where we would keep the hibernate.cfg.xml ?
The essential answer is : it should be in the application classpath. you need to load this file while application initialization. so you should be aware of how to load this xml file.

So we are done with all the efforts. Lets summarize ..
we defined a table, a class , a mapping for class and table and hibernate configuration ...

The next step is to use the hibernate in our application to store and retrieve the Users. right?
We will write some sample program that would do these things ...

1 comments:

Anonymous said...

It's perfect time to make some plans for the longer term and it's time to be happy.
Ӏ've read this publish and if I may I wish to counsel you some fascinating issues or tips. Perhaps you can write subsequent articles regarding this article. I desire to read even more issues approximately it!

my web blog :: http://www.sfgate.com
Also see my web site :: lawngreen.w3bookmarks.com