Chanaka De Silva Chanaka De Silva - 4 months ago 14
Java Question

How to use MySQL in Spring and Hibernate background which currently uses HSQLDB

I have a project created using Spring & Hibernate. The database is used in this project is HSQLDB. (http://hsqldb.org/) Actually I haven't use that database earlier and no idea about it. This is the GitHub repository of the project. (https://github.com/chanakaDe/spring-hibernate-20120924). In the

pom.xml
file, you can see the hsql version and everything.

<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.2.8</version>
<scope>runtime</scope>
</dependency>


There are two things I need to know. I'm creating a REST API for an Accounting System using this project. Is it ok to use HSQLDB for that kind of a project? Performance and functionalities?

And also how can I use MySQL with this project? Is there any way to remove HSQL and use MYSQL insteed of? Do I need to do lot of changes if I need to change the database to MYSQL?

Answer

On your pom.xml add this:

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>${mysql-connector-java.version}</version>
</dependency>

then on your applicationContext.xml replace those hsqldb stuffs with these:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="packagesToScan" value="The package the contains your entities here e.g. com.example.model" />
  <property name="jpaVendorAdapter">
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
  </property>
  <property name="jpaProperties">
     <props>
        <prop key="hibernate.hbm2ddl.auto">validate</prop>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
     </props>
  </property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url" value="jdbc:mysql://localhost:3306/your-database-here" />
  <property name="username" value="db-username" />
  <property name="password" value="db-password" />
</bean>
Comments