5 Replies Latest reply on Sep 14, 2005 8:50 AM by altheaparker

    Can't save data in database

      Hi,

      I am developing an application in jboss integreting hibernate.I am saving database through hibernate.It's not giving any error or exception. But,transaction is not commiting.
      Code of Bean class is like that................................

      SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
      Configuration cfg=new Configuration();
      cfg.configure(new File(System.getProperties().getProperty("sun.boot.library.path")+"\\hibernate.cfg.xml"));
      SessionFactory sessionFactory=cfg.buildSessionFactory();
      Tb_VoucherMain tb_vm=new Tb_VoucherMain(vMain.elementAt(0).toString(),formatter.parse(vMain.elementAt(1).toString()),vMain.elementAt(2).toString(),Double.parseDouble(vMain.elementAt(3).toString()),Double.parseDouble(vMain.elementAt(4).toString()),vMain.elementAt(5).toString());
      Tb_VoucherDetail tb_vd=new Tb_VoucherDetail(vDetail.elementAt(4).toString(),vDetail.elementAt(0).toString(),Double.parseDouble(vDetail.elementAt(2).toString()),Double.parseDouble(vDetail.elementAt(3).toString()));
      Session session=sessionFactory.openSession();
      Transaction tx=session.beginTransaction();
      session.save(tb_vm);
      session.save(tb_vd);
      tx.commit();
      session.flush();
      session.close();


      Can u help to fix out th problem.

        • 1. Re: Can't save data in database
          khoff999

          I've been having the same problem for 2 weeks. Its so bad that my management told me to stop using Hibernate, but I keep playing with it hoping I can figure it out. The problem has to be something stupid, but its equally as stupid that you can execute session.save() and not get an exception, or for that matter any intelligible DEBUG output.

          Maybe we can help each other on this (I think we better because no one has responded to my HIbernate Forum post in 2 weeks).

          Below is what I'm seeing in the console when I do a save. Obviously the mapping is good, it is calculating the generated identifer correctly (I already have a 0 & 1 in the table, so 2 is correct. What I'm not seeing is the SQL insert statement (ShowSQL is set to true).

          Do you see something similar? I am trying to step through the Hibernate code, but it is painful process because it is often hard to tell hat the methods should return if they are working correctly.


          14:02:52,828 DEBUG [SessionFactoryObjectFactory] JNDI lookup: hibernate/SessionFactory
          14:02:52,828 DEBUG [SessionFactoryObjectFactory] lookup: uid=4028a02e060db29a01060db2a2770000
          14:02:58,468 DEBUG [JDBCContext] successfully registered Synchronization
          14:02:58,484 DEBUG [SessionImpl] opened session at timestamp: 11255113782
          14:03:00,328 DEBUG [DefaultSaveOrUpdateEventListener] saving transient instance
          14:03:00,328 DEBUG [AbstractBatcher] opening JDBC connection
          14:03:00,343 DEBUG [IncrementGenerator] fetching initial value: select max(LEDGER_ID) from gl_Ledger
          14:03:00,343 DEBUG [SQL] select max(LEDGER_ID) from gl_Ledger
          14:03:02,328 DEBUG [IncrementGenerator] first free id: 2
          14:03:02,343 DEBUG [AbstractSaveEventListener] generated identifier: 2, using strategy: org.hibernate.id.IncrementGenerator
          14:03:02,359 DEBUG [AbstractSaveEventListener] saving [com.blackdog.gl.domain.Ledger#2]
          14:03:10,281 DEBUG [SessionImpl] closing session
          14:03:10,281 DEBUG [AbstractBatcher] closing JDBC connection (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)
          14:03:38,562 DEBUG [CacheSynchronization] transaction before completion callback
          14:03:38,562 DEBUG [CacheSynchronization] automatically flushing session
          14:03:38,562 DEBUG [SessionImpl] automatically flushing session
          14:03:38,562 DEBUG [JDBCContext] before transaction completion
          14:03:38,562 DEBUG [SessionImpl] before transaction completion
          14:03:38,609 DEBUG [CacheSynchronization] transaction after completion callback, status: 3
          14:03:38,609 DEBUG [JDBCContext] after transaction completion
          14:03:38,609 DEBUG [SessionImpl] after transaction completion

          • 2. Re: Can't save data in database
            khoff999

            Here's a little more I found out:

            During a save Hibernate is executing a method in class AbstractSaveEventListener that is testing whether a variable useIdentityColumn is true or false. It's false in mine (I don't know why, I do have an indentity column defined). It looks to me that the call to
            source.getActionQueue().execute(insert);
            is being skipped. I'm just guessing this in the path it needs to go down to execute the save. There must be another configuration setting needed to make useIdentityColumn == true.


            if (useIdentityColumn) {
            EntityIdentityInsertAction insert = new EntityIdentityInsertAction
            (values, entity, persister, source);
            source.getActionQueue().execute(insert);
            id = insert.getGeneratedId();
            persister.setIdentifier( entity, id, source.getEntityMode() );
            source.getPersistenceContext().checkUniqueness(id, persister, entity);
            //source.getBatcher().executeBatch (); //found another way to ensure that all batched joined inserts have been executed
            }

            • 3. Re: Can't save data in database
              altheaparker

              I was having the same problem too, and for a day and a half nearly went out of my mind trying to figure out why the data wouldn't save! Then I realized it was simple - I forgot to add session.flush() after saving but before closing the session. With jboss you use a HAR and put it in an EAR. By putting the hibernate classes in a HAR along with hibernate-service.xml as I understand you don't configure hibernate anymore with the hibernate.cfg.xml or hibernate.properties files. You also don't build the SessionFactory either you reference it, and don't need to do the transactions yourself in the method but the container will do them for you -of course don't forget to put in the ejb-jar.xml that your method has a required container managed transaction :) I think jboss also sets up the transaction configuration for you too, so you don't need to configure that for hibernate either.

              Here is my method:

              public void store(String title, Date theDate) throws EJBException
              {
              System.out.println("EventFacadeBean.store: entered in method");
              Session hsession = null;

              try
              {

              InitialContext ctx = new InitialContext();
              SessionFactory factory = (SessionFactory) ctx.lookup("java:/hibernate/SessionFactory");
              hsession = factory.openSession();

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

              hsession.save(theEvent);

              } catch(Exception e) {
              System.out.println("Exception found. " + e.getMessage());
              } finally {
              if (hsession!=null){
              hsession.flush();
              hsession.close();
              }
              }
              System.out.println("EventFacadeBean.store: finished method");
              }

              • 4. Re: Can't save data in database

                Can you post your hibernate.cfg.xml file for reference.

                Regards.

                Lokesh.

                • 5. Re: Can't save data in database
                  altheaparker

                  I didn't use a hibernate.cfg.xml or hibernate.properties when running the application as an EAR using jboss, accessing hibernate classes with a stateless session bean. I made a HAR which is a basic archive (just like a jar except .har instead) and put hibernate-service.xml in the META-INF folder. Then put the HAR in an EAR and put the above store() method in an ejb. My hibernate-service.xml looks like this. (i think jboss forum adds unnecessary br tags(?))




                  java:test
                  org.hibernate.dialect.HSQLDialect
                  java:/hibernate/SessionFactory
                  org.hibernate.cache.HashtableCacheProvider




                  When I was NOT running the application as an EAR (not using ejb) I ran the hibernate documentation tutorial using hibernate.cfg.xml file. Of course I had set up the hypersonic db beforehand.


                  <?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">


                  <hibernate-configuration>

                  <session-factory>
                  org.hsqldb.jdbcDriver
                  jdbc:hsqldb:C:\hsqldb\db4hibernate\test
                  sa

                  org.hibernate.dialect.HSQLDialect
                  true

                  org.hibernate.transaction.JDBCTransactionFactory


                  org.hibernate.cache.HashtableCacheProvider

                  update



                  </session-factory>

                  </hibernate-configuration>