Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rows not getting inserted: Hibernate

 
K Kiran Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was using the following code to insert a row in the database using Hibernate framework.

public static void main(String[] args)
{
Session session = null;
SessionFactory sf=null;
try
{
sf = new Configuration().configure().buildSessionFactory();
session =sf.openSession();

dbtable table = new dbtable();
table.setRoll(12);
table.setFirstName("ab");
table.setLastName("yz");
table.setMail("abc@gmail.com");

session.save(table);
System.out.println("Done");
}
catch(Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
finally
{
session.flush();
session.close();
}
}

Here dbtable is a java bean class with getters and setters for the columns of the table.
I am using the following in hiberbate.cfg.xml file
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbcracle:thin:@<IPaddress>:<port>:hrqa115g</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">pwd</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>

<!-- Mapping files -->
<mapping resource="dbmapping.hbm.xml"/>

</session-factory>
</hibernate-configuration>

The dbmapping.hbm.xml file has the following:
<hibernate-mapping>
<class name="a.b.dbtable" table="info">
<id name="roll" type="long" column="roll">
<generator class="native"/> </id>
<property name="firstName" column="fname" type="java.lang.String" update="true" insert="true"/>
<property name="lastName" column="lname" type="java.lang.String" update="true" insert="true"/>
<property name="mail" column="mail" type="java.lang.String" update="true" insert="true"/>
</class>
</hibernate-mapping>

When I execute the java code, I am not getting any error. After execution, when I check in the table, I get "0 rows returned".

Any help on my issue will be appreciated. Looking for your valuable response.
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you enable auto-commit, you'll need to explicitly commit the transaction.
 
K Kiran Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your suggestion Scott! That worked if I add the following in the hibernate.cfg.xml file.

<property name="hibernate.connection.autocommit">true</property>

Is there any other way to do it, I mean how can we explictly commit the transaction? Is there any separate code for it?

Regards,
Kiran.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that we have a separate forum for O/R mapping frameworks. I'll move this for you.
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any other way to do it


Yes, Hibernate.org has some information on how to manage transactions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic