package com.bloggers.model;
import java.util.Date;
public class Book
{
/* for primary key in database table */
private int bookId;
private String bookName;
private Date date;
public int getBookId()
{
return bookId;
}
public void setBookId(int bookId)
{
this.bookId = bookId;
}
public String getBookName()
{
return bookName;
}
public void setBookName(String bookName)
{
this.bookName = bookName;
}
public Date getDate()
{
return date;
}
public void setDate(Date date)
{
this.date = date;
}
}
<hibernate -mapping="-mapping"> [...] </hibernate>Note:
<?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="com.bloggers.model.Book" >
</class>
</hibernate-mapping>
<?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="com.bloggers.model.Book" >
<id name="bookId"/>
<property name="date" column="PUBLISH_DATE"/>
</class>
</hibernate-mapping>
name="bookId" attribute of the property element tells Hibernate which getter and setter methods to use in our case hibernate will search for getBookId() , setBookId() etc methods. <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernatedb</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property> <mapping resource="com/bloggers/model/Book.hbm.xml"/> </session-factory> </hibernate-configuration>
package com.bloggers.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil
{
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory()
{
try
{
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
}
catch (Throwable ex)
{
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
}
package com.bloggers;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.bloggers.model.Book;
import com.bloggers.util.HibernateUtil;
public class HibernateTest
{
public static void main(String[] args)
{
HibernateTest hibernateTest = new HibernateTest();
/* creating a new book object */
Book book = new Book();
book.setBookId(1);
book.setDate(new Date());
book.setBookName("Hibernate in action");
/* saving the book object */
hibernateTest.saveBook(book);
}
public void saveBook(Book book)
{
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
/* saving the book object here */
session.save(book);
session.getTransaction().commit();
}
}

Labels: Hibernate