Hibernate

In the below exercises, i use Mysql for database. There are four classes for four different actions.
I used MyEclipse 6.6 for these examples.

  1. Open MyEclipse / New / Project. Choose Java Project.
  2. After creating your new project by right clicking the project name in the package explorer, choose MyEclipse / Add Hibernate Capabilities. You will see:
  3. Hibernate

  4. Without changing default settings, click the Next button.
  5. Hibernate

  6. Write your connection string, username and password here,:
  7. Hibernate
    then click Next.

  8. And in the last part of the configuration, write new Java Package for your classes by clicking the New button.
  9. Hibernate

I have a table called Users and four columns in it. These columns’ names are Firstname, Lastname, Email and Password. Before you do something with Hibernate, let’s change our perspective by clicking Window/Open Perspective/MyEclipse Hibernate. When the DB Browser is shown in the scene, right click your table name, select Hibernate Reverse Engineering.

  1. Listing:
    Create a new Java class by right clicking your package, which you created few steps before, in the Package Explorer. Choose create method static void main and give your class a name.
  2. Hibernate
    Copy and paste this code to your new class:
    import java.util.Iterator;
    import java.util.List;

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

    public class UsersList {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    SessionFactory sessionFact = new Configuration().configure().buildSessionFactory();
    Session session = sessionFact.openSession();
    Query query = session.createQuery(”from Users”);
    List users = query.list();
    for (Users users2 : users) {
    System.out.println(users2.getFirstname());
    }
    }

    }

  3. Inserting:
    Create a new class for insert new data to your database
  4. import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.classic.Session;

    public class UserAdd {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    SessionFactory sessionFact = new Configuration().configure().buildSessionFactory();
    Session ses = sessionFact.openSession();
    Transaction trans = ses.beginTransaction();
    Users user = new Users();
    user.setFirstname(”John”);
    user.setLastname(”Locke”);
    user.setEmail(”jj@c.com”);
    user.setPassword(”john”);
    ses.save(user);
    trans.commit();
    ses.flush();
    ses.close();

    }

    }

  5. Updating:
  6. import java.util.Iterator;
    import java.util.List;

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

    public class UserUpdate {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    SessionFactory sessionFact = new Configuration().configure().buildSessionFactory();
    Session session = sessionFact.openSession();
    Transaction trans = session.beginTransaction();
    Users user = (Users) session.get(Users.class, new Integer(5));
    user.setFirstname(”x”);
    session.update(user);
    trans.commit();
    session.close();
    System.out.println(”İslem basarıyla tamamlandı.”);

    }

    }

  7. Deleting:
  8. import org.hibernate.Query;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.classic.Session;

    public class UserDelete {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Session ses = new Configuration().configure().buildSessionFactory().openSession();
    Transaction trans = ses.beginTransaction();
    String hql = “delete Users where id = 5″;
    Query query = ses.createQuery(hql);
    int rowCnt = query.executeUpdate();
    trans.commit();
    System.out.println(rowCnt);

    }

    }


P.S.: Right click your classes and choose Run As / Java Application to run.