我一直在學習使用休眠幾個月。 我很難在決定如何配置hibernate以在測試數據庫上工作。休眠配置測試 - (新手們)
我有一個hibernate.cfg.xml db參數給出爲元素。
<property name="connection.url">
jdbc:postgresql://localhost/mydb
</property>
<property name="connection.username">me</property>
<property name="connection.password">mypasswd</property>
我的web應用程序使用它加載的配置如下
class HibernateUtil {
private Class<T> persistentClass;
private static SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
}catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
...
吾道實現使用上面的類來獲取會議
public class BaseDaoImpl<T, ID extends Serializable>{
private Session session;
...
public Session getSession() {
if (session == null || !session.isOpen()){
return HibernateUtil.getCurrentSession();
}else{
return session;
}
}
public T findById(ID id){
T entity = (T) getSession().load(getPersistentClass(), id);
return entity;
}
這是確定一個HibernateUtil類只要我在cfg.xml文件中配置的mydb上工作。但對於我的測試,我正在使用另一個數據庫,它在test.properties文件中給出
test.db.url=jdbc:postgresql://localhost/mytestdb
test.db.driver=org.postgresql.Driver
test.db.username=testme
test.db.password=testpass
我可以對mytestdb休眠工作的唯一方法是手動替換cfg.xml.I每個數據庫相關的屬性非常希望與測試數據庫性能使用test_hibernate.cfg.xml,但由於配置是在HibernateUtil的一個靜態塊中完成的,這是行不通的。
有沒有更好的方式爲這兩個數據庫配置hibernate? 我使用Ant作爲構建工具..
任何建議將是最歡迎
吉姆