users@glassfish.java.net

hsqldb with GlassFish

From: <glassfish_at_javadesktop.org>
Date: Fri, 02 Oct 2009 11:54:07 PDT

hi all,, i am new user for glassfish + hslqdb + flash !!
 
1) i tried to search for the answer of my problem every were i couldn't find the answer.. so just in case if i am opening a thread that already answered i am sorry

2) i build a project that use glassfish as server and hslqdb as standalone (embedded) database using flash builder 4 beta.

3) *** the PROBLEM i am facing is : when i run my program the insertion finish with no error, and then i read it and the select is finished with no error, but the problem after that if i insert and read again.. it will just read the new data.. the old data disappear, and even when i close the program i try to go to \WEB-INF\lib and run my hslqdb.jar.. the table is still empty !!!


here is my code :

hibernate.cfg.xml

<?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">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
        <!-- jdbc:hsqldb:file:hsqldb/public or jdbc:hsqldb:hsql://localhost/public or jdbc:hsqldb:mem:public -->
        <property name="hibernate.connection.url">jdbc:hsqldb:file:C:\test_prjct\WebContent\WEB-INF\lib\public</property>
          
        <property name="hibernate.connection.username">sa</property>
        <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>

        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>
                <property name="hibernate.connection.provider.class">org.hibernate.connection.DriverManagerConnectionProvider</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">create</property>
        
        <property name="hibernate.connection.shutdown">true</property>
        <property name="connection.shutdown">true</property>
        <property name="hibernate.connection.WRITE_DELAY">0</property>

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


-----------*----------------------*-----------------------------*-----------------------

here is my mapping
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


<hibernate-mapping package="model">
  <class name="Server" table="SERVER">
   <id name="serverID" type="string" column="SERVERID" >
   <generator class="assigned"/>
  </id>

  <property name="serverName">
     <column name="SERVERNAME" />
  </property>
  <property name="serverIP">
    <column name="SERVERIP"/>
  </property>

 </class>
</hibernate-mapping>

-----------*----------------------*-----------------------------*-----------------------

here is my model file called Server.java

package model;

/**
 * @author Suleiman Khader
 *
 * Java Class to map to the datbase server Table
 */
public class Server {
        
  private String serverID;
  private String serverName;
  private String serverIP;
  
  
  
          public String getServerID() {
                return serverID;
        }
        public void setServerID(String serverID) {
                this.serverID = serverID;
        }
        public String getServerName() {
                return serverName;
        }
        public void setServerName(String serverName) {
                this.serverName = serverName;
        }
        public String getServerIP() {
                return serverIP;
        }
        public void setServerIP(String serverIP) {
                this.serverIP = serverIP;
}

  

 

}


-----------*----------------------*-----------------------------*-----------------------


and here is my HibernateUtil.java

package utility;

import org.hibernate.*;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static SessionFactory factory;

    public static synchronized Session getSession() {
        if (factory == null) {
            factory = new Configuration().configure().buildSessionFactory();
        }
        return factory.openSession();
    }

    public static void setSessionFactory(SessionFactory factory) {
        HibernateUtil.factory = factory;
    }
}

-----------*----------------------*-----------------------------*-----------------------

here is my servlet

package model;
import java.io.*;
import java.util.Iterator;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import utility.*;




public class HelloWorld extends HttpServlet implements Servlet{
        
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                                           throws ServletException,IOException{
                
                doPost( request, response);
          }

        public void doPost(HttpServletRequest request, HttpServletResponse response) throws
        ServletException, IOException {
                System.out.println("start 1st");
                //FirstExample fex= new FirstExample();
                Session session = null;

            try{
             
                     System.out.println("start");
                     
                     SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
               session =HibernateUtil.getSession();// i used singelton to have one connection to the database
               
               Transaction transaction = session.beginTransaction();
               
               //Create new instance of Contact and set values in it by reading them from form object
                System.out.println("Inserting Record");
                System.out.println("Inserting Record");
                System.out.println("Inserting Record");
                Server server = new Server();
                server.setServerID("123test");
                server.setServerIP("ip92929");
                server.setServerName("suleiman");
                System.out.println(server.getServerID() +" "+server.getServerIP()+" "+server.getServerName());
               session.save(server);
               transaction.commit();
               if (session.isOpen())
                      {
                              session.flush();
                              session.close();
                      }
               session =HibernateUtil.getSession();
               String SQL_QUERY ="Select s.serverID,s.serverIP,s.serverName from Server as s";
                       Query query = session.createQuery(SQL_QUERY);
                        for(Iterator it=query.iterate();it.hasNext();){
                              Object[] row = (Object[]) it.next();
                              System.out.println("ID: " + row[0]);
                              System.out.println("IP: " + row[1]);
                              System.out.println("Name: " + row[2]);
                            }
               
               
               System.out.println(server.getServerID() +" "+server.getServerIP()+" "+server.getServerName());
                System.out.println("Done.....");
                if (session.isOpen())
                      {
                              session.flush();
                              session.close();
                      }
            }catch(Exception e){
                    
              System.out.println(e.getMessage());
              System.out.println("ERROR: failed to load HSQLDB JDBC driver.");
                e.printStackTrace();
              
            }finally{
              // Actual contact insertion will happen at this step
                    System.out.println("IN FINALLY !!!");
              
              if (session.isOpen())
                      {
                              session.flush();
                              session.close();
                      }
              

              }
           
        
        
        }

          
}



please till me were is my mistake... i have store my hsqldb.jar in the \WebContent\WEB-INF\lib directory !!
[Message sent by forum member 'suleimankh' (suleiman_khader_at_hotmail.com)]

http://forums.java.net/jive/thread.jspa?messageID=366599