In Chapter 3, Advanced JPA Support in Spring Roo we discussed that the database reverse engineering process of Roo requires the JDBC driver for the database to be available as an OSGi bundle. In this recipe, we'll look at how to convert H2 database's JDBC driver into a OSGi bundle using the addon
create
wrapper
command and use it in database reverse engineering.
Download the H2 database bundled as a ZIP file from , and unzip it to the C:
oo-cookbook
directory. Extracting the H2 database ZIP file will create a directory named h2
inside the C:
oo-cookbook
directory.
The following steps will demonstrate how to convert non-OSGi JDBC drivers into OSGi compliant bundles:
C:
oo-cookbookh2in
directory and double-click the h2.bat
file. This will start the H2 database and also open H2 Console in your default web browser, as shown in the following screenshot:Make sure that you select Generic H2 (Server) as the value of the Saved Settings option and the value of the JDBC URL field is jdbc:h2:tcp://localhost/~/myflightappdb
. Click the Connect button to log in to the H2 Console. This will automatically create the myflightappdb
in H2 database.
myflightappdb
details, as shown here:Now, paste the following SQL statement in the text area shown in the given screenshot and execute it by clicking the Run (Ctrl+Enter) button:
DROP TABLE IF EXISTS 'customer_tbl'; CREATE TABLE IF NOT EXISTS 'customer_tbl' ( 'cust_id' int(10) NOT NULL, 'cust_dob' date NOT NULL, 'cust_name' varchar(50) NOT NULL, PRIMARY KEY ('cust_id','cust_dob') )
Executing the SQL statement will create the CUSTOMER_TBL
table in the myflightappdb
database.
C:
oo-cookbookch07-recipesdriver
directory and copy the h2*.jar
file from the C:
oo-cookbookh2in
directory to the driver
directory.C:
oo-cookbookch07-recipesdriver
and execute the maven
install
command:C:
oo-cookbookdriver> mvn install:install-file -Dfile=h2-1.3.160.jar -DgroupId=com.h2database -DartifactId=h2 -Dversion=1.3.60 -Dpackaging=jar
Here, it is assumed that H2 database driver JAR file is the h2-1.3.160.jar
file.
C:
oo-cookbookch07-recipeswrapper
directory and start the Roo shell from it.addon
create
wrapper
command to create a Roo add-on that wraps the h2-1.3.160.jar
maven
artifact:... roo> addon create wrapper --topLevelPackage com.h2.roo.jdbc --groupId com.h2database --artifactId h2 --version 1.3.60 --vendorName H2 --licenseUrl http://www.h2database.com
pom.xml
file and add the following <Import-Package>
element to the configuration of Maven Bundle Plugin:<instructions> <Import-Package>javax.servlet.*;resolution:=optional, org.apache.lucene.*;resolution:=optional, org.slf4j;resolution:=optional,* </Import-Package> ... </instructions>
C:
oo-cookbookch07-recipeswrapper> mvn bundle:bundle
This maven goal creates the OSGi-compliant H2 database driver in the target
directory of the project with name com.h2.roo.jdbc.h2-1.3.60.0001.jar
.
C:
oo-cookbookch07-recipesflight-app
directory and start the Roo shell from it.flight-app
directory:... roo> project --topLevelPackage sample.roo.flightapp --java 6 --projectName flight-app
myflightappdb
H2 database:... roo> persistence setup --provider HIBERNATE --database H2_IN_MEMORY --databaseName myFlightAppDB
... roo> database properties set --key database.username --value sa
perform
eclipse
command to create Eclipse IDE-specific configuration files:... roo> perform eclipse
flight-app
project into Eclipse IDE and modify the database.url
property in the SRC_MAIN_RESOURCES/META-INF/spring/database.properties
file to point to the myflightappdb
H2 database, as shown here:database.password= database.url=jdbc:h2:tcp://localhost/~/myflightappdb database.username=sa database.driverClassName=org.h2.Driver
... roo> osgi start --url file:///C:/roo-cookbook/ch07-recipes /wrapper/target/com.h2.roo.jdbc.h2-1.3.60.0001.jar
database reverse engineer
command to instruct Roo to create the JPA entity corresponding to the CUSTOMER_TBL
in the H2 database:roo> database reverse engineer --schema PUBLIC
The addon
create
wrapper
command creates an add-on that wraps a Maven artifact. The Apache Felix Maven Bundle Plugin's bundle
goal creates an OSGi-compliant JAR for the add-on project.
The <Import-Package>
element specifies the packages that are required or optional for the bundle.
3.14.134.17