开发者

running "virtual" database in Java code with Hibernate

开发者 https://www.devze.com 2023-01-31 03:50 出处:网络
I used to run my Java code with Hibernate to mysql. Some logical changes led me to decide th开发者_高级运维at I don\'t need to save the data in the database, but in java class that will handle it as

I used to run my Java code with Hibernate to mysql.

Some logical changes led me to decide th开发者_高级运维at I don't need to save the data in the database, but in java class that will handle it as a database (The data is delete in every startup of the application).

How can I do in Hibernate? is there a way to create an in memory database?


Consider H2 (recommended) or HSQLDB. You can certainly use Hibernate with them. In fact, HSQLDB was the database used by default in Hibernate for their test suite (I think they just changed to H2).


Take a look at Apache Derby.


You could also use SqlLite, which is an in-memory database. I'm just about to start using it with NHibernate for the same purpose in unit tests.


You just need to:

  • add the driver for the database you want to use and change the hibernate settings for this driver,
  • change the setting hibernate database dialect,
  • change the jdbc connection String
  • (add the jars for the database)

For example for Hypersonic H2 Database:

  • driverClassName = org.h2.Driver
  • dialect = org.hibernate.dialect.H2Dialect
  • connection string = jdbc:h2:mem:test
0

精彩评论

暂无评论...
验证码 换一张
取 消