Roo supports creating JPA entities by introspecting an existing database. If you want Roo to create JPA entities for an existing database, then you may want to know the database metadata used as an input by Roo to create JPA entities corresponding to database tables. In this recipe, we'll look at the database
introspect
command, which lets us view the database metadata in XML format. In the next recipe, Creating entities from a database, we will see how this metadata is used by Roo to create JPA entities.
The following figure shows tables and views of a database that we'll introspect using the database
introspect
commd:
In the given figure, the FLIGHT_TBL
, FLIGHT_DESC_TBL,
and CUSTOMER_TBL
represent database tables, and the FLIGHTS_VIEW
represents a database view. The relationship between the FLIGHT_TBL
and FLIGHT_DESC_TBL
tables is many-to-one. The CUSTOMER_TBL
uses a composite primary key (consisting of the CUST_ID
and CUST_DOB
columns) to uniquely identify a customer. The FLIGHTS_VIEW
database view combines data from the FLIGHT_TBL
and FLIGHT_DESC_TBL
tables based on the FLIGHT_DESC_ID
column.
Exit the Roo shell and delete the contents of the C:
oo-cookbookch03-recipes
directory.
Start the Roo shell from the C:
oo-cookbookch03-recipes
directory.
Execute the ch03_jpa_setup.Roo
script which creates the flight-app
Roo project, sets up Hibernate as a persistence provider, and configures MySQL as the database for the application. If you are using a different database than MySQL or your connection settings are different than what is specified in the script, then modify the script accordingly.
Execute the myflightappdb.sql
SQL script (that accompanies this book) against the MySQL database. The myflightappdb.sql
creates a database named myflightappdb
consisting of the FLIGHT_TBL
, FLIGHT_DESC_TBL,
and CUSTOMER_TBL
tables and a FLIGHT_VIEW
database view, as shown earlier in this recipe. Ensure that the database.properties
file of your Roo project contains the settings to connect to this newly created myflightappdb
database.
For the purpose of this recipe, we'll be using the database
introspect
command to view the metadata of a myflightappdb
database created in MySQL database. Now, you're all set to view metadata related to the database you've configured in the database.properties
file of your Roo project.
To use the database
introspect
command to view the database metadata, follow the steps given here:
download
accept
terms
of
use
command, as shown here:roo> download accept terms of use
database
introspect
command, as shown here:roo> database introspect --schema no-schema-required --file mydb.xml --enableViews Located add-on that may offer this JDBC driver 1 found, sorted by rank; T = trusted developer; R = Roo 1.1 compatible ID T R DESCRIPTION ----- 01 Y Y 5.1.13.0001 #jdbcdriver driverclass:com.mysql.jdbc.Driver. This... ------------------------ [HINT] use 'addon info id --searchResultId ..' to see details about a search result [HINT] use 'addon install id --searchResultId ..' to install a specific search result, or [HINT] use 'addon install bundle --bundleSymbolicName TAB' to install a specific add-on version
database
introspect
command for the first time suggests the database driver that we need to download to perform the introspection of the myflightappdb
database in MySQL. So, download the suggested driver using the addon
install
command, as shown here:roo> addon install id --searchResultId 01 Target resource(s): ------------------- Spring Roo - Wrapping - mysql-connector-java (5.1.13.0001) Deploying...done. Successfully installed add-on: Spring Roo - Wrapping - mysql-connector-java [version: 5.1.13.0001]
database
introspect
command as shown here:roo> database introspect --schema no-schema-required --file mydb.xml --enableViews Database metadata written to file C: oo-cookbookch03-recipesmydb.xml
mydb.xml
file to view the database metadata.The database
introspect
command makes use of the connection properties defined in the database.properties
file of the Roo project to create metadata information. This implies that you can't simultaneously introspect multiple databases using the database
introspect
command. The command accepts the following arguments:
schema
(mandatory): It is the database schema which you want to introspect. If you want Roo to connect to the database and provide the list of schemas available, then simply press TAB after entering the--schema
argument on the command line. As some of the databases such as MySQL and Firebird don't support the concept of schemas, pressing TAB after entering the--schema
argument on the command line will result in substituting no-schema-required
as the value of the schema
argument.file
(optional): It is the file to which you want to save the database metadata. If you don't specify this argument, the metadata is displayed directly in the Roo console.enableViews
(optional): It is the flag that indicates whether to include database views in the generated metadata. By default database views are not included in the generated metadata.The output of the database
introspect
command is an XML file (assuming a file
argument was specified), which contains details of database tables, including fields and their type, primary key, foreign key, and so on. If you specify the enableViews
argument, the generated metadata includes database views also.
The following XML fragment shows how the database metadata is presented in the mydb.xml
file:
<database name="no-schema-required"> <table name="customer_tbl"> <column name="cust_id" primaryKey="true" required="true" scale="0" size="10" type="4,INT"/> ... <unique name="PRIMARY"> <unique-column name="cust_id"/> <unique-column name="cust_dob"/> </unique> </table> <table name="flight_desc_tbl"> <column name="flight_desc_id" primaryKey="true" required="true" scale="0" size="19" type="-5,BIGINT"/> ... <foreign-key foreignTable="flight_tbl" name="FK7E26BB6F365DD59" onDelete="none" onUpdate="none"> <option key="exported" value="true"/> <reference foreign="flight_desc_id" local="flight_desc_id"/> </foreign-key> <unique name="PRIMARY"> <unique-column name="flight_desc_id"/> </unique> </table> <table name="flight_tbl"> ... </table> <table name="flights_view"> <column name="origin_city" primaryKey="false" required="true" scale="0" size="255" type="12,VARCHAR"/> ... </table> </database>
The following table describes the significance of the elements in the given XML:
The metadata generated by Roo is used as input for generating JPA entities, as we'll see in the Creating entities from database recipe. Errors may be reported when you use the database
introspect
command if the META-INF/spring/database.properties
file is not found or the connection properties defined in the database.properties
are incorrect. The following table shows error messages that might be reported by the database
introspect
and the corresponding resolution:
Error message |
Resolution |
---|---|
|
Indicates that the |
|
Indicates that the |
3.145.17.18