Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Rows not getting inserted: Hibernate

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41945
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is there any other way to do it



Yes, Hibernate.org has some information on how to manage transactions.
 
Normally trees don't drive trucks. Does this tiny ad have a license?
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic