-->
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.  [ 7 posts ] 
Author Message
 Post subject: Help Newbie: No errors but DB still wont insert the record
PostPosted: Fri Jun 02, 2006 8:00 am 
Newbie

Joined: Wed May 31, 2006 3:03 am
Posts: 9
Hi,
Now that i have successfully setup hibernate configs, still No Record is getting inserted in the db table abc_1412 after that SQL (it replaced values passed in java file with question marks as shown in Hibernate SQL log below). Though it successfully finds the required tables / fields (ref: see log below). as shown in the log below.

I want to insert the record!!

Hibernate version: 3.1

Mapping documents:

Code:
  <class name="tutorial.hibernate.Contact" table="CONTACT">
      <id name="id" type="long" column="ID" >
      <generator class="assigned"/>
     </id>

     <property name="firstName">
       <column name="FIRSTNAME" />
     </property>
     <property name="lastName">
      <column name="LASTNAME"/>
     </property>
     <property name="email">
      <column name="EMAIL"/>
     </property>
   </class>

   <class name="tutorial.hibernate.Book" table="book">
      <id name="lngBookId" type="long" column="id" >
         <generator class="increment"/>
      </id>

      <property name="strBookName">
         <column name="bookname" />
      </property>
   </class>

   <class name="tutorial.hibernate.Insurance" table="insurance">
      <id name="lngInsuranceId" type="long" column="ID" >
         <generator class="increment"/>
      </id>

      <property name="insuranceName">
         <column name="insurance_name" />
      </property>
      <property name="investementAmount">
         <column name="invested_amount" />
      </property>
      <property name="investementDate">
         <column name="investement_date" />
      </property>
   </class>


Code between sessionFactory.openSession() and session.close():
Code:
      try{
         // This step will read hibernate.cfg.xml and prepare hibernate for use
         SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
         session =sessionFactory.openSession();
         System.out.println("Inserting Record");
         Contact contact = new Contact();
         contact.setId(6);
         contact.setFirstName("Fee");
         contact.setLastName("Lee");
         contact.setEmail("flee@yahoo.com");
         session.save(contact);
         System.out.println("Done");
      }catch(Exception e){
         System.out.println(e.getMessage());
      }finally{
         // Actual contact insertion will happen at this step
         session.flush();


Full stack trace of any exception that occurs:
No exceptions
Name and version of the database you are using:
Oracle 8i
The generated SQL (show_sql=true):
Code:
Hibernate: insert into CONTACT (FIRSTNAME, LASTNAME, EMAIL, ID) values (?, ?, ?, ?)


Debug level Hibernate log excerpt:
Code:
15:51:08,515  INFO TableMetadata:38 - table found: ABC_1412.CONTACT
15:51:08,515  INFO TableMetadata:39 - columns: [lastname, firstname, email, id]
15:51:08,515  INFO TableMetadata:40 - foreign keys: []
15:51:08,515  INFO TableMetadata:41 - indexes: []
15:51:08,765  INFO TableMetadata:38 - table found: ABC_1412.BOOK
15:51:08,765  INFO TableMetadata:39 - columns: [bookname, id]
15:51:08,765  INFO TableMetadata:40 - foreign keys: []
15:51:08,765  INFO TableMetadata:41 - indexes: []
15:51:08,937  INFO TableMetadata:38 - table found: ABC_1412.INSURANCE
15:51:08,937  INFO TableMetadata:39 - columns: [insurance_name, invested_amount, investement_date, id]
15:51:08,937  INFO TableMetadata:40 - foreign keys: []
15:51:08,937  INFO TableMetadata:41 - indexes: []
15:51:08,937  INFO SchemaUpdate:153 - schema update complete
15:51:08,953  INFO DriverManagerConnectionProvider:147 - cleaning up connection pool: jdbc:oracle:thin:@localhost:1521:cs
15:51:08,953  INFO SessionFactoryImpl:366 - Checking 0 named queries
Inserting Record
Done
15:51:09,046 DEBUG SQL:292 - insert into CONTACT (FIRSTNAME, LASTNAME, EMAIL, ID) values (?, ?, ?, ?)
Hibernate: insert into CONTACT (FIRSTNAME, LASTNAME, EMAIL, ID) values (?, ?, ?, ?)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 8:17 am 
Beginner
Beginner

Joined: Fri May 26, 2006 7:15 am
Posts: 20
Location: Vandavasi, TamilNadu
Contact.java please ---

_________________
gajini
------------------


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 8:32 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
You may need to commit the transaction.

try using

Session s = sessionFactory.openSession();
Transaction t = s.beginTransaction();
...
session.save(contact);
..
t.commit();
s.close();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 8:34 am 
Regular
Regular

Joined: Thu Sep 22, 2005 1:53 pm
Posts: 88
Location: Rio de Janeiro
Hi I include this in your Java code:

Code:
Transaction tx = null; //Hibernate transaction!!!
try{
       ...........
         tx = session.beginTransaction()
         
//          your code to create and save your object

         tx.commit();
      }catch(Exception e){
         tx.rollback();
      }finally{
         // Actual contact insertion will happen at this step
        // no need to do a flush with transaction
      }
     


Or you put the following in your hibernate.cfg.xml:

Code:
    <property name="hibernate.connection.autocommit">true</property>

_________________
DonĀ“t forget to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 05, 2006 1:47 am 
Newbie

Joined: Wed May 31, 2006 3:03 am
Posts: 9
Have been trying.. unfort still da same result: no exception but no record would insert in db

The code:

Code:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
* Hibernate to inset data into Contact table
*/
public class FirstExample {
   public static void main(String[] args) {
      SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
      
      Session s = null;
      Transaction t = null;
      

      try {   
         // This step will read hibernate.cfg.xml and prepare hibernate for use
         
         s = sessionFactory.openSession();
         t = s.beginTransaction();

         // Create new instance of Contact and set values in it by reading them from form object
         System.out.println("Inserting Record");
         Contact contact = new Contact();
         contact.setId(6);
         contact.setFirstName("Fee");
         contact.setLastName("Lee");
         contact.setEmail("flee@yahoo.com");
         t.commit();
         s.save(contact);
         System.out.println("Done");
      }catch(Exception e){
         t.rollback();
         System.out.println(e.getMessage());
      }finally{
         // Actual contact insertion will happen at this step
         //session.flush();
         s.close();
         }
      
   }


the log..

Code:
10:39:32,281  INFO TableMetadata:38 - table found: ABC_1412.CONTACT
10:39:32,281  INFO TableMetadata:39 - columns: [lastname, firstname, email, id]
10:39:32,281  INFO TableMetadata:40 - foreign keys: []
10:39:32,281  INFO TableMetadata:41 - indexes: []
10:39:32,500  INFO TableMetadata:38 - table found: ABC_1412.BOOK
10:39:32,500  INFO TableMetadata:39 - columns: [bookname, id]
10:39:32,500  INFO TableMetadata:40 - foreign keys: []
10:39:32,500  INFO TableMetadata:41 - indexes: []
10:39:32,671  INFO TableMetadata:38 - table found: ABC_1412.INSURANCE
10:39:32,671  INFO TableMetadata:39 - columns: [insurance_name, invested_amount, investement_date, id]
10:39:32,671  INFO TableMetadata:40 - foreign keys: []
10:39:32,671  INFO TableMetadata:41 - indexes: []
10:39:32,687  INFO SchemaUpdate:153 - schema update complete
10:39:32,687  INFO DriverManagerConnectionProvider:147 - cleaning up connection pool: jdbc:oracle:thin:@localhost:1521:cs
10:39:32,687  INFO SessionFactoryImpl:366 - Checking 0 named queries
Inserting Record
Done


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 05, 2006 2:12 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
"s.save(contact)" should be between t = s.beginTransaction(); and t.commit.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 05, 2006 2:19 am 
Newbie

Joined: Wed May 31, 2006 3:03 am
Posts: 9
aaarrhh.. change of order solved my problem - Thanks everyone!

contact.setEmail("flee@yahoo.com");
t.commit();
s.save(contact);
System.out.println("Done");

contact.setEmail("flee@yahoo.com");
s.save(contact);
t.commit();
System.out.println("Done");

Cheers

- - - edit - - -
@JayeshJ - had the order fixed b4 i cud postback the results - thx for the quick reply - gonn give u credit fo tha though ;)
- - - - - - - - -


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