I am using some sample apps I downloaded. The code compiles and runs without any exceptions, but no data is saved. I am using:
Windows XP
Eclipse 3.2
Hibernate 3.1.1
HSQLDB 1.8
The java code
=========
Code:
package actions;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import tables.Product;
public class Driver1 {
private static Logger log =Logger.getLogger(Driver1.class);
public static void main(String[] args)
throws Exception
{
Session session = null;
Transaction tx = null;
// 1. Build a Product
Product p = new Product();
p.setId(10);
p.setName("Widgets");
p.setAmount(100);
p.setPrice(10.10);
session = HibernateUtil.currentSession();
tx = session.beginTransaction();
try
{
session.save(p);
tx.commit();
session.flush();
HibernateUtil.closeSession();
}
catch (HibernateException e) {
e.printStackTrace();
if (tx != null && tx.isActive())
tx.rollback();
}
//
// Now look for the Products just inserted
String query = "select p from Product as p";
session = HibernateUtil.currentSession();
tx = null;
tx = session.beginTransaction();
// search and return
List list = session.createQuery(query).list();
if (list.size() == 0)
{System.out.println("No products found ");}
for (Iterator iter = list.iterator(); iter.hasNext();)
{
Product p2 = (Product) iter.next();
System.out.println("Product ID = " + p2.getId());
System.out.println("Name = " + p2.getName());
System.out.println("Price = " + p2.getPrice());
System.out.println("Amount = " + p2.getAmount());
System.out.println(" ");
}
tx.commit();
session.flush();
HibernateUtil.closeSession();
}
}
This is HibernateUtil
=============
package actions;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
public static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration config = new Configuration().configure();
sessionFactory = config.buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this thread has none yet
if (s == null) {
s = sessionFactory.openSession();
// Store it in the ThreadLocal variable
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
if (s != null)
s.close();
session.set(null);
}
}
The Hibernate xml config
================
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!-- HSQL connection -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="show_sql">true</property>
<property name="use_outer_join">false</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property>
<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
<!-- use the 'file:' format to point to a database not in the project -->
<!-- added shutdown=true from comment in blog -->
<!-- is supposed to ix bug that prevents data saving to DB -->
<property name="hibernate.connection.url">jdbc:hsqldb:file:c:\temp\productsDB\products_t</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">2</property>
<property name="transaction.factory_class">org.hibernate.transaction.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>
<!-- this will create the database tables for us -->
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="tables/Order.hbm.xml" />
<mapping resource="tables/Product.hbm.xml" />
<mapping resource="tables/OrderItem.hbm.xml" />
</session-factory>
</hibernate-configuration>
The HSQLDB Properties
===============
#HSQL Database Engine
#Mon Jul 24 20:31:29 CDT 2006
hsqldb.script_format=0
runtime.gc_interval=0
sql.enforce_strict_size=false
hsqldb.cache_size_scale=8
readonly=false
hsqldb.nio_data_file=true
hsqldb.cache_scale=14
version=1.8.0
hsqldb.default_table_type=memory
hsqldb.cache_file_scale=1
hsqldb.log_size=200
modified=yes
hsqldb.cache_version=1.7.0
hsqldb.original_version=1.8.0
hsqldb.compatible_version=1.8.0
The HSQLBD script file
===============
CREATE SCHEMA PUBLIC AUTHORIZATION DBA
CREATE MEMORY TABLE ORDERS(ID INTEGER NOT NULL PRIMARY KEY,ORDER_DATE TIMESTAMP NOT NULL,PRICE_TOTAL DOUBLE NOT NULL)
CREATE MEMORY TABLE PRODUCTS(ID INTEGER NOT NULL PRIMARY KEY,NAME VARCHAR NOT NULL,PRICE DOUBLE NOT NULL,AMOUNT INTEGER NOT NULL)
CREATE MEMORY TABLE ORDER_ITEMS(ID INTEGER NOT NULL PRIMARY KEY,ORDER_ID INTEGER NOT NULL,PRODUCT_ID INTEGER NOT NULL,AMOUNT INTEGER NOT NULL,PRICE DOUBLE NOT NULL)
CREATE USER SA PASSWORD ""
GRANT DBA TO SA
SET WRITE_DELAY 20
????
So what am I doing wrong?
Thanks,
Rich Morrison