Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , ,

Sponsored Links



Update database with Hibernate

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 10-12-2009
Member
 
Join Date: Nov 2008
Posts: 68
Update database with Hibernate

Hi, I want to know how to use the hibernate to update the database. So, please if anyone is having the details about it, please forward those to me. I want to know in detail about the hibernate, so, please give me the code regarding this. I have a book regarding this, but it is not possible to know exact details about it. So, please forward me the details about it.
Reply With Quote
  #2  
Old 10-12-2009
Zecho's Avatar
Member
 
Join Date: May 2008
Posts: 2,267
Re: Update database with Hibernate

Hello, it is a quiet new concept to me. I had never heard about this term Hibernate. In which language this term is used. I have searched on internet, but not find any details about it. Will you please give me some information from which I can come to know about the concept of hibernate. So, please reply me and give the details about the hibernate. I am waiting for the reply.
Reply With Quote
  #3  
Old 10-12-2009
Reegan's Avatar
Member
 
Join Date: Oct 2005
Posts: 2,299
Re: Update database with Hibernate

I don't have knowledge about the Hibernate but while searching on internet about it, I have got something. Look at the below code whether it is helpful to you or not.

Code:
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Update
 {
	public static void main(String[] args) 
	{
		Session session= null;
		try 
		{
			SessionFactory sessionfact = new Configuration().configure().buildSessionFactory();
			session= sessionfact.openSession();
			Transaction tran = sess.beginTransaction();
			Insurance insurance = (Insurance)sess.get(Insurance.class, new Long(1));
			insurance.setInsuranceName("Jivan Dhara");
			insurance.setInvestementAmount(20000);
			insurance.setInvestementDate(new Date());
			sess.update(insurance);
			tran.commit();
			sess.close();
			System.out.println("Update successfully!");
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
	}
}
Reply With Quote
  #4  
Old 10-12-2009
opaper's Avatar
Member
 
Join Date: May 2008
Posts: 2,362
Re: Update database with Hibernate

Hello, I don't have much more knowledge about the Hibernate. I also have basic knowledge about the hibernate. I am giving you the basic code which will help you to improve your knowledge about the Hibernate. Just go through it and understand logic about it.

Code:
import java.util.*;
import java.sql.*;
import org.hibernate.*;
import org.hibernate.criterion.*;

public class Main 
{
 public static void main(String[] args) 
{
 HibernateUtil.setup("create table Employee ( id int, name VARCHAR);");
 HibernateUtil.setup("create table Department ( id int, name VARCHAR, description VARCHAR, total double,branchid int);");
 prepareData();
 Session session = HibernateUtil.currentSession();
 String sql = "select {branch.*} from Employee branch";
 String hql = "update Employee set name = :newName where name = :name";
 Query query = session.createQuery(hql);
 query.setString("name","Employee Name 1");
 query.setString("newName","s1");
 int rowCount = query.executeUpdate();
 System.out.println("Rows affected: " + rowCount);
  query = session.createQuery("from Employee");
  List results = query.list();
  displayEmployeeList(results);
  HibernateUtil.checkData("select * from Employee");
  HibernateUtil.checkData("select * from Department");
  }

 static public void displayEmployeeList(List list)
 {
  Iterator iter = list.iterator();
  if (!iter.hasNext()) 
{
System.out.println("No branchs to display.");
return;
 }  
  while (iter.hasNext()) 
{
Employee branch = (Employee) iter.next();
String msg = branch.getName();
System.out.println(msg);
 }
 }

  private static void prepareData()
{
  Session session = HibernateUtil.currentSession();

  Employee branch1 = new Employee();
  branch1.setName("Employee Name 1");
  session.save(branch1);
  
  Employee branch2 = new Employee();
  branch2.setName("Employee Name 2");
  session.save(branch2);  
  
  Department product1 = new Department("Department 1","Name for Department 1", 2.0);
  product1.setEmployee(branch1);
  branch1.getDepartments().add(product1);
  session.save(product1);
  
  Department product12 = new Department("Department 2","Name for Department 2", 22.0);
  product12.setEmployee(branch1);
  branch1.getDepartments().add(product12);  
  session.save(product12);
  
  Department product2 = new Department("Department 3", "Name for Department 3", 30.0);
  product2.setEmployee(branch2);
  branch2.getDepartments().add(product2);
  session.save(product2);
  
  session.flush();
  HibernateUtil.closeSession();
  }
}

public class Department
{
 private int id;
 private Employee branch;
 
 private String name;
 private String description;
 private double total;
 
 public Department()
 {
  super();
 }
 
 public Department(String name, String description, double total)
 {
  super();
  this.name = name;
  this.description = description;
  this.total = total;
 }
 
 public String getDescription()
 {
  return description;
 }
 public void setDescription(String description)
 {
  this.description = description;
 }
 public int getId()
 {
  return id;
 }
 public void setId(int id)
 {
  this.id = id;
 }
 public String getName()
 {
  return name;
 }
 public void setName(String name)
 {
  this.name = name;
 }
 
 public Employee getEmployee()
 {
  return branch;
 }
 public void setEmployee(Employee branch)
 {
  this.branch = branch;
 }
 
 public double getPrice()
 {
  return total;
 }
 public void setPrice(double total)
 {
  this.total = total;
 }
}
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Update database with Hibernate"
Thread Thread Starter Forum Replies Last Post
SpywareBlaster Database Update | Oct 16 | 2010 siljaline AntiVirus Software 4 18-10-2010 02:51 AM
DataBase update failure Santiaago Networking & Security 5 10-04-2010 04:16 PM
How to update Database with Changes made to the DataGridView afidelino Software Development 3 25-09-2009 09:41 AM
How to update two database tables coldfusion Rixwel Software Development 3 10-08-2009 04:13 PM
Hibernate dynamic update query GianCarlo Software Development 3 01-08-2009 09:47 PM


All times are GMT +5.5. The time now is 07:38 AM.