Setting up hibernate.cfg.xml

3 August 2008

Hibernate.cfg.xml is a configuration file that is put in the project’s root directory and it has to be put into class path as well.

A sample cfg file for MySQL is given below. Do change the database URL, database name, username and password.

<?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:mysql://localhost/dbname</property>
	<property name="connection.username">root</property>
	<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
	<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
	<property name="connection.password">password</property>
 <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
    <!--  thread is the short name for
      org.hibernate.context.ThreadLocalSessionContext
      and let Hibernate bind the session automatically to the thread
    -->
    <property name="current_session_context_class">thread</property>
    <!-- this will show us all sql statements -->
    <property name="hibernate.show_sql">true</property>
 
	<!-- mapping files -->
	<mapping resource="com/domain/example/Entity.hbm.xml" />
 
</session-factory>
</hibernate-configuration>

Hibernate.cfg.xml for Postgres is given below:

<?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:postgresql://localhost/dbname</property>
	<property name="connection.username">root</property>
	<property name="connection.driver_class">org.postgresql.Driver</property>
	<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
	<property name="connection.password">password</property>
 <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
    <!--  thread is the short name for
      org.hibernate.context.ThreadLocalSessionContext
      and let Hibernate bind the session automatically to the thread
    -->
    <property name="current_session_context_class">thread</property>
    <!-- this will show us all sql statements -->
    <property name="hibernate.show_sql">true</property>
	<!-- mapping files -->
	<mapping resource="com/domain/test/Entity.hbm.xml" />
</session-factory>
</hibernate-configuration>

del.icio.us:Setting up hibernate.cfg.xml  digg:Setting up hibernate.cfg.xml  spurl:Setting up hibernate.cfg.xml  wists:Setting up hibernate.cfg.xml  simpy:Setting up hibernate.cfg.xml  newsvine:Setting up hibernate.cfg.xml  blinklist:Setting up hibernate.cfg.xml  furl:Setting up hibernate.cfg.xml  reddit:Setting up hibernate.cfg.xml  fark:Setting up hibernate.cfg.xml  blogmarks:Setting up hibernate.cfg.xml  Y!:Setting up hibernate.cfg.xml  smarking:Setting up hibernate.cfg.xml  magnolia:Setting up hibernate.cfg.xml  segnalo:Setting up hibernate.cfg.xml  gifttagging:Setting up hibernate.cfg.xml

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Setting up hibernate.cfg.xml

Leave a Reply