- Must have a Sun JDK, with %JAVA_HOME% set to point to the JDK install directory. Additionally, PATH must include a pointer to %JAVA_HOME%\bin. We call this {java}. Using 1.5.0_04.
- Must have Apache ANT, with %ANT_HOME% set to point to the ANT install directory. Additionally, PATH must include a pointer to %ANT_HOME%\bin. We call this {ant}, my version is 1.6.5.
- antlr-2.7.6rc1.jar
- junit-3.8.1.jar
- antlr-2.7.6rc1.jar
- asm.jar
- asm-attrs.jar
- cglib-2.1.3.jar
- commons-collections-2.1.1.jar
- commons-logging-1.0.4.jar
- dom4j-1.6.1.jar
- hibernate3.jar
- jaas.jar
- jta.jar
- junit-3.8.1.jar
- log4j-1.2.11.jar
Using the Enterprise Manager console, I created my database for use in this project. Let's call it [HIBERNATE]. Since I will connect to it using TCP URLs, you should find out what port mssqlserver listens at; mine is 1433, the default.
At this point, we can open Eclipse (or an IDE of your choice) and create our project. I'll call the project directory {project}. Before coding, we need to reference libraries needed for Hibernate and data access: include all Jars in {archives}\hibernate and {archives}\jdbc in the classpath. Then create the basic directory structure as follows:
- src = where source files and mapping files will exist
- package.JavaClass.java (the Java class)
- JavaClass.hbm.xml (at the same level as the class it will map)
- src\hibernate.cfg.xml (hibernate configuration)
- src\log4j.properties (logging - just copy this from {hibernate}\etc as is)
- lib = where all the libraries referenced above - hibernate and JDBC - are stored. As an alternative, rather than reference the {archive}, you could create a library in Eclipse and include these files therein. You can also include other 3rd party libraries needed by your application.
- build.xml = the ANT build file.
=====
[?xml version='1.0' encoding='utf-8'?]Basically, this configuration file defines the database connection parameters to the SQL database. Later, we will only need to add references to mapping files when we start developing code.
[!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"]
[hibernate-configuration]
[session-factory]
[!-- Database connection settings --]
[property name="connection.driver_class"]net.sourceforge.jtds.jdbc.Driver[/property]
[property name="connection.url"]
jdbc:jtds:sqlserver://localhost:1433/HIBERNATE;tds=8.0;lastupdatecount=true[/property]
[property name="connection.username"]sa[/property]
[property name="connection.password"]password[/property]
[!-- JDBC connection pool (use the built-in) --]
[property name="connection.pool_size"]1[/property]
[!-- SQL dialect --]
[property name="dialect"]org.hibernate.dialect.SQLServerDialect[/property]
[!-- Enable Hibernate's automatic session context management --]
[property name="current_session_context_class"]thread[/property]
[!-- Disable the second-level cache --]
[property name="cache.provider_class"]org.hibernate.cache.NoCacheProvider[/property]
[!-- Echo all executed SQL to stdout --]
[property name="show_sql"]true[/property]
[!-- Drop and re-create the database schema on startup --]
[property name="hbm2ddl.auto"]update[/property]
[/session-factory]
[/hibernate-configuration]
The next file is the ANT build file, which looks like this (replace block quotes with appropriate angle brackets):
[project name="hibernate-project-name" default="compile"]This file will instruct ANT to clean, compile, copy resources, and run the MainClass. At this point, your project is set up to run. I will discuss the *.hbm.xml file and inserting a reference to it in the hibernate.cfg.xml file in code samples that I may post. Otherwise, the Hibernate Tutorial is the go-to resource to start using right away. It explains in a little bit more detail what everything we have done here means.
[property name="sourcedir" value="${basedir}/src"/]
[property name="targetdir" value="${basedir}/build"/]
[property name="librarydir" value="${basedir}/lib"/]
[path id="libraries"]
[fileset dir="${librarydir}"]
[include name="*.jar"/]
[/fileset]
[/path]
[target name="clean"]
[delete dir="${targetdir}"/]
[mkdir dir="${targetdir}"/]
[/target]
[target name="compile" depends="clean, copy-resources"]
[javac srcdir="${sourcedir}"
destdir="${targetdir}"
classpathref="libraries"/]
[/target]
[target name="copy-resources"]
[copy todir="${targetdir}"]
[fileset dir="${sourcedir}"]
[exclude name="**/*.java"/]
[/fileset]
[/copy]
[/target]
[target name="run" depends="compile"]
[java fork="true" classname="package.MainClass" classpathref="libraries"]
[classpath path="${targetdir}"/]
[/java]
[/target]
[/project]