Results 1 to 5 of 5

Thread: Hibernate delete query using criterion

  1. #1
    Join Date
    May 2008
    Posts
    28

    Hibernate delete query using criterion

    Hello,

    I need to see how can I delete an object in Hibernate? I mean an example to delete from database in Hibernate. Delete query criterion is required.

    Thanks.

  2. #2
    Join Date
    Apr 2008
    Posts
    45

    Re: Hibernate delete query using criterion

    Try this:

    Code:
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    
    public class DeleteHQLExample {
      /**
     * 
     Criteria Query Example
     *  
     */
      public static void main(String[] args) {
        // TODO Auto-generated method stub  
        Session sess = null;
        try {
          SessionFactory fact = new 
    Configuration().configure().buildSessionFactory();
          sess = fact.openSession();
          String hql = "delete from 
    Insurance insurance where id = 2";
          Query query = sess.createQuery(hql);
          int row = query.executeUpdate();
          if (row == 0){
            System.out.println("Doesn'
    t deleted any row!");
          }
          else{
            System.out.println("Deleted
     Row: " + row);
          }
          sess.close();
        }
        catch(Exception e){
          System.out.println(e.getMessage());
        }
      }
    }

  3. #3
    Join Date
    May 2008
    Posts
    26

    Re: Hibernate delete query using criterion

    Hey have a look at this:
    example:

    Code:
    import java.util.Date;
     
    import org.hibernate.Session;
    
    public class LabelManager {
            private void deleteLabel(Long id) {
            /*
             * Load the object to be deleted
             */
            Label label = getLabel(id);
             
            /*
             * We get the current session and delete the Label object from database.
             */
            Session session = SessionFactoryHelper.getSessionFactory().getCurrentSession();
            session.beginTransaction();   
            session.delete(label);
            session.getTransaction().commit();
        }
     
        private Label getLabel(Long id) {
            Session session = SessionFactoryHelper.getSessionFactory().getCurrentSession();
             
            session.beginTransaction();
             
            /*
             * We get back Label object from database by calling the Session object
             * get() method and passing the object type and the object id to be
             * read.
             */
            Label label = (Label) session.get(Label.class, id);
            session.getTransaction().commit();
             
            return label;
        }
     
        private void saveLabel(Label label) {
            /*
             * To save an object we first get a session by calling getCurrentSession()
             * method from the SessionFactoryHelper class. Next we create a new
             * transcation, save the Label object and commit it to database,
             */
            Session session = SessionFactoryHelper.getSessionFactory().getCurrentSession();
             
            session.beginTransaction();       
            session.save(label);       
            session.getTransaction().commit();
        }
     
        public static void main(String[] args) {       
            LabelManager manager = new LabelManager();
     
            /*
             * Creates a Label object we are going to stored in the database. We
             * set the name, modified by and modified date information.
             */
            Label label = new Label();
           label.setName("Sony Music");
            label.setModifiedBy("admin");
            label.setModifiedDate(new Date());
             
            /*
             * Call the LabelManager saveLabel method.
             */
            manager.saveLabel(label);
             
            /*
             * Read the object back from database.
             */
            label = manager.getLabel(label.getId());
            System.out.println("Label = " + label);
             
            manager.deleteLabel(label.getId());
        }
    }

  4. #4
    Join Date
    May 2008
    Posts
    28

    Re: Hibernate delete query using criterion

    Thanks for the quick reply.
    I am trying the suggestion right away.

  5. #5
    Join Date
    Jun 2010
    Posts
    1
    i have run the similar code but it has given an out put of null and delete operation is not reflected to database too

    here is my code

    package roseindia.tutorial.hibernate;

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

    public class DeleteExample {

    public static void main(String[] args) {

    Session session=null;
    try
    {
    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    //System.out.println("SessionFactory IS:"+sessionFactory);
    session =sessionFactory.openSession();
    // System.out.println("Session IS:"+session);
    String hql="delete from Contact where id=5";
    //System.out.println("HQL IS:"+hql);
    Query query=session.createQuery(hql);
    int row=query.executeUpate();

    //System.out.println(row);
    if(row==0)
    {
    System.out.println("fail to perform operation");
    }
    else
    {
    System.out.println(row+"row deleted sucessfully");
    }
    session.close();
    }catch(Exception e)
    {
    System.out.println(e.getMessage());

    }

    }

    }
    can any one help greatly appriciated

Similar Threads

  1. Replies: 20
    Last Post: 21-08-2010, 03:55 PM
  2. Delete objects in the collections with Hibernate
    By SoftWore in forum Tips & Tweaks
    Replies: 1
    Last Post: 23-07-2010, 01:31 PM
  3. Hibernate dynamic update query
    By GianCarlo in forum Software Development
    Replies: 3
    Last Post: 01-08-2009, 08:47 PM
  4. SQL query to delete a row in database
    By Connect_Me in forum Software Development
    Replies: 3
    Last Post: 05-05-2009, 01:45 PM
  5. Replies: 2
    Last Post: 24-04-2009, 11:13 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,713,489,979.66982 seconds with 17 queries