简体   繁体   English

没有数据库的休眠/JPA

[英]Hibernate/JPA without DB

Wondering if it is possible to get rid completely of SQL dependency.想知道是否有可能完全摆脱 SQL 依赖。 Let's say I'm writing test which communicates with DB, this introduces the need to manage DB schema, this is very cumbersome as in-memory DB often do not match to production type DBs.假设我正在编写与 DB 通信的测试,这引入了管理 DB 模式的需要,这非常麻烦,因为内存 DB 通常与生产类型的 DB 不匹配。

Would it be possible to test with Hibernate/JPA and no underline DB Schema?是否可以使用 Hibernate/JPA 进行测试并且没有下划线 DB Schema?

I know so far of two options, I'd like here to broaden my knowledge, if there are any other possibilities, please share.到目前为止,我知道两种选择,我想在这里扩大我的知识,如果还有其他可能性,请分享。

  1. mock the hibernate模拟冬眠
  2. use fake JDBC driver使用伪造的JDBC 驱动程序

Or should I just focus on mocking DAO layer and not wasting time with this at all?或者我应该只专注于模拟 DAO 层而不是在这上面浪费时间? Sanity checking JPA entities vs DB is completely different story.健全性检查 JPA 实体与 DB 是完全不同的故事。

You fear the difference between an in-memory database and the production database?您害怕内存数据库和生产数据库之间的区别吗? But you don't fear the much greater difference between your two "mock" options and production?但是您不担心您的两个“模拟”选项和生产之间的巨大差异?

From my point of view any replacement of the production database for testing will create false positives somehow.从我的角度来看,任何用于测试的生产数据库的替换都会以某种方式产生误报。 And I would guess that an in-memory database will mimic the real environment best.我猜想内存数据库最能模拟真实环境。 Especially if you use something like H2 which tries to support different SQL dialects.特别是如果您使用H2 之类的尝试支持不同 SQL 方言的东西。

As long as you only write small and encapsulated unit tests a mock of your DAO layer may help as well.只要您只编写小型和封装的单元测试,DAO 层的模拟也可能有所帮助。 But as soon as you are writing integration tests you will need to provide a database somehow.但是,一旦您编写集成测试,您就需要以某种方式提供一个数据库。

And no - Hibernate can't work without a database schema or connection.不 - 如果没有数据库模式或连接,Hibernate 就无法工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM