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.
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
);
With this example you can will are shown using int, String, and long datatypes in Hibernate. In this particular application, the architects choose to use the Date.getTime() functionality to store date values.
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.