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.  [ 1 post ] 
Author Message
 Post subject: Unable to save data into DB with hibernate template
PostPosted: Mon Sep 21, 2009 9:02 am 
Newbie

Joined: Thu Aug 17, 2006 1:33 am
Posts: 3
Location: India
My requirement is to use multiple distinct database. So I have created multiple config files like filename.cfg.xml for multiple session factories.
Code:
<hibernate-configuration>
    <session-factory>
      <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/dbanme</property>
        <property name="hibernate.connection.username">username</property>
        <property name="hibernate.connection.password">pswd</property>
        <property name="hibernate.show_sql">true</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <mapping resource="user.hbm.xml"/>
        <mapping resource="userGroup.hbm.xml"/>
        <mapping resource="address.hbm.xml"/>
    </session-factory>
</hibernate-configuration>


Similarly I have created a file for Oracle Database.

I have a written a class HibernateUtil.java to create session factories(SF) and hibernate template(HT) from above config file and putting them into map so that based on customerId system can use respective SF or HT to interact with their DB.

Code:
public class HibernateUtil {
    private static HashMap<String, HibernateTemplate> hibTemplateFactoryMap = new HashMap<String, HibernateTemplate>();
    public static final ThreadLocal sessionMapsThreadLocal = new ThreadLocal();
   
   public static Session getCurrentSession(String key) throws HibernateException {
      HashMap<String, Session> sessionMaps = (HashMap<String, Session>) sessionMapsThreadLocal.get();
        if (sessionMaps == null) {
            sessionMaps = new HashMap();
            sessionMapsThreadLocal.set(sessionMaps);
        }
        // Open a new Session, if this Thread has none yet
        Session s = (Session) sessionMaps.get(key);
        if (s == null) {
            s = ((SessionFactory)((HibernateTemplate)hibTemplateFactoryMap.get(key)).getSessionFactory()).openSession();
            sessionMaps.put(key, s);
        }

        return s;
    }

    public static void buildSessionFactories(HashMap<String, String> configs) {
        try {
            // Create the SessionFactory
            for (String key : configs.keySet()) {
                URL url = HibernateUtil.class.getResource(configs.get(key));
                SessionFactory sessionFactory = new Configuration().configure(url).buildSessionFactory();
                HibernateTemplate template = new HibernateTemplate(sessionFactory);
                hibTemplateFactoryMap.put(key, template);
            }
        } catch (Exception ex) {
            ex.printStackTrace(System.out);
            System.out.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);

        } // end of the try - catch block
    }

    public static HibernateTemplate getHibTemplate(String key) throws HibernateException {
       HibernateTemplate hibTemplate = hibTemplateFactoryMap.get(key);
   
       return hibTemplate;
   }
   
} // end of the class


Part A :TestMultiSF class is trying to insert/save a record into Oracle database. System is not able to save record in database without any exception.

Hibernate: select max(id) from test
Hibernate: insert into test (name, category, count, id) values (?, ?, ?, ?)

Might be my application is not able to commit changes.
Code:
public class TestMultiSF {
   public static void main(String[] args) {
      HashMap<String, String> configs = new HashMap();
      configs.put("cust1", "myoracle.cfg.xml");
      configs.put("cust2", "mysql.cfg.xml");
      HibernateUtil.buildSessionFactories(configs);
      Test test = new Test();
      try {
         test.setCategory("Testing12");
         test.setName("vikky12");
         test.setCount(1);
         HibernateUtil.getHibTemplate("cust1").save(test);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}


Part B :I am to able insert records into Database as given below code.
Code:
public class TestMultiSF {
   public static void main(String[] args) {
      HashMap<String, String> configs = new HashMap();
      configs.put("cust1", "myoracle.cfg.xml");
      configs.put("cust2", "mysql.cfg.xml");
      HibernateUtil.buildSessionFactories(configs);
      Test test = new Test();
      try {
         test.setCategory("Testing12");
         test.setName("vikky12");
         test.setCount(1);
         Session s = HibernateUtil.getCurrentSession("cust1");
         Transaction tx= s.beginTransaction();
         s.save(test);
         tx.commit();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}


I need to keep both the approaches i.e. Part A[with HT] and Part B[with session]. Can anyone help me to find out a mistake or configuration missing in Part B apporach. I am not able to DML operation with hibernate template.

Thanks in advance.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.