Results 1 to 4 of 4

Thread: Update database with Hibernate

  1. #1
    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.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    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.

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    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());
    		}
    	}
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    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;
     }
    }

Similar Threads

  1. DataBase update failure
    By Santiaago in forum Networking & Security
    Replies: 5
    Last Post: 10-04-2010, 03:16 PM
  2. Unable to update my database using insert command
    By Govardhann in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 11:10 AM
  3. How to update Database with Changes made to the DataGridView
    By afidelino in forum Software Development
    Replies: 3
    Last Post: 25-09-2009, 08:41 AM
  4. How to update two database tables coldfusion
    By Rixwel in forum Software Development
    Replies: 3
    Last Post: 10-08-2009, 03:13 PM
  5. Hibernate dynamic update query
    By GianCarlo in forum Software Development
    Replies: 3
    Last Post: 01-08-2009, 08:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,727,104,764.32231 seconds with 17 queries