Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Problem inserting to Database

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to insert the record to DB using org.springframework.orm.hibernate3.HibernateTempla te. Everything is going fine no error on the console but when I try to see the record in the DB no record exists.(I am not using datasource.)

But, when I try to insert the record using pure Hibernate API it is working fine. Below is the code.

************************************************
* SessionFactory sessionFactory = new Configuration().configure ().buildSessionFactory ();
* Session session = sessionFactory.openSession ();
* Transaction t = session.beginTransaction();
* t.begin();
* session.persist(empdata);
* t.commit();
***********************************************


Now below is the code from Spring

Below is my applicationcontext.xml

<beans>
<bean id="MyTemplate" class="org.springframework.orm.hibernate3.Hibernat eTemplate">
<property name="sessionFactory"> <ref bean="MySessionFactory"/></property>
</bean>

<bean id="MySessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">

<property name="mappingResources">
<list>
<value>emp.hbm.xml</value>
</list>
</property>

<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.driver_class">com.xxx.xx xx.JDBCDriver</prop>
<prop key="hibernate.connection.password">tiger</prop>
<prop key="hibernate.connection.url">jdbc://...</prop>
<prop key="hibernate.connection.username">scott</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.DB2D ialect</prop>
</props>
</property>
</bean>


In the Java Prog
--------------------

HibernateTemplate hTemplate = (HibernateTemplate)EmpBeanFactory.getBean("MYConte xt","MyTemplate");
hTemplate.saveOrUpdate(empdata);
hTemplate.flush(); // tried with/without using flush


Interstingly, when I try to retrieve the record from the Database, it is going fine. I can iterate through all the records.

List res = rmTemplate.find("From emp e");
System.out.println("Number of records fetched " + res.size());

It is printing 22 records on the console.

I did one more test, like before inserting to DB I fetched the records it is 22. After insert it is printing 23 but when look into db its only 22. Record is somehow not committing to DB but it in the Session.


I know somewhere I am missing something. Could anybody please help me to figure out this.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are running this within a transaction and a runtime exception occurs, then the transaction will be rolled back. Are you sure you are not swallowing the exception somewhere?
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sabarish Sasidharan:
If you are running this within a transaction and a runtime exception occurs, then the transaction will be rolled back. Are you sure you are not swallowing the exception somewhere?



I'd bet Sabarish is on the right track. Springs exceptions must be caught or they are swallowed. Surround all your db related work in a try/catch block and see what you get.
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic