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
|