Generating database metadata

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:

Generating database metadata

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.

Getting ready

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.

How to do it...

To use the database introspect command to view the database metadata, follow the steps given here:

  1. To enable downloading of the driver for the database which we are about to introspect, execute the download accept terms of use command, as shown here:
    roo> download accept terms of use
    
  2. Execute the 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
    
  3. Executing the 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]
    
  4. Re-execute the 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
    
  5. Now, open the generated mydb.xml file to view the database metadata.

How it works...

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:

XML element

Description

<table>

Describes a database table or a database view. The name attribute specifies the name of the table or view which the <table > element describes.

<column>

The <column>, sub-element of the <table> describes the details of a column in the database table or view. The name attribute identifies the column name. The primaryKey attribute indicates if the column is one of the primary keys of the table or view.

<foreignKey>

Specifies the details of an imported or exported foreign key of a table. The foreignTable attribute specifies the name of the table containing the imported or exported foreign key. The <option> sub-element specifies whether the foreign key is imported or exported by the table. The <reference > sub-element specifies the foreign key column and the local column to which the imported or exported foreign key maps.

<unique>

Describes the unique constraint that applies to the table or view. If the name attribute value is PRIMARY, then it means that the unique constraint describes the primary key of the table.

There's more...

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

Connection properties must not be null or empty

Indicates that the database.properties file was not found in the META-INF/spring folder

Unable to get connection from driver

Indicates that the database.properties file was found but it doesn't correctly define the connection properties. Check if the username, password, and other connection properties are correctly specified.

Tip

Testing database connection

As the database introspect command connects to the database to generate metadata, you can also use this command to validate the database connection after you've executed the persistenceXsetup command.

See also

  • Refer to the Creating entities from a database recipe to see how the database metadata is used for auto-generating JPA entities
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.17.18