- This topic has 1 reply, 2 voices, and was last updated 16 years, 7 months ago by Loyal Water.
-
AuthorPosts
-
usha12MemberHi,
Iam able to retrive the data from the table in mysql but unable to insert a record.
1)POJO is Book.java
package com.hibernate.bookproject;public class Book extends AbstractBook implements java.io.Serializable {
public Book() {
}
public Book(Integer id) {
super(id);
}
public Book(Integer id, String bookname) {
super(id, bookname);
}
}2) Book.hbm.xml
<?xml version=”1.0″ encoding=”utf-8″ ?>
<!DOCTYPE hibernate-mapping (View Source for full doctype…)>
– <!–
Mapping file autogenerated by MyEclipse Persistence Tools
–>
– <hibernate-mapping default-cascade=”none” default-access=”property” default-lazy=”true” auto-import=”true”>
– <class name=”com.hibernate.bookproject.Book” table=”book” catalog=”test” mutable=”true” polymorphism=”implicit” dynamic-update=”false” dynamic-insert=”false” select-before-update=”false” optimistic-lock=”version”>
– <id name=”id” type=”java.lang.Integer”>
<column name=”id” />
<generator class=”increment” />
</id>
– <property name=”bookname” type=”java.lang.String” unique=”false” optimistic-lock=”true” lazy=”false” generated=”never”>
<column name=”bookname” length=”50″ />
</property>
</class>
</hibernate-mapping>3)BookTest.java
package com.hibernate.bookproject;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.*;
public class BookTest{
public static void main(String[] args) {
Session session=null;
try
{ SessionFactory sessionfactory=new Configuration().configure().buildSessionFactory();session=sessionfactory.openSession();
org.hibernate.Transaction tx=session.beginTransaction();System.out.println(“Inserting a book object into the database.”);
Book book=new Book();
BookDAO bdao=new BookDAO();
book.setBookname(“Hibernate tutorials”);
bdao.save(book);
System.out.println(“Book object persisted to database”);
tx.commit();
session.flush();
session.close();
//bdao.getSession().close();
}catch(Exception e)
{
System.out.println(e.getMessage());
}}
}
And save method in BookDAO.java is like this:
public class BookDAO extends BaseHibernateDAO {
private static final Log log = LogFactory.getLog(BookDAO.class);
public static final String BOOKNAME = “bookname”;public void save(Book transientInstance) {
log.debug(“saving Book instance”);
try {
getSession().save(transientInstance);
log.debug(“save successful”);
} catch (RuntimeException re) {
log.error(“save failed”, re);
throw re;
}
}After running the BookTest.java the println statements r getting executed. When i checked in database no records r getting inserted.
Y iam unable to insert the book name “Hibernate tutorials” into book table. And id is not getting incremented automatically. What is my mistake?Help me please.
Regards
usha.
Loyal WaterMemberSounds like a transaction management issue. Please refer to this post:-
https://www.genuitec.com/forums/topic/hibernate-spring-tutorial-on-latest-myeclipse-no-data/ -
AuthorPosts