Saturday, December 27, 2008

Configuring OpenXava with MySql


Recently I started learning OpenXava and I wanted to configure OpenXava with MySQL. Lack of documentation on OpenXava 3.1 and all of them were configured to HSQL database, took me a long time to configure it to MySQL. So I thought of sharing my findings with others.

The intention of this post is not to give a step by step guide for OpenXava (which I’ll do in some other post) but to mention the changes you have to do in OpneXava project configuration files to work it with MySQL.

If you are new to OpeanXava please go through the reference manual and have a basic knowledge first.
http://openxava.wikispaces.com/reference_en

Ok, let’s get in to the business now.

Installs

1) MySQL database (ex. MySQL 5.0.24)
2) MySQL connector J (ex. mysql-connector-java-5.1.7-bin.jar)
3) OpenXava 3.1


Steps to follow


1) Create a new project (ex. Academy) according to the steps mentioned in the OpenXava reference guide.

2) Go to MySQL console and create a new database ex. academydb



3) Place MySQL connector jar into {OpenXavaBase}/tomcat/common/lib


4) Open the context.xml file in {OpenXavaBase}/tomcat/conf folder. Insert the following lines into the file.


name : name of the data source, use the default name OpeanXava gives. (Since I created the project as Academy, the default datasource name given is AcademyDS.
username: username of the MySQL database
password: password for the MySQL database
driverClassName: com.mysql.jdbc.Driver in this case
url: you have to point to the database you have creates. (ex. academydb)


5) Go to the project folder’s build.xml (Academy/build.xml) and change updateSchema target as follows. You have to change the value of the schema.path to point to the MySQL connector jar.



6) Go to persistence.xml (Academy/persistence/META-INF/persistence.xml)
Change the default persistence unit as follows



And change the junit persistence unit as follows




7) Go to hibernate.cfg.xml (Academy/persistence/hibernate.cfg.xml) and change the hibernate.dialect to org.hibernate.dialect.MySQLDialect as follows


That’s it, and now when you run updateSchema target, it will create the tables in MySQL and once you deploy the application, that will access the MySQL to retrieve and save data. (Assumed that you have written the relevant entity classes)