-->
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.  [ 5 posts ] 
Author Message
 Post subject: No records are registererd while doing the tutorial
PostPosted: Tue Feb 24, 2009 7:01 am 
Newbie

Joined: Thu Feb 19, 2009 12:24 pm
Posts: 14
Hello,

I am new to Hibernate and I am trying to learn by doing the tutorial http://www.hibernate.org/hib_docs/v3/re ... stapp.html

It seems that there is nothing stored when I am trying out the tutorial. I assume that what is in the database should be listed when providing the "list" argument while executing. The only thing I see that differs is when I try to store evetns the SQL that is presented has this row
Code:
(null, ?, ?)
but should it really look like that, should null be there? What is the problem and is there any other way to find out what is stored in the HSQLDB database?

Hibernate version:
3.3.1.ga

Mapping documents:

Code:
<?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>

    <class name="events.Event" table="EVENTS">
        <id name="id" column="EVENT_ID">
            <generator class="native"/>
        </id>
        <property name="date" type="timestamp" column="EVENT_DATE"/>
        <property name="title"/>
    </class>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Code:
private void createAndStoreEvent(String title, Date theDate) {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();

        Event theEvent = new Event();
        theEvent.setTitle(title);
        theEvent.setDate(theDate);

        session.save(theEvent);

        session.getTransaction().commit();
    }

   private List listEvents() {

         Session session = HibernateUtil.getSessionFactory().getCurrentSession();

          session.beginTransaction();

          List result = session.createQuery("from Event").list();

          session.getTransaction().commit();

       return result;
   }

}


Full stack trace of any exception that occurs:

No stacktrace presented



Name and version of the database you are using:
HSQLDB 1.8.0.10

The generated SQL (show_sql=true):

When storing events:
Code:
Hibernate:
     insert
     into
         EVENTS
         (EVENT_ID, EVENT_DATE, title)
     values
         (null, ?, ?)
Hibernate:
     call identity()


When trying to list events that has been stored:
Code:
Hibernate:
    select
        event0_.EVENT_ID as EVENT1_0_,
        event0_.EVENT_DATE as EVENT2_0_,
        event0_.title as title0_
    from
        EVENTS event0_



hibernate.cfg.xml:

Code:
<?xml version="1.0"?><!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
        <property name="connection.username">sa</property>
        <property name="connection.password"></property>

   <!--<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/hb</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>-->

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</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>

        <mapping resource="events/Event.hbm.xml"/>

    </session-factory>

</hibernate-configuration>


Thanks in advance!

_________________
Beginners must also learn!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 7:21 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
Are you sure that your list does not contain a single Event? You could iterate your list und print each Event or check size() of the list. Do you call insert and select inside a single program or do you have two programs? If you have two, your tables are created each time you call it.
Because of that: <property name="hbm2ddl.auto">create</property>
Just comment out after the first program is finished.

Null seem to be okay because your mapping says that database itself has to fill in the "next" value.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 8:30 am 
Newbie

Joined: Thu Feb 19, 2009 12:24 pm
Posts: 14
Yes! It worked when I, in hibernate.cfg.xml, commented out
Code:
<property name="hbm2ddl.auto">create</property>


Exactly at which point is the databas created? Is it at this row:

Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();


or perhaps:

Code:
session.beginTransaction();


Thanks!

_________________
Beginners must also learn!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 8:35 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
The database is created when you initialize your SessionFactory for the first time.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 8:37 am 
Newbie

Joined: Thu Feb 19, 2009 12:24 pm
Posts: 14
Ok, great, thanks!

_________________
Beginners must also learn!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.