Hibernate configuration

Skip to end of metadata
Go to start of metadata

Here's a resulting config for a Hibernate w/ Tomcat & MySQL setup. This certainly doesn't replace their documentation (and might be crap) but perhaps it will be helpful or save you some time.

install:

Create a Tomcat 5.5 context descriptor in $TOMCAT_HOME/conf/Catalina/localhost.

Tomcat configuration
<Context path="/yourProjectName"
	docBase="/path/to/yourProjectName" debug="5"
	reloadable="true" crossContext="true">

	<Logger className="org.apache.catalina.logger.FileLogger"
		prefix="localhost_yourProjectName_log." suffix=".txt" timestamp="true" />

	<Resource name="jdbc/yourDatabaseName"
	          auth="Container"
	          type="javax.sql.DataSource"
	          factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
	          maxActive="10"
	          maxIdle="5"
	          validationQuery="select 1"
	          testOnBorrw="true"
	          testWhileIdle="true"
	          timeBetweenEvictionRunsMillis="10000"
	          minEvicatableIdleTimeMillis="60000"
	          removeAbandoned="true"
	          removeAbandonedTimeout="60"
	          logAbandoned="true"
	          username="yourUserName"
	          password="yourPassword"
	          driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://localhost:3306/yourDatabaseName"
	/>

</Context>

You'll need to edit the example above in 8 places.

  1. yourProjectName (twice)
  2. Set the docBase to point to the directory where your exploded WAR is built.
  3. yourDatabaseName (twice)
  4. yourUserName
  5. yourPassword
  6. yourDatabaseName again

I'm using maven2 to build the project itself, but Hibernate uses ant to generate its schema and models and such. This is what its build.xml (located in the project root directory) looks like. You'll need to customize--I didn't remove other references.

build.xml
<project default="generate-ddl">
	<property file="build.properties" />

	<path id="toolslib">
		<path location="${maven-repository.dir}/commons-collections/commons-collections/2.1.1/commons-collections-2.1.1.jar" />
		<path location="${maven-repository.dir}/commons-logging/commons-logging/1.0.4/commons-logging-1.0.4.jar" />
		<path location="${maven-repository.dir}/log4j/log4j/1.2.9/log4j-1.2.9.jar" />
		<path location="${maven-repository.dir}/dom4j/dom4j/1.6.1/dom4j-1.6.1.jar" />
		<path location="${maven-repository.dir}/javax/persistence/ejb/3.0-public-draft-20060502/ejb-3.0-public-draft-20060502.jar" />
		<path location="hbm-libs/hibernate-tools.jar" />
		<path location="${maven-repository.dir}/org/hibernate/hibernate/3.1.3/hibernate-3.1.3.jar" />
		<path location="${maven-repository.dir}/org/hibernate/hibernate-annotations/3.2.0.cr1/hibernate-annotations-3.2.0.cr1.jar" />
		<path location="hbm-libs/freemarker.jar" />
		<path location="config/mysql-connector-java-3.1.13-bin.jar" />
		<path location="target/classes" />
	</path>

	<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="toolslib" />

	<target name="generate-ddl">
		<mkdir dir="target/generated" />
		<hibernatetool destdir="target/generated">
			<annotationconfiguration configurationfile="src/main/resources/hibernate.cfg.xml" />
			<hbm2ddl export="false" outputfilename="sql.ddl" />
		</hibernatetool>
	</target>
</project>

And Hibernate's config file hibernate.cfg.xml, located in src/main/resources/:

hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
        <!-- properties -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="connection.datasource">java:comp/env/jdbc/yourDatabase</property>
        <property name="show_sql">true</property>
        <property name="use_outer_join">true</property>
        <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
        <property name="current_session_context_class">thread</property>


<!-- mappings -->
 	<mapping class="com.yourproj.model.User" />

-- snip --

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

Two replacements:

  1. yourDatabase
  2. yourproj and all the mappings

feels like I'm missing something...

Then run ant.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.