View RSS Feed

My Java Tips

Setting up hibernate.cfg.xml

Rate this Entry
by , 10-30-2011 at 05:50 PM (20944 Views)
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'?-->
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
<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.Driv er</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.password">password</property>
<property name="transaction.factory_class">org.hibernate.tra nsaction.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">

</mapping></session-factory>
</hibernate-configuration></pre>


Hibernate.cfg.xml for Postgres is given below:

<!--?xml version='1.0' encoding='UTF-8'?-->
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
<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.Driv er</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDia lect</property>
<property name="connection.password">password</property>
<property name="transaction.factory_class">org.hibernate.tra nsaction.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">
</mapping></session-factory>
</hibernate-configuration>

Submit "Setting up hibernate.cfg.xml" to Facebook Submit "Setting up hibernate.cfg.xml" to Digg Submit "Setting up hibernate.cfg.xml" to del.icio.us Submit "Setting up hibernate.cfg.xml" to StumbleUpon Submit "Setting up hibernate.cfg.xml" to Google

Tags: None Add / Edit Tags
Categories
Hibernate

Comments