-->
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.  [ 5 posts ] 
Author Message
 Post subject: problem in Insert into database
PostPosted: Tue Jan 29, 2008 3:27 am 
Newbie

Joined: Fri Jan 25, 2008 8:32 am
Posts: 15
My code is running fine...everything is o.k. But when i check my database .it reflects empty set...my code is written below


[java] log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
[java] log4j:WARN Please initialize the log4j system properly.
[java] Inserting Record
[java] Done
[java] Hibernate: insert into CONTACT (FIRSTNAME, LASTNAME, EMAIL, ID) values (?, ?, ?, ?)
BUILD SUCCESSFUL

package hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;



public class FirstExample {

private Session session;

public static void main(String[] args) {
Session session = null;

try{
Configuration configuration = new Configuration();
configuration.setProperty("hibernate.connection.driver_class", com.mysql.jdbc.Driver.class.getName());
configuration.setProperty("hibernate.connection.url",("jdbc:mysql://localhost:3306/hibernatetutorial"));
configuration.setProperty("hibernate.connection.username", "root");
configuration.setProperty("hibernate.connection.password", "test");
configuration.setProperty("hibernate.connection.pool_size", "1");
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
configuration.addClass(Contact.class);
session = configuration.buildSessionFactory().openSession();

//"C:\\svn\\workspace\\hibernateTutorial\\cfg\\hibernate.cfg.xml"
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Contact c = new Contact();
c.setId(3);
c.setFirstName("Deepak");
c.setLastName("Kumar");
c.setEmail("deepak_38@yahoo.com");
session.persist(c);
System.out.println("Done");
}catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
}finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();

}

}

}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 29, 2008 5:20 am 
Newbie

Joined: Tue Jan 22, 2008 5:40 am
Posts: 10
Location: Hungary
I think you must commit the transaction before you close the session.


Top
 Profile  
 
 Post subject: Re: problem in Insert into database
PostPosted: Wed Jan 30, 2008 12:47 am 
Newbie

Joined: Fri Jan 25, 2008 8:32 am
Posts: 15
solution of the following error is ..
1. insert autocommit in configration xml file
2. use transaction on hibernateutil.java



rhlpnt wrote:
My code is running fine...everything is o.k. But when i check my database .it reflects empty set...my code is written below


[java] log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
[java] log4j:WARN Please initialize the log4j system properly.
[java] Inserting Record
[java] Done
[java] Hibernate: insert into CONTACT (FIRSTNAME, LASTNAME, EMAIL, ID) values (?, ?, ?, ?)
BUILD SUCCESSFUL

package hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;



public class FirstExample {

private Session session;

public static void main(String[] args) {
Session session = null;

try{
Configuration configuration = new Configuration();
configuration.setProperty("hibernate.connection.driver_class", com.mysql.jdbc.Driver.class.getName());
configuration.setProperty("hibernate.connection.url",("jdbc:mysql://localhost:3306/hibernatetutorial"));
configuration.setProperty("hibernate.connection.username", "root");
configuration.setProperty("hibernate.connection.password", "test");
configuration.setProperty("hibernate.connection.pool_size", "1");
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
configuration.addClass(Contact.class);
session = configuration.buildSessionFactory().openSession();

//"C:\\svn\\workspace\\hibernateTutorial\\cfg\\hibernate.cfg.xml"
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Contact c = new Contact();
c.setId(3);
c.setFirstName("Deepak");
c.setLastName("Kumar");
c.setEmail("deepak_38@yahoo.com");
session.persist(c);
System.out.println("Done");
}catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
}finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();

}

}

}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 30, 2008 7:42 am 
Newbie

Joined: Tue Jan 22, 2008 5:40 am
Posts: 10
Location: Hungary
Like this:

Code:
Transaction transaction = session.beginTransaction();
... //do something with the database
transaction.commit();
// session.flush() not required


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 30, 2008 8:48 am 
Newbie

Joined: Fri Jan 25, 2008 8:32 am
Posts: 15
thanks i did
kokog wrote:
Like this:

Code:
Transaction transaction = session.beginTransaction();
... //do something with the database
transaction.commit();
// session.flush() not required


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