-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Update of a set
PostPosted: Wed Jul 09, 2008 6:20 am 
Newbie

Joined: Wed Jul 02, 2008 8:10 am
Posts: 2
I need to make an update of a set of records in database with Hibernate3 ... somebody knows any way to do it than with Hql or JDBC?

Thansk to all


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 09, 2008 5:02 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
With Hibernate, it's as simple as loading a set of records and then manipulating the data:

Code:
  public static void updateAll() {
    AnnotationConfiguration config =
                             new AnnotationConfiguration();
    config.addAnnotatedClass(User.class);
    SessionFactory factory;
    factory = config.configure().buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();

    java.util.List allUsers;
    System.out.println("Updating all records...");
    Query queryResult = session.createQuery("from User");
    allUsers = queryResult.list();
    System.out.println("# of rows:"+allUsers.size());

    for (int i = 0; i < allUsers.size(); i++) {
      User user = (User) allUsers.get(i);
      System.out.println(user);
      user.setPassword("password");
      session.update(user);
    }

    System.out.println("Database table updated...");
    session.getTransaction().commit();
  }


If you're doing many records, you might want to look at ETL tools as perhaps a better solution. Just straight batch isn't always best performed with Hibernate.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.