-->
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.  [ 8 posts ] 
Author Message
 Post subject: Could not save object
PostPosted: Sat Sep 30, 2006 3:25 am 
Newbie

Joined: Sat Sep 30, 2006 3:12 am
Posts: 4
Hello There,
I am new to Hibernate and I am unable to insert any data into DB

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

<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/products</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<mapping resource="mypkg/book.hbm.xml"/>
</session-factory>
</hibernate-configuration>
-----------------------------------------------------------------------------
[book.hbm.xml]
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name = "mypkg.Book" table = "books">
<id name="id" type = "int" unsaved-value="0">
<generator class = "hilo" />
</id>

<property name="title"/>
<property name="author"/>
<property name="isbn" not-null="true"/>
<property name="pages" type="integer" column="pagecount" />
<property name="copyright"/>
<property name="cost">
<column name="cost" sql-type="NUMERIC(12,2)"/>
</property>
</class>
</hibernate-mapping>
-----------------------------------------------------------------------------
[ mypkg/Book.java ]
public class Book
{
private int id;
private String title;
private String author;
private String isbn;
private int pages;
private int copyright;
private float cost;

// All Setter and Getter
}
-----------------------------------------------------------------------------
[ BookTest.java ]
package mypkg;

import java.io.*;
import net.sf.hibernate.*;

public class BookTest
{
public static void main(String [] args)
{
try
{
Session session = HibernateSession.currentSession();
Book book = new Book();
book.setTitle("Learning Hibernate");
book.setIsbn("123469");
book.setAuthor("Amp");
book.setPages(500);
book.setCost(30.22f);

session.save(book);
session.flush();

session.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
-----------------------------------------------------------------------------
[ HibernateSession.java ]
package mypkg;

import net. sf.hibernate.*;
import net. sf.hibernate.cfg.*;

public class HibernateSession {
private static final SessionFactory sessionFactory;

static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException e) {
throw new RuntimeException("SessionFactory Error - " + e.getMessage(), e);
}
}

public static final ThreadLocal session = new ThreadLocal();

public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();

if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}

return s;
}

public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session. set(null);
if (s != null)
s.close();
}
}

------------------------------------------------------------------------------------
Here it is what I have in my whole program under "mypkg" package. I am using Hibernate2.1, MySQL4.1 and also I have all required class/jar files in my classpath, since I am working with Eclipse3.1.

I am getting only this message:

Hibernate: insert into books
(title, author, isbn, pagecount, copyright, cost) values (?, ?, ?, ?, ?, ?)

No Data at all !

Please help me in this context. Thanks in Advance.

_________________
Amp


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 30, 2006 9:11 am 
Regular
Regular

Joined: Wed Jul 27, 2005 2:33 am
Posts: 118
Did you check your database table to see whether the data is inserted? HIbernate uses prepared statement to fire queries. Hibernate replaces the '?' with appropriate values while firing the query to database.


Top
 Profile  
 
 Post subject: Could not save object
PostPosted: Sat Sep 30, 2006 9:22 am 
Newbie

Joined: Sat Sep 30, 2006 3:12 am
Posts: 4
Hi JaiKiran,

Thanks for replying too early.

Actually I gone through my table in my database, and there is no any data which I have inserted through programs.

I think there must be something different is going there.

Help !!!

Thanks!

_________________
Amp


Top
 Profile  
 
 Post subject: Could not save object
PostPosted: Sat Sep 30, 2006 9:22 am 
Newbie

Joined: Sat Sep 30, 2006 3:12 am
Posts: 4
Hi JaiKiran,

Thanks for replying too early.

Actually I gone through my table in my database, and there is no any data which I have inserted through program.

I think there must be something different is going there.

Help !!!

Thanks!

_________________
Amp


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 30, 2006 9:32 am 
Regular
Regular

Joined: Wed Jul 27, 2005 2:33 am
Posts: 118
Do you see any exceptions in the log files? If yes, then post the exception stacktrace.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 30, 2006 9:34 am 
Regular
Regular

Joined: Wed Jul 27, 2005 2:33 am
Posts: 118
Also have a look at the sample pseudo-code:
Code:
Session sess = factory.openSession();
Transaction tx;
try {
     tx = sess.beginTransaction();
     //do some work
     ...
     tx.commit();
}
catch (Exception e) {
     if (tx!=null) tx.rollback();
     throw e;
}
finally {
     sess.close();
}


mentioned at:

http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 01, 2006 3:56 pm 
Beginner
Beginner

Joined: Tue Sep 26, 2006 11:46 pm
Posts: 33
I suspect the problem is you're missing the transaction. Hibernate generally doesn't persist anything unless you start and commit a transaction as the sample code posted above does.


Top
 Profile  
 
 Post subject: Thanks A Lot
PostPosted: Tue Oct 03, 2006 3:31 am 
Newbie

Joined: Sat Sep 30, 2006 3:12 am
Posts: 4
Thanks Jaikiran and Edc.
Sorry for replying too late. Your (Both of U) solution is really working for me.
Thanks a Lot again !!!

_________________
Amp


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