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.  [ 14 posts ] 
Author Message
 Post subject: Runs perfectly but not updated in database table
PostPosted: Wed Jul 18, 2007 2:15 pm 
Beginner
Beginner

Joined: Wed Jul 18, 2007 7:38 am
Posts: 23
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hi all,

The application is running perfectly but it is not updating in the database table.

My code:


package roseindia.tutorial.hibernate;

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

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

try{
// 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 Record1");
Contact contact = new Contact();
System.out.println("Inserting");
contact.setId(6);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("deepak_38@yahoo.com");
session.save(contact);

System.out.println("Done2");
}catch(Exception e){
System.out.println("Error"+e);
}finally{
// Actual contact insertion will happen at this step
session.flush();

session.close();

}

}
}


Top
 Profile  
 
 Post subject: Bump up Logging First
PostPosted: Wed Jul 18, 2007 4:53 pm 
Newbie

Joined: Fri Apr 07, 2006 11:29 am
Posts: 17
Bump up the logging to debug, which may give you some insight into your problem. You may want to bump your logging up to include the SQL that Hibernate is generating. You can do so by following:

http://www.javalobby.org/java/forums/t44119.html


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 18, 2007 7:05 pm 
Regular
Regular

Joined: Sat Jan 22, 2005 6:57 pm
Posts: 50
Location: Chicago
You need to wrap your save or update with a transaction.



Code:
Transaction t = session.beginTransaction();
session.save(contact);
t.commit();



Hope this works for you.


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Thu Jul 19, 2007 12:23 am 
Beginner
Beginner

Joined: Wed Jul 18, 2007 7:38 am
Posts: 23
markricard wrote:
You need to wrap your save or update with a transaction.



Code:
Transaction t = session.beginTransaction();
session.save(contact);
t.commit();



Ya it was the problem thanks for ur help.


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Thu Jul 19, 2007 12:23 am 
Beginner
Beginner

Joined: Wed Jul 18, 2007 7:38 am
Posts: 23
markricard wrote:
You need to wrap your save or update with a transaction.



Code:
Transaction t = session.beginTransaction();
session.save(contact);
t.commit();



Ya it was the problem thanks for ur help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 19, 2007 12:24 am 
Regular
Regular

Joined: Sat Jan 22, 2005 6:57 pm
Posts: 50
Location: Chicago
Don't forget to rate! :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 19, 2007 2:27 am 
Beginner
Beginner

Joined: Wed Jul 18, 2007 7:38 am
Posts: 23
markricard wrote:
You need to wrap your save or update with a transaction.



Code:
Transaction t = session.beginTransaction();
session.save(contact);
t.commit();



Hope this works for you.


The solutions u give was perfectly working in my house but in my office the same example gives an exception given below:

Exception in thread "main" java.lang.NullPointerException
at roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:37)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 19, 2007 8:15 am 
Regular
Regular

Joined: Sat Jan 22, 2005 6:57 pm
Posts: 50
Location: Chicago
Looks like an application problem. What is on FirstExample.java line 37?

Also, be sure that you don't have other errors above that. Could be that you are pointing at the wrong database or the database does not exist.

The code I gave was correct.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 19, 2007 8:48 am 
Beginner
Beginner

Joined: Wed Jul 18, 2007 7:38 am
Posts: 23
markricard wrote:
Looks like an application problem. What is on FirstExample.java line 37?

Also, be sure that you don't have other errors above that. Could be that you are pointing at the wrong database or the database does not exist.

The code I gave was correct.

It is pointing towards flush() methods

No there are no error other than this.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 19, 2007 11:52 am 
Regular
Regular

Joined: Sat Jan 22, 2005 6:57 pm
Posts: 50
Location: Chicago
You are not providing enough information for me to help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 19, 2007 11:17 pm 
Beginner
Beginner

Joined: Wed Jul 18, 2007 7:38 am
Posts: 23
markricard wrote:
You are not providing enough information for me to help.

hello sir
i am right out of office so i will give u the most details on monday.

Thanks u vm


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 23, 2007 2:00 am 
Beginner
Beginner

Joined: Wed Jul 18, 2007 7:38 am
Posts: 23
shiva_krish wrote:
markricard wrote:
You are not providing enough information for me to help.

hello sir
i am right out of office so i will give u the most details on monday.

Thanks u vm


This is the example code:


package hibernate.tutorial;

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


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

try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
System.out.println("Done");
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 contact = new Contact();
contact.setId(6);
contact.setFirstName("Shiva");
contact.setLastName("Kumar");
contact.setEmail("shiva@yahoo.com");
session.saveOrUpdate(contact);

System.out.println("Done");
}catch(Exception e){
System.out.println("Error:"+e);
}finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();

}

}
}


This is my class :
[b]package hibernate.tutorial;


public class Contact {
private String firstName;
private String lastName;
private String email;
private int id;

public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}



}




[/b]


This is my hbm file:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="roseindia.tutorial.hibernate.Contact" table="contact">
<id name="id" type="int" 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>
</hibernate-mapping>

This is my config file:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hiber</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">shiva</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>


When i execute my FirstExample.java
I am getting exception given below:

Exception in thread "main" java.lang.NullPointerException
at hibernate.tutorial.FirstExample.main(FirstExample.java:32)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 23, 2007 3:53 am 
Newbie

Joined: Wed Mar 21, 2007 3:42 am
Posts: 4
Location: Chennai
Hi,

Can u give full stacktrace of the exception.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 23, 2007 4:47 am 
Beginner
Beginner

Joined: Wed Jul 18, 2007 7:38 am
Posts: 23
rajkm78 wrote:
Hi,

Can u give full stacktrace of the exception.


Exception:-

Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException
at hibernate.tutorial.FirstExample.main(FirstExample.java:16)


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