Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Hibernate Update Query 
 

In this tutorial we will show how to update a row with new information by retrieving data from the underlying database using the hibernate.

 

Hibernate Update Query

                         

In this tutorial we will show how to update a row with new information by retrieving data from the underlying database using the hibernate. Lets first write a java class to update a row to the database.

Create a java class:
Here is the code of our java file (UpdateExample.java), where we will update a field name "InsuranceName" with a value="Jivan Dhara" from a row of the insurance table.

Here is the code of delete query: UpdateExample .java 

package roseindia.tutorial.hibernate;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class UpdateExample {
  /**
   @param args
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Session sess = null;
    try {
      SessionFactory fact = new Configuration()
.configure().buildSessionFactory();
      sess = fact.openSession();
      Transaction tr = sess.beginTransaction();
      Insurance ins = (Insurance)sess.get
(
Insurance.class, new Long(1));
      ins.setInsuranceName("Jivan Dhara");
      ins.setInvestementAmount(20000);
      ins.setInvestementDate(new Date());
      sess.update(ins);
      tr.commit();
      sess.close();
      System.out.println("Update successfully!");
    }
    catch(Exception e){
      System.out.println(e.getMessage());
    }
  }
}

Download this code.

Output:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).

log4j:WARN Please initialize the log4j system properly.

Hibernate: select insurance0_.ID as ID0_0_, insurance0_.insurance_name as insurance2_0_0_, insurance0_.invested_amount as invested3_0_0_, insurance0_.investement_date as investem4_0_0_ from insurance insurance0_ where insurance0_.ID=?

Hibernate: update insurance set insurance_name=?, invested_amount=?, investement_date=? where ID=?

Update successfully!

                         

» View all related tutorials
Related Tags: sql c hibernate api com class ant comparison import io property method sed const operators expression operator port criteria name

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

12 comments so far (
post your own) View All Comments Latest 10 Comments:

This is the most valueble tutorial but it is not clear....

Posted by Dil on Thursday, 12.4.08 @ 02:08am | #82365

how to update already existing child object when parent object changed... (This is one to many relations)

Posted by Sridhar on Saturday, 09.20.08 @ 16:41pm | #80568

I always use HQL in Hql editer for testing perpeses. But i want to know, How to use this editor in our Hibernate applicaions.

Posted by manjula on Wednesday, 05.14.08 @ 11:23am | #59770

Hi,
i am using Hibernate Query Language (hql). i want to change the date format while inserting and selecting. Date format conversion are DataBase dependent. So u help me out in finding any Date format conversion function which will be dataBase independent.

Thanks,
Akhilesh

Posted by Akhilesh on Monday, 05.5.08 @ 14:49pm | #58530

SessionFactory fact = new Configuration()
.configure().buildSessionFactory();
sess = fact.openSession();
Transaction tr = sess.beginTransaction();
Insurance ins = (Insurance)sess.get
(Insurance.class, new Long(1));

Posted by venkata naidu on Saturday, 02.9.08 @ 15:54pm | #47675

Hi,

I can not run this example it show me the warning like WARNING: /hibernate.cfg.xml not found

Posted by sivakumar on Wednesday, 01.2.08 @ 12:13pm | #44359

i'm not getting any error. still the value is not getting inserted inside database. The netbeans log is attached.
init:
deps-jar:
compile-single:
run-single:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Inserting Record
Done
Hibernate: insert into CONTACT (FIRSTNAME, LASTNAME, EMAIL, ID) values (?, ?, ?, ?)
BUILD SUCCESSFUL (total time: 1 second)

plz explain where to configure the log4j. and who creates the stated query. and how can i get the reference of values that is passed inside the query.

looking for your great suuport.

Posted by amit kumar on Monday, 12.31.07 @ 19:21pm | #44282

Can someone explain me this line
Insurance ins = (Insurance)sess.get(Insurance.class, new Long(1));

Posted by ali on Thursday, 11.15.07 @ 19:02pm | #37506

How do we update two rows or more than one row at a time ?

Posted by DHAR on Tuesday, 11.13.07 @ 14:20pm | #37219

Hi ,

Its not working for me. On running code its giving me exception following is the output:-

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?
exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of roseindia.tutorial.hibernate.Insurance.setInvestementAmount

Also if we get instance then why ins.getInvestementAmount() is not giving me anything.

Thanks & Regards



Posted by nature on Thursday, 11.8.07 @ 11:58am | #36560

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.