用于测试的休眠配置 -(新手哇)
我已经学习使用 hibernate 几个月了。 我发现很难决定如何配置休眠以在测试数据库上工作。
我有一个 hibernate.cfg.xml ,其中数据库参数作为 元素给出。
<property name="connection.url">
jdbc:postgresql://localhost/mydb
</property>
<property name="connection.username">me</property>
<property name="connection.password">mypasswd</property>
我的 Web 应用程序使用 HibernateUtil 类,该类加载如下配置
class HibernateUtil {
private Class<T> persistentClass;
private static SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
}catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
...
我的 dao 实现使用上面的类来获取 Session
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;
}
只要我处理 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
我可以使 hibernate 在 mytestdb 上工作的唯一方法是手动替换 cfg.xml 中的每个与数据库相关的属性。我非常想使用test_hibernate.cfg.xml 具有测试数据库属性,但由于配置是在 HibernateUtil 的静态块中完成的,所以这是行不通的。
有没有更好的方法为这两个数据库配置hibernate? 我正在使用 ant 作为构建工具。
任何建议将非常受欢迎,
吉姆
I have been learning to use hibernate for a couple of months.
I am finding it difficult in deciding how to configure hibernate to work on a test database.
I have a hibernate.cfg.xml with db parameters given as elements.
<property name="connection.url">
jdbc:postgresql://localhost/mydb
</property>
<property name="connection.username">me</property>
<property name="connection.password">mypasswd</property>
My web app uses a HibernateUtil class which loads the configuration as below
class HibernateUtil {
private Class<T> persistentClass;
private static SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
}catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
...
My dao implementation uses the above class to get the Session
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;
}
This is OK as long as I work on the mydb configured in cfg.xml file.But for my tests I am using another database which is given in a test.properties file
test.db.url=jdbc:postgresql://localhost/mytestdb
test.db.driver=org.postgresql.Driver
test.db.username=testme
test.db.password=testpass
The only way I can make hibernate work on mytestdb is to manually replace every db related property in cfg.xml.I would very much like to use test_hibernate.cfg.xml with the test db properties,but since the configuration is done in a static block in HibernateUtil ,that won't work.
Is there a better way of configuring hibernate for these two databases?
I am using ant as build tool..
Any suggestions would be most welcome
jim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以不要在静态块中创建配置。
相反,创建一个接受配置文件路径参数的类(类似于 Spring 的
LocalSessionFactoryBean
) 返回要使用的 Configuration/SessionFactory。或者,如果您确实想坚持使用 HibernateUtil(在任何生产环境中强烈建议不要采用这种策略),请将其更改为读取属性或系统环境变量来读取配置。So then don't create your configuration in a static block.
Instead, create a class which accepts a parameter for the path to the configuration file (a la Spring's
LocalSessionFactoryBean
) which returns the Configuration/SessionFactory to use. Or if you truly want to stick withHibernateUtil
(which is a strategy very much recommended against in any production setting), change it to read a property or system environment variable to read the configuration.