%BROADCASTMESSAGE%
CAAD | ARCH TWiki > Catch0405 > KaisersrotPersistenz > SoftwareVersions > TestExampleWithHibernate21 TWiki webs:
Admin| Aizo | Archinf | BIP | Bgyalex | Brandhub | Catch0405 | Cityscan05 | CommunityMedia | Control | Curtain | Easa005 | Easydb | Education | Extern | Extern0405 | Freudenhaus | Fund | Game0405 | Game05 | Gamearch | Luise | MAS0506 | MAS0506stu | Mas0506 | Mas0506stu | Maschinen0405 | Menz | Mill | Mill0405 | NDS | NDS0405 | NDS0405stu | Plugins | Qwipp | RZM | Replay | Replay0405 | Research | Second | Trash | Udintern | Urbandesign |
Catch0405 . { Changes | Index | Search | Go }

import java.util.Iterator;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;


/*---------------------------------------------------------------------------
 * running inserting pgm with hibernate 2.1
 *----------------------------------------------------------------------------/

/**
 * @author denis
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class HibernateTest2 {

   public static void main(String[] args) {
      HibernateTest2 hibernateTest1 = new HibernateTest2();
   }

   public HibernateTest2() {
      this.store();
      //this.select();
   }

/*------------------------------   
 * store data to db   
 *------------------------------*/   
   
      void store (){
            
         try {
            
//              Create the SessionFactory
               Configuration cfg = new Configuration()
                      .addClass(User1.class);                                             
            SessionFactory factory = cfg.buildSessionFactory();
//            Create the Session
               Session session = factory.openSession();
//              Create the Transaction               
            Transaction tx = session.beginTransaction();

            //Create new User and store them the database
            User1 newUser1 = new User1();
            //newUser1.setID("dr");
            newUser1.setUserName("pia5 raschpichler");

            session.save(newUser1);
            tx.commit();

            // close our session and release resources
            session.flush();
            session.close();
         } catch (HibernateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }      
   }
      
      
/*----------------------------------   
 * select data from db   
 *----------------------------------*/
      void select() {

      try {
//          Create the SessionFactory
            Configuration cfg = new Configuration()
                  .addClass(User1.class);                                             
         SessionFactory factory = cfg.buildSessionFactory();
//         Create the Session
            Session session = factory.openSession();
//          Create the Transaction               
         Transaction tx = session.beginTransaction();

         Query query = session
               .createQuery("select c from User1 as c where name = :name");
         
         query.setString("name", "pia raschpichler");
         for (Iterator it = query.iterate(); it.hasNext();) {
            User1 user1 = (User1) it.next();
             System.out.println("searched name is: " + user1.getUserName());
         }

         tx.commit();

      } catch (HibernateException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }

   }
      
}

-- OtherDenisRaschpichler - 06 Dec 2004


Topic TestExampleWithHibernate21 . { Edit | Attach | Ref-By | Printable | Diffs | r1.2 | > | r1.1 | More }
Revision r1.2 - 06 Dec 2004 - 09:00 - TorstenSpindler
Parents: WebHome > KaisersrotPersistenz > SoftwareVersions

Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.

This website has been archived and is no longer maintained.