Общее·количество·просмотров·страницы

пятница, 1 июня 2012 г.

Тесты проходят, хибер работает, круды делаются

package org.codestream.perseus.core.admin.test;

import org.codestream.perseus.core.admin.domain.Admin;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

public class AdminDAOTest {

 @Test
 public void addAdmin() {
  Session session = null;
  try {
   session = HibernateUtil.currentSession();
   Transaction tx = session.beginTransaction();
   Admin admin = new Admin();
   admin.setLogin("Karl");
   admin.setEmail("chuck@norris.com");
   admin.setPassword("111");
   session.save(admin);
   System.out.println("Successfully data insert in database");
   tx.commit();
  }
  catch(Throwable t){
   System.err.println("Got an exception" + t.toString());
  }
  finally {
   session.close();
  }
 }

 @SuppressWarnings("deprecation")
 @Test
 public void updateAdmin(){
  Session session = null;
  try {
   SessionFactory sf = new Configuration().configure().buildSessionFactory();
   session = sf.openSession();
   Transaction tx = session.beginTransaction();
   Admin admin = (Admin)session.get(Admin.class, new Integer(1));
   admin.setLogin("Yed");
   admin.setLogin("Mike");
   admin.setEmail("chuck@norris.com");
   admin.setEmail("gmail@norris.com");
   session.update(admin);
   tx.commit();
   session.close();
   System.out.println("Update successfully!");
  }
  catch(Throwable t){
   System.out.println("Got an exception" + t.toString());
  }
 }

 @SuppressWarnings("deprecation")
 @Test
 public void deleteAdmin(){
  Session s = null;
  try {
   SessionFactory sf = new Configuration().configure().buildSessionFactory();
   s = sf.openSession();
   Transaction tx = s.beginTransaction();
   Admin admin = (Admin)s.get(Admin.class, new Integer(1));
   admin.setId(1);
   s.delete(admin);
   tx.commit();
   s.close();
   System.out.println("Delete successfully!");
  }
  catch(Throwable t){
   System.out.println("Got an exception" + t.toString());
  }
 }

 @Test
 public void selectAllAdmins(){
  Transaction tx = null;
  Session s = null;
  try {
   SessionFactory sf = new Configuration().configure().buildSessionFactory();
   s = sf.openSession();
   tx = s.beginTransaction();
   Admin a = new Admin();
   Admin admin = (Admin) s.get(Admin.class, a.getId());
   System.out.println("Email :" + admin.getEmail());
   Admin adm = (Admin)s.get(Admin.class, a.getId());
   System.out.println("Login :" + adm.getLogin());
   Admin _adm = (Admin)s.get(Admin.class, a.getId());
   System.out.println("Login :" + _adm.getPassword());
   tx.commit();
   tx = null;
  }
  catch(Exception exc){
   if(tx != null){
    tx.rollback();
   }
   exc.printStackTrace();
  }
  HibernateUtil.checkData("SELECT * FROM tbl_admin" );
 }

 @Test
 public void selectAdminByID(){
  Transaction tx = null;
  Session s = null;
  try {
   SessionFactory sf = new Configuration().configure().buildSessionFactory();
   s = sf.openSession();
   tx = s.beginTransaction();
   Admin a = new Admin();
   Admin admin = (Admin) s.get(Admin.class, a.getId());
   System.out.println("Email :" + admin.getEmail());
   Admin adm = (Admin)s.get(Admin.class, a.getId());
   System.out.println("Login :" + adm.getLogin());
   Admin _adm = (Admin)s.get(Admin.class, a.getId());
   System.out.println("Login :" + _adm.getPassword());
   tx.commit();
   tx = null;
  }
  catch(Exception exc){
   if(tx != null){
    tx.rollback();
   }
   exc.printStackTrace();
  }
  HibernateUtil.checkData("SELECT * FROM tbl_admin WHERE id = 5" );
 }

}
Вот так можно слепить круды при помощи хибернейта