为什么我的Hibernate 中getSession().save() 保存不到数据库
我的dao中怎么写的:
[code="java"]
/**
* 保存实体
*/
public void save(T o) {
getSession().persist(o);
}
[/code]
我在测试单元这样写的:
[code="java"]
@Test
public void testSave() {
ApplicationContext cxt = new ClassPathXmlApplicationContext("/applicationContext.xml");
buyerService = (BuyerService)cxt.getBean("buyerService");
Buyer buyer = new Buyer("dengmingti","123456","test@sina.com");
ContactInfo contactInfo = new ContactInfo();
contactInfo.setAddress("百色市朝阳区左家庄");
contactInfo.setMobile("13671323507");
contactInfo.setPhone("010-64469090-9");
contactInfo.setPostalcode("531500");
buyer.setContactinfo(contactInfo);
buyer.setGender("男");
buyer.setRealname("刘德华");
buyer.setRegTime(new Date());
buyerService.save(buyer);
}[/code]
在dao中写 Transaction tran=getSession().beginTransaction();
tran.commit();也没有用
如果我的测试单元这样写怎可以保存到数据库中:
[code="java"]
protected static ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/applicationContext.xml");
protected static SessionFactory sessionFactory = (SessionFactory) applicationContext.getBean("sessionFactory");
@Test
public void testSave() {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Buyer buyer = new Buyer("dengmingrun","123456","test@sina.com");
ContactInfo contactInfo = new ContactInfo();
contactInfo.setAddress("北海市朝阳区左家庄");
contactInfo.setMobile("13671323507");
contactInfo.setPhone("010-64469090-9");
contactInfo.setPostalcode("533000");
buyer.setContactinfo(contactInfo);
buyer.setGender("男");
buyer.setRealname("张学友");
buyer.setRegTime(new Date());
session.save(buyer);
tx.commit();
session.close();
}[/code]
我怎样才能使用buyerService.save(buyer);存到数据库中呀?