📄 testhibernatedao.java
字号:
package com.manning.hq.ch07;
import junit.framework.TestCase;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.HibernateCallback;
import java.util.List;
import java.util.Iterator;
import java.sql.SQLException;
/**
*
*/
public class TestHibernateDao extends TestCase {
private SessionFactory sessionFactory;
private HibernateTemplate template;
public TestHibernateDao() {
}
protected void setUp() throws Exception {
super.setUp();
HibernateFactory.buildSessionFactory();
sessionFactory = HibernateFactory.getSessionFactory();
template = new HibernateTemplate(sessionFactory);
}
protected void tearDown() throws Exception {
super.tearDown();
HibernateFactory.closeFactory();
}
public void testTemplateOperations() throws Exception {
Event event = new Event();
event.setName("TestTemplate");
try {
template.saveOrUpdate(event);
Event obj = (Event) template.load(Event.class, event.getId());
assertEquals("Loads the event", event.getName(), obj.getName());
} finally {
template.delete(event);
}
}
public void testQuery() throws Exception {
Event event1 = new Event();
event1.setName("Event 1");
Event event2 = new Event();
event2.setName("Event 2");
try {
template.save(event1);
template.save(event2);
List events = (List) template.find("from Event");
assertTrue(events.size() >= 2);
} finally {
template.delete(event1);
template.delete(event2);
}
}
public void testCallback() {
Event event1 = new Event("Event 1");
Event event2 = new Event("Event 2");
Event event3 = new Event("Event 3");
List results = null;
try {
template.save(event1);
template.save(event2);
template.save(event3);
results = template.executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery("from Event");
query.setMaxResults(2);
return query.list();
}
});
assertEquals(2, results.size());
} finally {
template.deleteAll(results);
}
}
public void testCallbackUpdate() {
Event event1 = new Event("Event 1");
Event event2 = new Event("Event 2");
Event event3 = new Event("Event 3");
List results = null;
try {
template.save(event1);
template.save(event2);
template.save(event3);
template.execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery("from Event");
query.setMaxResults(2);
List events = query.list();
for (Iterator it = events.iterator(); it.hasNext();) {
Event event = (Event) it.next();
event.setDuration(60);
}
return null;
}
});
results = template.executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery("from Event");
query.setMaxResults(2);
return query.list();
}
});
assertEquals(2, results.size());
for (Iterator it = results.iterator(); it.hasNext();) {
Event event = (Event) it.next();
assertEquals(60, event.getDuration());
}
} finally {
template.delete(event1);
template.delete(event2);
template.delete(event3);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -