简单描述Hibernate单元测试
Hibernate还是比较常用的,于是我研究了一下Hibernate单元测试,在这里拿出来和大家分享一下,希望对大家有用。
本文介绍在Hibernate单元测试中最重要的就是要保持测试实例是独立的。因为该方法仍然涉及数据库,所以需要一种方法在每个Hibernate单元测试实例之前清理数据库。在我的数据库架构中有四个表,所以我在TestSchemaz上编写了reset()方法,该方法从使用JDBC的表中删除所有行。注意,因为HSQLDB能识别外键,删除表的顺序是很重要的,下面是代码:
public static void reset() throws SchemaException { Session session = HibernateUtil.getSession(); try { Connection connection = session.connection(); try { Statement statement = connection.createStatement(); try { statement.executeUpdate("delete from Batting"); statement.executeUpdate("delete from Fielding"); statement.executeUpdate("delete from Pitching"); statement.executeUpdate("delete from Player"); connection.commit(); } finally { statement.close(); } } catch (HibernateException e) { connection.rollback(); throw new SchemaException(e); } catch (SQLException e) { connection.rollback(); throw new SchemaException(e); } } catch (SQLException e) { throw new SchemaException(e); } finally { session.close(); } }
相关推荐
蛰脚踝的天蝎 2020-11-10
Cocolada 2020-11-12
TuxedoLinux 2020-09-11
snowphy 2020-08-19
83540690 2020-08-16
lustdevil 2020-08-03
83417807 2020-07-19
张文倩数据库学生 2020-07-19
bobljm 2020-07-07
83417807 2020-06-28
86427019 2020-06-28
86427019 2020-06-25
zhengzf0 2020-06-21
tobecrazy 2020-06-16
宿命java 2020-06-15
83417807 2020-06-15
84284855 2020-06-11