-->
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: Nothing gets saved!
PostPosted: Thu May 18, 2006 12:27 am 
Newbie

Joined: Thu May 18, 2006 12:12 am
Posts: 3
Hello All,
I am a hibernate novice and working my way through the tutorial from Hibernate. I have a Oracle 10g. Everythings seem to be working fine as I don't see any exceptions being thrown. Here are what I get in my log:

    22:20:27,651 INFO Environment:464 - Hibernate 3.0.5
    22:20:27,651 INFO Environment:477 - hibernate.properties not found
    22:20:27,651 INFO Environment:510 - using CGLIB reflection optimizer
    22:20:27,651 INFO Environment:540 - using JDK 1.4 java.sql.Timestamp handling
    22:20:27,712 INFO Configuration:1110 - configuring from resource: /hibernate.cfg.xml
    22:20:27,712 INFO Configuration:1081 - Configuration resource: /hibernate.cfg.xml
    22:20:27,962 INFO Configuration:444 - Mapping resource: Event.hbm.xml
    22:20:28,072 INFO HbmBinder:260 - Mapping class: trai.hibernate.Event -> EVENT
    22:20:28,092 INFO Configuration:1222 - Configured SessionFactory: null
    22:20:28,092 INFO Configuration:875 - processing extends queue
    22:20:28,092 INFO Configuration:879 - processing collection mappings
    22:20:28,092 INFO Configuration:888 - processing association property references
    22:20:28,092 INFO Configuration:917 - processing foreign key constraints
    22:20:28,152 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
    22:20:28,152 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 10
    22:20:28,152 INFO DriverManagerConnectionProvider:45 - autocommit mode: false
    22:20:28,162 INFO DriverManagerConnectionProvider:80 - using driver: oracle.jdbc.driver.OracleDriver at URL: jdbc:oracle:thin:@localhost:1521:jaguar
    22:20:28,162 INFO DriverManagerConnectionProvider:86 - connection properties: {user=scott, password=****}
    22:20:28,423 INFO SettingsFactory:77 - RDBMS: Oracle, version: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
    With the Partitioning, OLAP and Data Mining options
    22:20:28,433 INFO SettingsFactory:78 - JDBC driver: Oracle JDBC driver, version: 10.2.0.1.0
    22:20:28,453 INFO Dialect:92 - Using dialect: org.hibernate.dialect.Oracle9Dialect
    22:20:28,463 INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
    22:20:28,463 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
    22:20:28,473 INFO SettingsFactory:125 - Automatic flush during beforeCompletion(): disabled
    22:20:28,473 INFO SettingsFactory:129 - Automatic session close at end of transaction: disabled
    22:20:28,473 INFO SettingsFactory:136 - JDBC batch size: 15
    22:20:28,473 INFO SettingsFactory:139 - JDBC batch updates for versioned data: disabled
    22:20:28,473 INFO SettingsFactory:144 - Scrollable result sets: enabled
    22:20:28,473 INFO SettingsFactory:152 - JDBC3 getGeneratedKeys(): enabled
    22:20:28,473 INFO SettingsFactory:160 - Connection release mode: null
    22:20:28,473 INFO SettingsFactory:187 - Default batch fetch size: 1
    22:20:28,473 INFO SettingsFactory:191 - Generate SQL with comments: disabled
    22:20:28,473 INFO SettingsFactory:195 - Order SQL updates by primary key: disabled
    22:20:28,473 INFO SettingsFactory:334 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
    22:20:28,473 INFO ASTQueryTranslatorFactory:21 - Using ASTQueryTranslatorFactory
    22:20:28,473 INFO SettingsFactory:203 - Query language substitutions: {}
    22:20:28,483 INFO SettingsFactory:209 - Second-level cache: enabled
    22:20:28,483 INFO SettingsFactory:213 - Query cache: disabled
    22:20:28,483 INFO SettingsFactory:321 - Cache provider: org.hibernate.cache.EhCacheProvider
    22:20:28,483 INFO SettingsFactory:228 - Optimize cache for minimal puts: disabled
    22:20:28,483 INFO SettingsFactory:237 - Structured second-level cache entries: disabled
    22:20:28,493 INFO SettingsFactory:257 - Echoing all SQL to stdout
    22:20:28,493 INFO SettingsFactory:261 - Statistics: disabled
    22:20:28,493 INFO SettingsFactory:265 - Deleted entity synthetic identifier rollback: disabled
    22:20:28,503 INFO SettingsFactory:279 - Default entity-mode: pojo
    22:20:28,583 INFO SessionFactoryImpl:152 - building session factory
    22:20:28,593 WARN Configurator:126 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/Program%20Files/MyEclipse/eclipse/plugins/org.hibernate.eclipse_4.1.0/myeclipse-data/3.0/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
    22:20:28,823 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
    22:20:28,823 INFO Dialect:92 - Using dialect: org.hibernate.dialect.Oracle9Dialect
    22:20:28,823 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
    22:20:28,823 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 10
    22:20:28,823 INFO DriverManagerConnectionProvider:45 - autocommit mode: false
    22:20:28,823 INFO DriverManagerConnectionProvider:80 - using driver: oracle.jdbc.driver.OracleDriver at URL: jdbc:oracle:thin:@localhost:1521:jaguar
    22:20:28,823 INFO DriverManagerConnectionProvider:86 - connection properties: {user=scott, password=****}
    22:20:28,833 INFO SchemaUpdate:105 - Running hbm2ddl schema update
    22:20:28,833 INFO SchemaUpdate:117 - fetching database metadata
    22:20:28,923 INFO SchemaUpdate:133 - updating schema
    22:20:28,933 INFO Configuration:875 - processing extends queue
    22:20:28,933 INFO Configuration:879 - processing collection mappings
    22:20:28,933 INFO Configuration:888 - processing association property references
    22:20:28,933 INFO Configuration:917 - processing foreign key constraints
    22:20:29,154 INFO TableMetadata:38 - table found: SCOTT.EVENT
    22:20:29,154 INFO TableMetadata:39 - columns: [event_id, title, event_date]
    22:20:29,154 INFO TableMetadata:40 - foreign keys: []
    22:20:29,154 INFO TableMetadata:41 - indexes: []
    22:20:29,154 INFO SchemaUpdate:153 - schema update complete
    22:20:29,154 INFO DriverManagerConnectionProvider:147 - cleaning up connection pool: jdbc:oracle:thin:@localhost:1521:jaguar
    22:20:29,154 INFO SessionFactoryImpl:379 - Checking 0 named queries
    Hibernate: select hibernate_sequence.nextval from dual
    22:20:29,204 INFO SessionFactoryImpl:776 - closing
    22:20:29,204 INFO DriverManagerConnectionProvider:147 - cleaning up connection pool: jdbc:oracle:thin:@localhost:1521:jaguar


I have Oracle table "EVENT" but there is nothing being saved yet. I have no idea why? can you please help me with it?
Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 1:06 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You'll have to post your example code. You need to do something like:
- Create object to be saved
- call session.getTransaction().begin()
- call session.save(obj)
- call session.commit()

Is this what you're doing? You're not closing the session without committing/flushing?

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 1:27 am 
Newbie

Joined: Thu May 18, 2006 12:12 am
Posts: 3
I think I have done what you have suggested. Here is what I have done:
Code:

package com.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
   private static final SessionFactory sessionFactory;
   
   static{
      try{
         sessionFactory = new Configuration().configure().buildSessionFactory();
      }catch(Throwable ex){
         System.err.println("Initial SessionFactory creation failed.");
         throw new ExceptionInInitializerError(ex);
      }
   }
   
   public static SessionFactory getSessionFactory(){
      return sessionFactory;
   }
}


==============================================

package com.event;

import java.util.Date;

import org.hibernate.Session;

import com.hibernate.Event;
import com.util.HibernateUtil;

public class EventManager {

   public static void main(String[] args) {
      // TODO Auto-generated method stub
      EventManager mgr = new EventManager();
      if(args[0].equals("store")){
         mgr.createAndStoreEvent("My Event", new Date());
      }
      HibernateUtil.getSessionFactory().close();
   }
   
   private void createAndStoreEvent(String title, Date theDate){
      //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      //the above didn't work for some reason, i have to do the following
      Session session = HibernateUtil.getSessionFactory().openSession();
      session.beginTransaction();
      
      Event theEvent = new Event();
      theEvent.setTitle(title);
      theEvent.setDate(theDate);
      
      session.save(theEvent);

      session.beginTransaction().commit();
   }
}



I would appreciate your help. thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 2:01 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The transaction that you do the save in is never committed. You commit a new, empty transaction. Here's the modified code:
Code:
      Transaction tx = session.beginTransaction();
     
      Event theEvent = new Event();
      theEvent.setTitle(title);
      theEvent.setDate(theDate);
     
      session.save(theEvent);

      tx.commit();

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 2:27 am 
Newbie

Joined: Thu May 18, 2006 12:12 am
Posts: 3
That's it. Works fine now. Appreciate your quick help. Thank you.

by the way, like I showed above, what's difference between getCurrentSession() and openSession()? Why did the later work and the first didn't? Where each of these is used?

I would appreciate if you could educate me on this too. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 5:27 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
getCurrentSession uses the CurrentSessionContext to find out what session is "current". If you're using a framework (like JBoss), or even a library of convenience methods, there may be a CurrentSessionContext set up and properly configured for you, but if you're running a standalone app, written from scratch, then there won't be. In this case, getCurrentSession won't work for you.

If you want to use getCurrentSession, read the hibernate javadocs for CurrentSessionContext, and see which of the hibernate-provided session contexts is appropriate for you. I can't find any examples showing how to set it up, but there's probably something in the wiki area on this site.

_________________
Code tags are your friend. Know them and use them.


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.