Setup Hibernate in a Swing Application
Hibernate works very well in Swing Applications. This page is intended to help you get started in implementing Hibernate with Swing. This particular example is set up in a database environment using Oracle 10g. However, this setup will work perfectly in other database enviroments by simply modifying the database connection information.
1. Download the latest Version
Hibernate can be found http://www.hibernate.org/6.html. After you download it, make certain you not only place hibernate.jar in you classpath, you may want to include all of the jar files that are found in the lib directory of the extracted files. While many may not be used in your application, some of them are likely to result in runtime errors if not included in the classpath.
2. Create the hibernate.cfg.xml file.
This is the primary configuration file that should be placed at the root of your source code.
<?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>
<property name=“hibernate.connection.driver_class”>oracle.jdbc.driver.OracleDriver</property>
<property name=“hibernate.connection.url”>jdbc:oracle:thin:@servername.domainname.com:1521:dbname</property>
<property name=“hibernate.connection.username”>username</property>
<property name=“hibernate.connection.password”>password</property>
<property name=“hibernate.connection.pool_size”>10</property>
<property name=“show_sql”>true</property>
<property name=“dialect”>org.hibernate.dialect.Oracle10gDialect</property>
<property name=“connection.autocommit”>true</property>
<!– Mapping files –>
<mapping resource=“Entry.hbm.xml”/>
</session-factory>
</hibernate-configuration>
You may need to modify the dialect to match your current version of Oracle. Other available dialects for the Oracle database include OracleDialect (deprecated), Oracle8iDialect, and Oracle9iDialect. It is also important that you set the autocommit property to true if you are not using exlicit transactions in your application. Otherwise, you may get frustrated as you try to look at the database and see that your changes have not been commited to the database.
3. Create a class to match a given database table.
This example called Entry.java matches a database table called Entry.
package com.utilities.data;
public class Entry
{
private int entryID;
private int userID;
private String title = “”;
private long creationDate;
public Entry(){ }
public void setEntryID(int entryID) { this.entryID=entryID; }
public void setUserID(int userID) { this.userID=userID; }
public void setTitle(String title) { this.title=title; }
public void setCreationDate(long creationDate) { this.creationDate = creationDate; }
public int getEntryID() { return this.entryID; }
public int getUserID() { return this.userID; }
public String getTitle() { return this.title; }
public long getCreationDate() { return this.creationDate; }
}
The corresponding table creation and sequence creation commands are given here as well.
CREATE SEQUENCE Entry_seq MINVALUE 1 START WITH 1 INCREMENT BY 1;
CREATE TABLE ENTRY
(
EntryID Number Primary Key,
UserID Number Not Null,
Title Varchar2(255),
CreationDate Number
);
4. Create the Mapping Resource File.
This particular file is called Entry.hbm.xml. It is stored in the root directory right next to the hibernate.cfg.xml file and is used to bridge the java object values to the database. In many applications, it is prefered to place this file right next to Entry.java. However, it appears that in Swing applications, Hibernate has difficulty finding it unless it is in the root directory.
<?xml version=“1.0″ encoding=“UTF-8″?>
<!DOCTYPE hibernate-mapping PUBLIC
“-//Hibernate/Hibernate Mapping DTD 3.0//EN”
“http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>
<hibernate-mapping package=“com.utilities.data”>
<class name=“Entry” table=“Entry”>
<id name=“entryID” column=“EntryID” type=“int”>
<generator class=“sequence”>
<param name=“sequence”>Entry_seq</param>
</generator>
</id>
<property name=“userID” type=“int” />
<property name=“title” type=“string” length=“255″ />
<property name=“creationDate” type=“long” />
</class>
</hibernate-mapping>
5. Populate the class called Entry as you would any other java class.
6. Save the data to the database
From within your DAO class make the following calls. Please pay attention to the following line:
configuration = new Configuration().configure(”/hibernate.cfg.xml”);
In some applications, it is enough to simply call configuration = new Configuration()
Unfortuneately, my swing application required the additional configure method to be called in order to find the configuration file.
package com.utilities.data;
import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
public class ImportToolDAO
{
private Configuration configuration;
private SessionFactory factory;
private static ImportToolDAO importToolDAO;
private ImportToolDAO()
{
configuration = new Configuration().configure(“/hibernate.cfg.xml”);
factory = configuration.buildSessionFactory();
}
public static ImportToolDAO getInstance()
{
if(importToolDAO==null)
{
importToolDAO = new ImportToolDAO();
}
return importToolDAO;
}
public void saveEntry(Entry entry)
{
Session session = factory.openSession();
session.save(entry);
session.flush();
session.close();
}
}
7. Run the Application. If you followed everything step by step, it just might work for you the first time. Go check your database and be prepared for a smile.