-->
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.  [ 6 posts ] 
Author Message
 Post subject: [b]unable to insert a record into database using hibernate
PostPosted: Fri Aug 15, 2008 9:26 am 
Newbie

Joined: Fri Aug 15, 2008 9:09 am
Posts: 12
unable to insert a record into database using hibernate:

Here is the code:

javabean:

package hibernate_example;

public class User {

String First_Name;
String Last_Name;
int age;
int User_id;
String Email;


public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
public String getFirst_Name() {
return First_Name;
}
public void setFirst_Name(String first_Name) {
First_Name = first_Name;
}
public String getLast_Name() {
return Last_Name;
}
public void setLast_Name(String last_Name) {
Last_Name = last_Name;
}
public int getUser_id() {
return User_id;
}
public void setUser_id(int user_id) {
User_id = user_id;
}



}




Actual accessing database

package hibernate_example;

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

public class TestClient
{


public static void main(String[] args)
{
try{

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();

System.out.println("Inserting Record");
User _user=new User();
_user.setFirst_Name("pankaj");
_user.setLast_Name("jain");
_user.setAge(25);
_user.setEmail("pankajjain15@gmail.com");

session.save(_user);
System.out.println("Done");
}catch(Exception e)
{
System.out.println(e.getMessage());

}

}
}

User.hbm file is

<?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="hibernate_example.User" table="user_1" >
<id name="User_id" type="java.lang.Integer" column="userid" >
<generator class="sequence">
<param name="sequence">userid_seq</param>
</generator>
</id>
<property name="First_Name" type="java.lang.String" column="fname" length="20" />
<property name="Last_Name" type="java.lang.String" column="lname" length="20" />
<property name="age" type="java.lang.Integer" column="age" length="-1" />
<property name="Email" type="java.lang.String" column="email" length="20" />
</class>

</hibernate-mapping>





It showing me following output
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Inserting Record
Hibernate: select max(userid) from user_1
Done



wel it is executing prperly but when i look into database, none record is get inserted


Last edited by pankajjain15 on Fri Aug 15, 2008 10:12 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 15, 2008 10:08 am 
Newbie

Joined: Wed Aug 13, 2008 12:48 pm
Posts: 7
Location: Mérida, Yucatán, México
after the line

Code:
Session session = sessionFactory.openSession();


you must begin transaction, this for insert, update or delete rows.

Code:
session.beginTransaction();


and after the line

Code:
session.save(_user);


commit the transaction

Code:
session.getTransaction().commit();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 15, 2008 10:18 am 
Newbie

Joined: Fri Aug 15, 2008 9:09 am
Posts: 12
as per u said i hv added those lines
but it showing me following output

[b]log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into user_1 (fname, lname, age, email, userid) values (?, ?, ?, ?, ?)
Could not execute JDBC batch update[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 15, 2008 10:40 am 
Newbie

Joined: Wed Aug 13, 2008 12:48 pm
Posts: 7
Location: Mérida, Yucatán, México
mmmmm...

the first and second line mean that you are not configured the Log4j( is a log system).

the third line mean that Hibernate try to insert the new row to the db.

the fourth line mean that the engine db no could execute the query .

review your code for populate the bean, I view that the property "email" has 22 characters when the definition of the column for this property is 20


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 15, 2008 3:01 pm 
Newbie

Joined: Fri Aug 15, 2008 9:09 am
Posts: 12
thanks it is running
plz keep in touch if i need help


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 16, 2008 3:44 am 
Newbie

Joined: Fri Aug 15, 2008 9:09 am
Posts: 12
But is it compulsory to add

session.beginTransaction();
session.getTransaction().commit();

coz i hv go through a lot tutorial , but it is not mentioned any where.

Also i referred a book, but not mentioned........


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