-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate 4.1.9 & HSQLDB 2.2.9
PostPosted: Thu Feb 14, 2013 10:44 pm 
Newbie

Joined: Sat Aug 11, 2012 5:37 pm
Posts: 5
I am having problems getting the latest versions of Hibernate & HSQLDB to work and I'm hoping someone can shed some light on what is wrong. I am just trying to get a simple example to work. The example comes from: http://www.java4s.com/hibernate/hibernate-hello-world-program-saving-an-objcet/ and is working with older versions of both. I am using NetBeans 7.0.1 on a Ubuntu computer.

[Edit] - I tried the H2 database also and that doesn't work either - so there is probably sometihng wrong with my Hibernate logic.

My current problem is hibernate save is not writing a record to the database.

I include a few files below.

hibernate.cfg.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
    <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
    <property name="hibernate.connection.url">jdbc:hsqldb:/home/jim2/myfiles/testdb3/myHSQLDB</property>
    <property name="hibernate.connection.username">SA</property>
    <property name="hibernate.connection.password"/>

    <property name="show_sql">true</property>
   
    <property name="hibernate.current_session_context_class">thread</property>

    <!-- HBM file -to- DDL -->
    <!-- <property name="hbm2ddl.auto">update</property> -->
   

    <mapping resource="main/Product.hbm.xml"/>
  </session-factory>
</hibernate-configuration>


Main.java:
Code:
/*
* NetBeans Project ProductHibernateTest3
*
* HSQLDB engine version is 2.2.9; current version as of August 2012. 
* The current Hibernate version released in Dec 2012 is 4.1.9.
*
* This very basic test is based on code from:
* http://www.java4s.com/hibernate/hibernate-hello-world-program-saving-an-objcet/
*
*
*/
package main;

import java.util.Properties;
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
*
* @author Jim
*/
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        // Use Hibernate SchemaExport to establish the database
        Configuration cfg = new Configuration();
        cfg.addAnnotatedClass(Product.class); 
        cfg.configure("hibernate.cfg.xml");
        new SchemaExport(cfg).create(true, true);

       
       
        // needed for Hibernate 4.1.9 to setup the session factory
        ServiceRegistry sReg;
        Properties properties = cfg.getProperties();
        sReg = new ServiceRegistryBuilder().applySettings(properties).buildServiceRegistry();       
        SessionFactory factory = cfg.buildSessionFactory(sReg);
       
        //Now use the session & trasaction
        Session session = factory.openSession();
        Transaction tx = session.beginTransaction();

        Product p=new Product();

        p.setProductId(102);
        p.setProName("GMC Yukon");
        p.setPrice(45000);

        session.save(p);
       
        System.out.println("Object saved successfully.....!!");
        tx.commit();
       
        System.out.println("Commited = " + tx.wasCommitted());
        session.close();
        //factory.close();   
   
    }
}


Product.java - the single Entity to be saved in the database
Code:
/*
* Product
*/
package main;

/**
* CLASS: Product
* @author Jim
*/
public class Product {
    private int productId;
    private String proName;
    private double price;

    public Product() {
    }

    public void setProductId(int productId)
    {
        this.productId = productId;
    }
    public int getProductId()
    {
        return productId;
    }

    public void setProName(String proName)
    {
        this.proName = proName;
    }
    public String getProName()
    {
        return proName;
    }

    public void setPrice(double price)
    {
        this.price = price;
    }
    public double getPrice()
    {
        return price;
    }   
}


Product.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="main" schema="PUBLIC">
    <!-- including 'package' in mapping element Specifies a package prefix to
    assume for unqualified class names in the mapping document.  Without the 'package'
    attribute, the class name needs the full package path (dotted syntax) -->
    <class name="Product" table="PRODUCTS">

        <id name="productId" column="pid"  >
            <generator class="assigned" />
        </id>

        <property name="proName" column="pname" />
        <property name="price"/>
        <!-- omitting the column= means the Table column name is the same as the
        name attribute. -->
    </class>
</hibernate-mapping>


I changed the original example code for Hibernate 4.1.9 and added use of SchemaExport() to create the database and Table. Not using SchemaExport() I could not get the database to create and be retained. hmb2dll.auto would never allow the table to be retained. But as I mentioned at this time I can't seem to get data written to the database.

myHSQLDB.script after running: (Table created, but not rows in the table):
Code:
SET DATABASE UNIQUE NAME HSQLDB3CDADB7B78
SET DATABASE GC 0
SET DATABASE DEFAULT RESULT MEMORY ROWS 0
SET DATABASE EVENT LOG LEVEL 0
SET DATABASE SQL NAMES FALSE
SET DATABASE SQL REGULAR NAMES TRUE
SET DATABASE SQL REFERENCES FALSE
SET DATABASE SQL SIZE TRUE
SET DATABASE SQL TYPES FALSE
SET DATABASE SQL TDC DELETE TRUE
SET DATABASE SQL TDC UPDATE TRUE
SET DATABASE SQL TRANSLATE TTI TYPES TRUE
SET DATABASE SQL CONCAT NULLS TRUE
SET DATABASE SQL NULLS FIRST TRUE
SET DATABASE SQL UNIQUE NULLS TRUE
SET DATABASE SQL CONVERT TRUNCATE TRUE
SET DATABASE SQL AVG SCALE 0
SET DATABASE SQL DOUBLE NAN TRUE
SET DATABASE SQL LONGVAR IS LOB FALSE
SET DATABASE TRANSACTION CONTROL LOCKS
SET DATABASE DEFAULT ISOLATION LEVEL READ COMMITTED
SET DATABASE TRANSACTION ROLLBACK ON CONFLICT TRUE
SET DATABASE TEXT TABLE DEFAULTS ''
SET FILES WRITE DELAY 500 MILLIS
SET FILES BACKUP INCREMENT TRUE
SET FILES CACHE SIZE 10000
SET FILES CACHE ROWS 50000
SET FILES SCALE 32
SET FILES LOB SCALE 32
SET FILES DEFRAG 0
SET FILES NIO TRUE
SET FILES NIO SIZE 256
SET FILES LOG TRUE
SET FILES LOG SIZE 50
CREATE USER SA PASSWORD DIGEST 'd41d8cd98f00b204e9800998ecf8427e'
ALTER USER SA SET LOCAL TRUE
CREATE SCHEMA PUBLIC AUTHORIZATION DBA
SET SCHEMA PUBLIC
CREATE MEMORY TABLE PUBLIC.PRODUCTS(PID INTEGER NOT NULL PRIMARY KEY,PNAME VARCHAR(255),PRICE DOUBLE)
ALTER SEQUENCE SYSTEM_LOBS.LOB_ID RESTART WITH 1
SET DATABASE DEFAULT INITIAL SCHEMA PUBLIC
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.YES_OR_NO TO PUBLIC
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.TIME_STAMP TO PUBLIC
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.CARDINAL_NUMBER TO PUBLIC
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.CHARACTER_DATA TO PUBLIC
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.SQL_IDENTIFIER TO PUBLIC
GRANT DBA TO SA
SET SCHEMA SYSTEM_LOBS
INSERT INTO BLOCKS VALUES(0,2147483647,0)


Example using earlier versions of Hibernate & HSQLDB adds the following 2 lines to the myHSQLDB.script file (showing the data record is saved in the database - that works!). So why can't the current versions of Hibernate and HSQLDB work?

Code:
SET SCHEMA PUBLIC
INSERT INTO PRODUCTS VALUES(101,'iPhone',25000.0E0)


[Edit] Here is the console output of running this code with HSQLDB:
Code:
run:
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

    drop table PUBLIC.PRODUCTS if exists

    create table PUBLIC.PRODUCTS (
        pid integer not null,
        pname varchar(255),
        price double,
        primary key (pid)
    )
Object saved successfully.....!!
Hibernate: insert into PUBLIC.PRODUCTS (pname, price, pid) values (?, ?, ?)
Commited = true
BUILD SUCCESSFUL (total time: 1 second)


Any ideas on how to fix this or what I can do to diagnose what is going on will be most appriciated.

Thanks,
Jim


Top
 Profile  
 
 Post subject: Re: Hibernate 4.1.9 & HSQLDB 2.2.9
PostPosted: Sat Feb 16, 2013 3:51 pm 
Newbie

Joined: Sat Aug 11, 2012 5:37 pm
Posts: 5
I found a solution to my problem. The problem was related to HSQLDB, not so much Hibernate.

Adding ;hsqldb.write_delay=false to the hibernate connection URL property helped considerably. I also added ;shutdown=true to the same property, but I suspect that doesn't do what it's advertized to do. So my URL property was changed to:

Code:
cfg.setProperty("hibernate.connection.url", "jdbc:hsqldb:file:/data/tests/testdb3/myHSQLDB;shutdown=true;hsqldb.write_delay=false;");


Unrelated - I moved the database file location. It was being accessed through a symbolic link and I thouhgt that might be contributing to my woes, but that does not appear to be a contributor.

Then I added the following to the end, just prior to closing the factory.

Code:
// open new session and shutdown the database
// this results in the hsqldb script file complete with the insert row
session = factory.openSession();
session.createSQLQuery( "SHUTDOWN" ).executeUpdate();
session.close();


With these additions all is working now.

Thanks - this problem took miltiple days of my time to resolve.
Jim


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.