facebook

session.save() does not save to database

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #234178 Reply

    mm2ha
    Member

    Hi, I am creating a webapp, using myeclipse ide, hibernate 3.0(with hsqldb database) and struts 1.2. I am trying to do a test for the database settings, so that I can be sure that it will work later. However, when I do session.save, it does not save into the database permanently(what I want it to do), but it is there only until the end of the java program(when I list the things in database during one run, it shows it, but then when I check the database, it is not there). But when I debug that program, it will add it to the database, but not everytime either, its really weird. What can be wrong? the codes follow

    SQL for of the table:

    TABLE users (
    user_id int IDENTITY,
    meno varchar(30) not null,
    heslo varchar(30) not null,
    admin boolean default false,
    isloggedin boolean default false,
    unique(meno), unique(user_id),
    primary key(user_id)
    )

    code for the actual java application which is being run:

    import java.util.Iterator;
    
    import hibernate.HibernateSessionFactory;
    import hibernate.Users;
    
    
    import org.hibernate.HibernateException;
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    
    public class UsersTest {
        
        private Session session;
        private Transaction tx;
        
        private void setSession(Session session){
            this.session = session;
        }
        
        private Session getSession(){
            return this.session;
        }
        
        private void createUser(String meno, String heslo){
            System.out.println("Creating user");
            try{
                this.session = HibernateSessionFactory.currentSession();
                tx = session.beginTransaction();
                Users user = new Users();
                user.setMeno(meno);
                user.setHeslo(heslo);
                System.out.println("Created user: " + user.getMeno());
                session.save(user);
                tx.commit();
                session.close();
            } catch (HibernateException e){
                e.printStackTrace();
                if (tx != null)
                    try {
                        tx.rollback();
                    } catch (HibernateException e1) {
                        System.out.println("rollback not successful");
                    }
                    
                    if (session != null)
                        try {
                            session.close();
                        } catch (HibernateException e2) {
                            System.out.println("session close not successful");
                        }
            }
        }
        
        private void listUsers(){
            System.out.println("listing users");
            this.session = HibernateSessionFactory.currentSession();
            Query query;
            try{
                tx = session.beginTransaction();
                query = session.createQuery("select u from Users as u");
                for (Iterator iter = query.iterate(); iter.hasNext();){
                    Users user = (Users) iter.next();
                    System.out.println("Meno: " + user.getMeno() + ", heslo: " + user.getHeslo());
                }
                tx.commit();
                session.close();
            }catch (HibernateException e){
                e.printStackTrace();
                if (tx != null)
                    try {
                        tx.rollback();
                    } catch (HibernateException e1) {
                        System.out.println("rollback not successful");
                    }
                    
                    if (session != null)
                        try {
                            session.close();
                        } catch (HibernateException e2) {
                            System.out.println("session close not successful");
                        }
            }
        }
        
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            UsersTest test = new UsersTest();
            test.createUser("adrian","ado");
            test.listUsers();
            
        }
        
    }
    

    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">
    
    <!-- Generated by MyEclipse Hibernate Tools.                   -->
    <hibernate-configuration>
    
    <session-factory>
        <property name="myeclipse.connection.profile">datalock</property>
        <property name="connection.url">
            jdbc:hsqldb:c:\hsqldb\databases\datalock
        </property>
        <property name="connection.username">sa</property>
        <property name="connection.password"></property>
        <property name="connection.driver_class">
            org.hsqldb.jdbcDriver
        </property>
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
        <mapping resource="hibernate/Users.hbm.xml" />
    
    
    </session-factory>
    
    </hibernate-configuration>

    Users.hbm.xml:

    <?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" >
    
    <!-- DO NOT EDIT: This is a generated file that is synchronized -->
    <!-- by MyEclipse Hibernate tool integration.                   -->
    <!-- Created Wed Aug 03 17:12:49 CEST 2005                         -->
    <hibernate-mapping package="hibernate">
    
        <class name="Users" table="USERS">
            <id name="userId" column="USER_ID" type="java.lang.Integer">
                <generator class="native"/>
            </id>
     
            <property name="meno" column="MENO" type="java.lang.String"  not-null="true" />
            <property name="heslo" column="HESLO" type="java.lang.String"  not-null="true" />
            <property name="admin" column="ADMIN" type="java.lang.Boolean" />
            <property name="isloggedin" column="ISLOGGEDIN" type="java.lang.Boolean" />
        </class>
        
    </hibernate-mapping>
    

    and Users.java created by myeclipse:

    /*
     * WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized
     * by MyEclipse Hibernate tool integration.
     *
     * Created Wed Aug 03 17:12:50 CEST 2005 by MyEclipse Hibernate Tool.
     */
    package hibernate;
    
    import java.io.Serializable;
    
    /**
     * A class that represents a row in the USERS table. 
     * You can customize the behavior of this class by editing the class, {@link Users()}.
     * WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized * by MyEclipse Hibernate tool integration.
     */
    public abstract class AbstractUsers 
        implements Serializable
    {
        /** The cached hash code value for this instance.  Settting to 0 triggers re-calculation. */
        private int hashValue = 0;
    
        /** The composite primary key value. */
        private java.lang.Integer userId;
    
        /** The value of the simple meno property. */
        private java.lang.String meno;
    
        /** The value of the simple heslo property. */
        private java.lang.String heslo;
    
        /** The value of the simple admin property. */
        private java.lang.Boolean admin;
    
        /** The value of the simple isloggedin property. */
        private java.lang.Boolean isloggedin;
    
        /**
         * Simple constructor of AbstractUsers instances.
         */
        public AbstractUsers()
        {
        }
    
        /**
         * Constructor of AbstractUsers instances given a simple primary key.
         * @param userId
         */
        public AbstractUsers(java.lang.Integer userId)
        {
            this.setUserId(userId);
        }
    
        /**
         * Return the simple primary key value that identifies this object.
         * @return java.lang.Integer
         */
        public java.lang.Integer getUserId()
        {
            return userId;
        }
    
        /**
         * Set the simple primary key value that identifies this object.
         * @param userId
         */
        public void setUserId(java.lang.Integer userId)
        {
            this.hashValue = 0;
            this.userId = userId;
        }
    
        /**
         * Return the value of the MENO column.
         * @return java.lang.String
         */
        public java.lang.String getMeno()
        {
            return this.meno;
        }
    
        /**
         * Set the value of the MENO column.
         * @param meno
         */
        public void setMeno(java.lang.String meno)
        {
            this.meno = meno;
        }
    
        /**
         * Return the value of the HESLO column.
         * @return java.lang.String
         */
        public java.lang.String getHeslo()
        {
            return this.heslo;
        }
    
        /**
         * Set the value of the HESLO column.
         * @param heslo
         */
        public void setHeslo(java.lang.String heslo)
        {
            this.heslo = heslo;
        }
    
        /**
         * Return the value of the ADMIN column.
         * @return java.lang.Boolean
         */
        public java.lang.Boolean isAdmin()
        {
            return this.admin;
        }
    
        /**
         * Set the value of the ADMIN column.
         * @param admin
         */
        public void setAdmin(java.lang.Boolean admin)
        {
            this.admin = admin;
        }
    
        /**
         * Return the value of the ISLOGGEDIN column.
         * @return java.lang.Boolean
         */
        public java.lang.Boolean isIsloggedin()
        {
            return this.isloggedin;
        }
    
        /**
         * Set the value of the ISLOGGEDIN column.
         * @param isloggedin
         */
        public void setIsloggedin(java.lang.Boolean isloggedin)
        {
            this.isloggedin = isloggedin;
        }
    
        /**
         * Implementation of the equals comparison on the basis of equality of the primary key values.
         * @param rhs
         * @return boolean
         */
        public boolean equals(Object rhs)
        {
            if (rhs == null)
                return false;
            if (! (rhs instanceof Users))
                return false;
            Users that = (Users) rhs;
            if (this.getUserId() == null || that.getUserId() == null)
                return false;
            return (this.getUserId().equals(that.getUserId()));
        }
    
        /**
         * Implementation of the hashCode method conforming to the Bloch pattern with
         * the exception of array properties (these are very unlikely primary key types).
         * @return int
         */
        public int hashCode()
        {
            if (this.hashValue == 0)
            {
                int result = 17;
                int userIdValue = this.getUserId() == null ? 0 : this.getUserId().hashCode();
                result = result * 37 + userIdValue;
                this.hashValue = result;
            }
            return this.hashValue;
        }
    }
    
    #234218 Reply

    Riyad Kalla
    Member

    HSQL is an in-memory DB, unless you persist it to disk, it will loose it’s contents when your program exists.

    #234222 Reply

    mm2ha
    Member

    Hsqldb can be also a persistent database, if you specify a location where it should be(example:jdbc:hsqldb:c:\hsqldb\databases\datalock), then it is there and you can retrieve it. I have no problem inserting into that database when I am in the database explorer and it holds the values also after I close eclipse. The problem is that the program saves it into the DB only temporarily, or I dont know what it does. But when I have debugged it and stepped thorough it, it added the values correctly, but again not every time I did that. So its really weird.

    #234239 Reply

    Riyad Kalla
    Member

    Hmm, I really don’t know why HSQL is behaving this way, I double checked your Hib code and it looks fine, you start your transaction then commit it… AFAIK that’s all you should need to do.

    Maybe you can try setting up a local MySQL install and seeing if the behavior normalizes? If it does, then it’s likely just a setup issue with HSQL.

Viewing 4 posts - 1 through 4 (of 4 total)
Reply To: session.save() does not save to database

You must be logged in to post in the forum log in