使用 Hibernate 进行数据模型测试

发布于 2024-11-18 13:44:10 字数 1554 浏览 3 评论 0原文

在我的项目中,我有很多 hbm.xml 文件,我从中生成 java 类和数据库的 sql。调用 buildSessionFactory() 后,我会看到 hbm.xml 文件中的语义错误,这对于当前情况来说确实很烦人。我想要一个测试类,它可以通过稍微不同的配置来为我做到这一点(使用 ebedded derby 代替)。我当前的“解决方案”看起来像这样:

    String dbName = "test";

    try{
        SessionFactory fact = new Configuration().configure()
                                                 .setProperty("hibernate.connection.driver_class","org.apache.derby.jdbc.EmbeddedDriver")
                                                 .setProperty("hibernate.connection.url","jdbc:derby:" + dbName + ";create=true")
                                                 .setProperty("hibernate.connection.username","")
                                                 .setProperty("hibernate.connection.password","")
                                                 .setProperty("hibernate.dialect","org.hibernate.dialect.DerbyDialect")
                                                 .setProperty("hibernate.hbm2ddl.auto","create-drop")
                                                 .buildSessionFactory();
        assertNotNull(fact);

        Session s = fact.openSession();
        assertNotNull(s);

        s.close();
        fact.close();
    }catch(Throwable t){
        fail(t.getMessage());
    }

但从我的角度来看,这并不真正令人满意。 我还想检查命名查询等是否都正常。有没有办法在自动化过程中进行此类测试?

我的问题的第二部分是,如果 derby 是测试的不错选择,为什么我不能在测试结束后自动删除数据库?我想在 url 中指定一个参数,例如

    "jdbc:derby:" + dbName + ";create=true;drop=true"

通过使用此解决方案,我最终会得到一个名为变量 dbName 的目录和一个项目目录中的 derby 文件,这并不酷。

In my project i have a lot of hbm.xml files from which i generate the java classes and the sql for the db. Semantic errors within the hbm.xml files are shown to me after calling buildSessionFactory() which is really annoying for the current situation. I would like to have a test class which does that for me with a slightly different config(use ebedded derby instead). My current "solution" looks like that:

    String dbName = "test";

    try{
        SessionFactory fact = new Configuration().configure()
                                                 .setProperty("hibernate.connection.driver_class","org.apache.derby.jdbc.EmbeddedDriver")
                                                 .setProperty("hibernate.connection.url","jdbc:derby:" + dbName + ";create=true")
                                                 .setProperty("hibernate.connection.username","")
                                                 .setProperty("hibernate.connection.password","")
                                                 .setProperty("hibernate.dialect","org.hibernate.dialect.DerbyDialect")
                                                 .setProperty("hibernate.hbm2ddl.auto","create-drop")
                                                 .buildSessionFactory();
        assertNotNull(fact);

        Session s = fact.openSession();
        assertNotNull(s);

        s.close();
        fact.close();
    }catch(Throwable t){
        fail(t.getMessage());
    }

But this it not really satisfying from my point of view.
I would also like to check if the named queries and so on are all ok. Is there a way to do that kind of tests in an automated process?

The second part of my question then is, if derby is a good choice for testing, why can't i automatically delete the db after the test ended? I'd like to specify a parameter in the url like

    "jdbc:derby:" + dbName + ";create=true;drop=true"

By using this solution i end up with a dir named like the variable dbName and a derby file in my project directory which is not cool.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放飞的风筝 2024-11-25 13:44:10

如果您想测试命名查询,解决方案可能是在测试中使用虚假参数调用它们(查询可能不会返回任何内容,但至少会强制 hibernate 检查它们);大致如下:

 s.getNamedQuery("Whatever").setParameters(...).execute/query

现在您的问题是您无法轻松获取所有查询的名称吗?

至于 derby 数据库,您不能在测试结束时“手动”删除创建的文件夹吗(例如,在tearDown 中?)

似乎可以控制创建 derby 基地的位置具有 java 属性:

http://db.apache.org/derby/manuals/develop/develop14.html

http://db.apache.org/derby/manuals/develop/develop12.html#HDRSII-DEVELOP-13018

If you want to test your named queries, a solution could be to call them in the test with bogus arguments (the queries would probably return nothing, but at least it would force hibernate to check them) ; something along the lines of :

 s.getNamedQuery("Whatever").setParameters(...).execute/query

Now is your problem the fact that you cannot easily get the names of all queries ?

As for the derby db, can't you just "manually" get rid of the created folder at the end of your test (in the tearDown, for example ?)

It seems like the location of where the derby base is created can be controled with a java property :

http://db.apache.org/derby/manuals/develop/develop14.html

http://db.apache.org/derby/manuals/develop/develop12.html#HDRSII-DEVELOP-13018

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文