packageorg.py.hib.relation.one2one;
importjunit.framework.Assert; importjunit.framework.TestCase;
importorg.hibernate.Session; importorg.hibernate.SessionFactory; importorg.hibernate.Transaction; importorg.hibernate.cfg.Configuration; importorg.junit.After; importorg.junit.Before;
publicclassOne2OneTestextendsTestCase { privateSessionFactoryfactory;
privateStringm_name="ryanpoy";
privateStringm_name2="ryanpoy2";
privateStringm_cardDesc1="desc_1";
privateStringm_cardDesc2="desc_2";
@Before publicvoidsetUp()throwsException { Configurationconf=newConfiguration().configure(); factory=conf.buildSessionFactory(); }
/** *测试添加 *@throwsException */ publicvoidtestSave()throwsException { System.out.println("\n===testsave===");
Cardcard=newCard(); card.setCardDesc(m_cardDesc1);
Personperson=newPerson(); person.setName(m_name);//设置用户名=m_name person.setCard(card);
Sessionsession=null; Transactiontran=null; try { session=factory.openSession(); tran=session.beginTransaction(); session.save(person);
tran.commit();
Assert.assertEquals(person.getId()!=null,true); Assert.assertEquals(card.getId()!=null,true);
}catch(Exceptionex) { tran.rollback(); throwex; }finally { if(session!=null) { try { session.close(); }catch(Exceptionex) { //nothingtodo }finally { if(session!=null) session=null; } } } }
/** *测试查询 *@throwsException */ publicvoidtestFind()throwsException { System.out.println("\n===testfind==="); Sessionsession=null; try { session=factory.openSession(); Personperson=(Person)session.createQuery("fromPerson").list().get(0);
Assert.assertEquals(true,person.getId()!=null); Assert.assertEquals(m_name,person.getName());
Assert.assertEquals(true,person.getCard().getId()!=null); Assert.assertEquals(m_cardDesc1,person.getCard().getCardDesc());
}catch(Exceptionex) { throwex; }finally { if(session!=null) { try { session.close(); }catch(Exceptionex) { //nothingtodo }finally { if(session!=null) session=null; } } } }
/** *测试修改 *@throwsException */ publicvoidtestModify()throwsException { System.out.println("\n===testmodify==="); Sessionsession=null; Transactiontran=null; try { session=factory.openSession(); tran=session.beginTransaction();
Personperson=(Person)session.createQuery("fromPerson").list().get(0); person.setName(m_name2);//修改用户名=m_name2.(原来用户名=m_name) person.getCard().setCardDesc(m_cardDesc2);//修改cardDesc为m_cardDesc2(原来是:m_cardDesc1) tran.commit();
}catch(Exceptionex) { throwex; }finally { if(session!=null) { try { session.close(); }catch(Exceptionex) { //nothingtodo }finally { if(session!=null) session=null; } } }
/* *修改后再查询 */ System.out.println("\n===testfindaftermodify==="); try { session=factory.openSession(); Personperson=(Person)session.createQuery("fromPerson").list().get(0);
Assert.assertEquals(true,person.getId()!=null); Assert.assertEquals(m_name2,person.getName());
Assert.assertEquals(true,person.getCard().getId()!=null); Assert.assertEquals(m_cardDesc2,person.getCard().getCardDesc());
}catch(Exceptionex) { throwex; }finally { if(session!=null) { try { session.close(); }catch(Exceptionex) { //nothingtodo }finally { if(session!=null) session=null; } } } }
/** *测试删除 *@throwsException */ publicvoidtestDelete()throwsException { System.out.println("\n===testdelete==="); Sessionsession=null; Transactiontran=null; try { session=factory.openSession(); tran=session.beginTransaction();
Personperson=(Person)session.createQuery("fromPerson").list().get(0); session.delete(person); tran.commit();
}catch(Exceptionex) { throwex; }finally { if(session!=null) { try { session.close(); }catch(Exceptionex) { //nothingtodo }finally { if(session!=null) session=null; } } }
/* *删除后再查询 */ System.out.println("\n===testfindafterdelete==="); try { session=factory.openSession();
Integernum=(Integer)session.createQuery("fromPerson").list().size(); Assert.assertEquals(0,num.intValue());
num=(Integer)session.createQuery("fromCard").list().size(); Assert.assertEquals(0,num.intValue());
}catch(Exceptionex) { throwex; }finally { if(session!=null) { try { session.close(); }catch(Exceptionex) { //nothingtodo }finally { if(session!=null) session=null; } } } }
/** * */ @After publicvoidtearDown()throwsException { factory.close(); }
}
|