-->
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: values not save in DB from java class
PostPosted: Tue Jun 10, 2008 5:37 am 
Beginner
Beginner

Joined: Sat May 31, 2008 2:35 am
Posts: 30
Location: pakistan
hi i create the application and using hibernate i it...


i create simple class and try to save the value in it...using that code


Session session = null;

try{
SessionFactory sessionFactory = new

Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
//Create new instance of Contact and set
System.out.println("Inserting Record");

Product prd=new Product();
prd.setTitle("new Hibernate Product by softwarepattern");
prd.setProductId(3600);
prd.setUrl("http://www.google.com");
session.save(prd);
System.out.println("Done");
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();

}



it show me that msg when i run the class but no change occur in DB i using mySQL and there is no Exception occur when i run the classs..

can any one tell me why my values not save in DB...

although if i create the DAO obj using that code


ProductDAO pdao=new ProductDAO();
List<Product> li=pdao.findAll();

System.out.println("size "+li.size());
for(Product p:li)
{
System.out.println(p.getTitle());

}



it run sucessfully in same class and sho me the out put from the table...





plzz GUIDE me how i save value from hibernate using simple java class


[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 10, 2008 8:25 am 
Newbie

Joined: Mon Jun 09, 2008 6:55 pm
Posts: 3
Location: Israel, Haifa
I'm new to Hibernate, but I think your solution will be to open a transaction for each DML (Insert, Update, Delete):

Example:

Code:
      
session.beginTransaction();
session.save(obj);
session.getTransaction().commit();   
//Now check the DB for new updates of the data

_________________
Thanks,

Omri


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 10, 2008 9:18 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Indeed, your problem is likely the issue of transaction demarcation.

It looks like you're expecting your DAOs to begin and end a transaction. That's not a great design pattern, as you get far too many database transactions. Instead, allow the client to demarcate the transaction, by starting and stopping the transaction.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 10, 2008 11:16 pm 
Beginner
Beginner

Joined: Sat May 31, 2008 2:35 am
Posts: 30
Location: pakistan
yaa thanx fr reply...




ession.beginTransaction();
session.save(obj);
session.getTransaction().commit();
//Now check the DB for new updates of the data



its working and my transection save in DB through hibernate...
thanx...
---------------------------------------------------------------------------


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.