Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

data inserting problem

 
Nareshkumar Kapilavai
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i am new to the hibernate. i am trying to do some sample application with hibernate using HSQLDatabase. i am able to retriving the data from the database. but when i am trying to save the data in to the database.. it is not inserting.i am using increment as a generator class for Id.after executing the program it is showing following information in the console..

Hibernate: select max(id) from BOOK
Hibernate: insert into BOOK (bookname, id) values (?, ?)

but in the database it is not showing the inserted data.i am not getting any exception also. when i try to retrive the data from the database using Query.list()it is retriving normally.

please see the code bellow what i did:

main class:

session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Book b1 = new Book();
b1.setLngBookId(1);
b1.setStrBookName("testData");
session.save(b1);
session.getTransaction().commit();

hbm file:

<hibernate-mapping>
<class name="test.Book" table="BOOK">
<id name="lngBookId" type="long" column="id" >
<generator class="increment"/>
</id>
<property name="strBookName">
<column name="bookname" />
</property>
</class>
</hibernate-mapping>

cfg file:

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="hibernate.connection.url">jdbc:hsqldb:file :\PAYMENTS\HSQLDB</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password"></property>
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="book.hbm.xml"/>
</session-factory>
</hibernate-configuration>




please give me the solution to my problem.

Thanks & regards!!
Naresh Kapilavai
 
Walter Bernstein
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to remove this statement:

b1.setLngBookId(1);
 
Bougnon Kipre
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not letting the mapping logic generate the next ID by setting the ID to 1 each time.
 
Nareshkumar Kapilavai
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply. i found the problem.problem is not with my code.it is something other problem with the database.when i change the database from HSQLDB to mysql it is working fine.Thank you very much.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!