一些有点奇怪的事情发生在我身上。我有一个项目,我正在测试我的数据访问层,好吗?但是我进行了测试,他通过了。后来我发现我的 Mysql Diamond 没有运行。可能会发生什么?
// 测试代码
public class AppTest {
static final Logger log = Logger.getLogger(AppTest.class);
@Test
public void sessionTest() { // Passed
Session s = HibernateUtil.getSessionFactory().openSession();
Assert.assertTrue(s.isOpen());
}
@Test
public void fetchEvent() { // Failed
EventDao edao = new EventDao();
Event e = null;
try {
e = edao.find(1);
} catch (Exception ex) {
log.fatal(ex.getMessage());
}
Assert.assertNotNull(e);
}
@Test
public void fetchAll() { // Failed
EventDao edao = new EventDao();
List<Event> e = null;
try {
e = edao.all(); // I have 6 rows on the Event table
} catch (Exception ex) {
log.fatal(ex.getMessage());
}
Assert.assertEquals(e.size(), 6);
}
}
// HibernateUtil
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
// src/main/resources/hibernate.cfg.xml 代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Connection settings -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/MyDB</property>
<property name="hibernate.connection.username">root</property>
<!-- Class mappings -->
...
<mapping class="br.siseventos.siseventosmaventest.model.Event"/>
...
</session-factory>
// EventDao 代码
public class EventDao extends BaseDao<Event> {
}
// BaseDao 代码
public abstract class BaseDao<T> implements Dao<T> {
// Fields
private Class actualClass;
// Constructor
public BaseDao() {
// Fetching generic class parameter
actualClass = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
// Getters e Setters
public Class getActualClass() {
return actualClass;
}
public void setActualClass(Class actualClass) {
this.actualClass = actualClass;
}
// Service
public T find(int id) throws Exception {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction t = null;
T o = null;
try {
t = session.beginTransaction();
o = (T) session.get(actualClass, new Integer(id));
t.commit();
} catch (Exception e) {
if (t != null) {
try {
t.rollback();
} catch (Exception ex) {
}
}
throw e;
} finally {
if (session != null) {
session.close();
}
}
return o;
}
public List<T> all() throws Exception {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction t = null;
List<T> o = null;
try {
t = session.beginTransaction();
o = (List<T>) session.createQuery("from " + getActualClassName()).list();
t.commit();
} catch (Exception e) {
if (t != null) {
try {
t.rollback();
} catch (Exception ex) {
}
}
throw e;
} finally {
if (session != null) {
session.close();
}
}
return o;
}
// Util
public String getActualClassName() {
return getActualClass().getSimpleName();
}
}