-->
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.  [ 4 posts ] 
Author Message
 Post subject: session.save not working properly
PostPosted: Fri Dec 04, 2009 5:23 am 
Newbie

Joined: Fri Dec 04, 2009 5:10 am
Posts: 14
Hello All,
I have a function which saves an instance to the database through session.save(instance). It does not save the instance to database and it never throws any exception. Can anyone tell me what I am doing wrong. Besides if I try to retrieve values of from database, they are retrieved but the problem is only with saving. My code is as under.

public static void saveTable1() {
Session session = null;
try{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
Table1 table1 = new Table1();
table1.setId(4);
table1.setName(new Character('F'));
session.save(table1);
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
session.flush();
session.close();
}
}

Regards,
Fawad.


Top
 Profile  
 
 Post subject: Re: session.save not working properly
PostPosted: Fri Dec 04, 2009 5:51 am 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
Don't know if this really would fix your problem, but:

You might add the following:
Code:
try {
    .........
    session = sessionFactory.openSession();
    session.beginTransaction();
    Table 1..................
    // Do what you need
    session.save(table1);
    session.getTransaction().commit;
} catch (Exception e) {
    .............


Or do you do transaction demarcation through Spring? (Then you wouldn't need the new Configuration().configure() stuff either.


Top
 Profile  
 
 Post subject: Re: session.save not working properly
PostPosted: Fri Dec 04, 2009 6:03 am 
Newbie

Joined: Fri Dec 04, 2009 5:52 am
Posts: 9
buddy,

u r saving an instance which is transaction. so use session.beginTransaction(); b4 save and tx.commit() after save

Jyeshta


Top
 Profile  
 
 Post subject: Re: session.save not working properly
PostPosted: Mon Dec 14, 2009 4:02 am 
Newbie

Joined: Fri Dec 04, 2009 5:10 am
Posts: 14
Hello guys,
Thanks alot. It helped me alot.


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