-->
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.  [ 6 posts ] 
Author Message
 Post subject: Can't Update the Database
PostPosted: Tue Mar 07, 2006 12:09 pm 
Newbie

Joined: Tue Mar 07, 2006 11:39 am
Posts: 4
I am fairly new to Hibernate and apparantly really dumb because I have the book and followed the online guide and still can't get it to update my database. I can select records just fine, but it will not save anything.

I apologize if this has been answered before. I saw something similar on http://forum.hibernate.org/viewtopic.php?t=932154&highlight=flush but the answer they gave (mutable="false") did not work as I am not doing this anywhere. I also saw a question about this in the FAQ, but all it said was to call commit() or flush(), and I am already calling commit(). What am I doing wrong?

Hibernate version:
3.1.2
Mapping documents:
Event.hbm.xml
Code between sessionFactory.openSession() and session.close():
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
session.save(theEvent);
session.flush();
session.getTransaction().commit();
Full stack trace of any exception that occurs:
no exceptions!!
Name and version of the database you are using:
HSQL (org.hsqldb.jdbcDriver)
The generated SQL (show_sql=true):
Hibernate: select max(EVENT_ID) from EVENT
Hibernate: insert into EVENT (EVENT_DATE, TITLE, EVENT_ID) values (?, ?, ?)

I am using netBeans, this is what it gives me...
init:
deps-jar:
compile:
10:04:17,265 INFO Environment:479 - Hibernate 3.1.2
10:04:17,281 INFO Environment:509 - hibernate.properties not found
10:04:17,281 INFO Environment:525 - using CGLIB reflection optimizer
10:04:17,296 INFO Environment:555 - using JDK 1.4 java.sql.Timestamp handling
10:04:17,375 INFO Configuration:1308 - configuring from resource: /hibernate.cfg.xml
10:04:17,375 INFO Configuration:1285 - Configuration resource: /hibernate.cfg.xml
10:04:17,546 INFO Configuration:469 - Reading mappings from resource: events/Event.hbm.xml
10:04:17,703 INFO HbmBinder:309 - Mapping class: events.Event -> EVENT
10:04:17,734 INFO Configuration:1419 - Configured SessionFactory: null
10:04:17,828 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
10:04:17,828 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 1
10:04:17,828 INFO DriverManagerConnectionProvider:45 - autocommit mode: false
10:04:17,859 INFO DriverManagerConnectionProvider:80 - using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:file:/data/testdb
10:04:17,859 INFO DriverManagerConnectionProvider:86 - connection properties: {user=sa, password=****}
10:04:18,359 INFO SettingsFactory:77 - RDBMS: HSQL Database Engine, version: 1.8.0
10:04:18,359 INFO SettingsFactory:78 - JDBC driver: HSQL Database Engine Driver, version: 1.8.0
10:04:18,406 INFO Dialect:103 - Using dialect: org.hibernate.dialect.HSQLDialect
10:04:18,437 INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
10:04:18,437 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
10:04:18,437 INFO SettingsFactory:125 - Automatic flush during beforeCompletion(): disabled
10:04:18,437 INFO SettingsFactory:129 - Automatic session close at end of transaction: disabled
10:04:18,437 INFO SettingsFactory:136 - JDBC batch size: 15
10:04:18,437 INFO SettingsFactory:139 - JDBC batch updates for versioned data: disabled
10:04:18,437 INFO SettingsFactory:144 - Scrollable result sets: enabled
10:04:18,437 INFO SettingsFactory:152 - JDBC3 getGeneratedKeys(): disabled
10:04:18,437 INFO SettingsFactory:160 - Connection release mode: auto
10:04:18,453 INFO SettingsFactory:187 - Default batch fetch size: 1
10:04:18,453 INFO SettingsFactory:191 - Generate SQL with comments: disabled
10:04:18,453 INFO SettingsFactory:195 - Order SQL updates by primary key: disabled
10:04:18,453 INFO SettingsFactory:338 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
10:04:18,453 INFO ASTQueryTranslatorFactory:24 - Using ASTQueryTranslatorFactory
10:04:18,453 INFO SettingsFactory:203 - Query language substitutions: {}
10:04:18,453 INFO SettingsFactory:209 - Second-level cache: enabled
10:04:18,453 INFO SettingsFactory:213 - Query cache: disabled
10:04:18,453 INFO SettingsFactory:325 - Cache provider: org.hibernate.cache.NoCacheProvider
10:04:18,468 INFO SettingsFactory:228 - Optimize cache for minimal puts: disabled
10:04:18,468 INFO SettingsFactory:237 - Structured second-level cache entries: disabled
10:04:18,468 INFO SettingsFactory:257 - Echoing all SQL to stdout
10:04:18,468 INFO SettingsFactory:264 - Statistics: disabled
10:04:18,468 INFO SettingsFactory:268 - Deleted entity synthetic identifier rollback: disabled
10:04:18,468 INFO SettingsFactory:283 - Default entity-mode: pojo
10:04:18,546 INFO SessionFactoryImpl:153 - building session factory
10:04:18,984 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
Hibernate: select max(EVENT_ID) from EVENT
Hibernate: insert into EVENT (EVENT_DATE, TITLE, EVENT_ID) values (?, ?, ?)
My Event
3
10:04:19,171 INFO SessionFactoryImpl:728 - closing
10:04:19,171 INFO DriverManagerConnectionProvider:147 - cleaning up connection pool: jdbc:hsqldb:file:/data/testdb
debug:
BUILD SUCCESSFUL (total time: 6 seconds)

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

Any help would be appreciated!! thx


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 07, 2006 11:16 pm 
Beginner
Beginner

Joined: Thu Nov 11, 2004 12:18 pm
Posts: 37
Location: Baltimore, MD
Try this as a replacement for your code:

Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction txn = session.beginTransaction();
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
session.save(theEvent);
session.flush();
txn.commit();


The important part to note is that I'm storing the Transaction instance, and invoking the commit() on that instance.

_________________
-Chris


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 11:04 am 
Newbie

Joined: Tue Mar 07, 2006 11:39 am
Posts: 4
I tried this and it still won't save to the database. Any other suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 11:27 am 
Newbie

Joined: Tue Mar 07, 2006 11:39 am
Posts: 4
still can't save to database, here is the code :) what am i doing wrong?

Code:
package util;

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

public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure().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 SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

Code:
package events;

import org.hibernate.Session;
import java.util.Date;
import org.hibernate.Transaction;
import util.HibernateUtil;

public class EventManager {

    public static void main(String[] args) {
        EventManager mgr = new EventManager();
        mgr.createAndStoreEvent("My Event", new Date());
        HibernateUtil.getSessionFactory().close();
    }

    private void createAndStoreEvent(String title, Date theDate) {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction txn = session.beginTransaction();
        Event theEvent = new Event();
        theEvent.setTitle(title);
        theEvent.setDate(theDate);
        session.save(theEvent);
        session.flush();
        txn.commit();
        System.out.println(theEvent.getTitle());
        System.out.println(theEvent.getId()); 
    }
}

Code:
<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="connection.url">jdbc:hsqldb:file:/data/testdb</property>
        <property name="connection.username">sa</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>
        <mapping resource="events/Event.hbm.xml"/>
    </session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 1:00 pm 
Beginner
Beginner

Joined: Thu Nov 11, 2004 12:18 pm
Posts: 37
Location: Baltimore, MD
I'm looking at your hibernate logs, and I don't see an entry for the Transaction committing. Whether it completes or fails, you should see a log message for it. I'm guessing you have all org.hibernate.* classes set up to log in debug mode.

It looks as though everything is fine except for that commit. The generated SQL matches what should be happening. One thing you might try to do is, is download the source, run in debug mode, and step into txn.commit() to see if there is anything strange going on.

_________________
-Chris


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 1:04 am 
Newbie

Joined: Tue Mar 07, 2006 9:09 am
Posts: 2
Try to add the following property in your hibernate-configuration file:

<!-- Bind the getCurrentSession() method to the thread (don't use for EJBs) -->
<property name="current_session_context_class">thread</property>


In my tutorial, I did not have to use session.flush(); however by adding the above property and the Enable Hibernate's automatic session context management property, which you have done already, seems to be able to insert a row into the Events table.

Without this property, I got an exception stayed that there is no context for the current session (upon getCurrentSession()). The code did not go any further than trying to get a session to perform any database work. (There is a connection to the database, though.)

Hope it helps.


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