Wednesday, December 23, 2009

Basic Hibernate APIs

      // configure will look for mapping & settings in an application     

      // resource file named hibernate.cfg.xml

      SessionFactory sessionFactory =

            new Configuration().configure().buildSessionFactory();

      Session session = sessionFactory.getCurrentSession();

      try

      {

            session.beginTransaction();

           

            Event testEvent = new Event();

            testEvent.setTitle("testTitle");

           

            session.save(testEvent);

            // Commit if all goes well

            session.getTransaction().commit();

      }

      catch (Exception e )

      {

            // rollback if something fails

            session.getTransaction().rollback();

      }

 

No comments: