Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate + embedded database - setup

I have created Java application using Hibernate with this configuration:

<hibernate-configuration>
 <session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306     /bee</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
    <property name="connection.username">root</property>
    <property name="connection.password"/>
    <property name="hibernate.connection.charSet">UTF-8</property>
    <property name="hibernate.connection.characterEncoding">UTF-8</property>
    <property name="hibernate.connection.useUnicode">true</property>
    <mapping resource="DatabaseMapping.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

Everything works fine when Iam using jdbc:mysql://localhost..., but now I need to have the database embedded in my application. Which database should I use? I need to have all my data stored and load it after start the application, update data, save, delete. I use HQL query or SQL query.

What is the simplest way to make the database embedded? I don´t want to change my queries. It would be fine to change only hibernate configuration and set it to the embedded database, is it possible?

like image 334
cernover Avatar asked May 17 '13 20:05

cernover


2 Answers

One of the advantages of ORM's like Hibernate is to shield you from DB differences. You can use any one of the below as an embedded DB solution. Just change the dialect, driver and URL in hibernate cfg file.

  1. H2
  2. SQLite
  3. HSQLDB
like image 95
Aravind Yarram Avatar answered Nov 12 '22 09:11

Aravind Yarram


Hava a look at using the H2 database in embedded mode

<hibernate-configuration>
 <session-factory>
    <property name="hibernate.connection.driver_class">org.h2.Driver</property>
    <property name="hibernate.connection.url">jdbc:h2:~/test</property>
    <property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
    <property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
    <property name="connection.username">sa</property>
    <property name="connection.password"/>
    <property name="hibernate.connection.charSet">UTF-8</property>
    <property name="hibernate.connection.characterEncoding">UTF-8</property>
    <property name="hibernate.connection.useUnicode">true</property>
    <property name="hibernate.default_schema">PUBLIC</property>
    <mapping resource="DatabaseMapping.hbm.xml"/>
 </session-factory>
</hibernate-configuration>
like image 38
Reimeus Avatar answered Nov 12 '22 09:11

Reimeus