Exporting schema

In this section, we export the schema in schemaExport.jsp JSP. We will run schemaExport.jsp in a later section. Import the org.hibernate.cfg.Configuration and org.hibernate.tool.hbm2ddl.SchemaExport Hibernate classes. An org.hibernate.cfg.Configuration object is an initialization-time-only object to configure properties and mapping files. Create an instance of the Configuration class with the no-argument constructor and configure the hibernate.cfg.xml Hibernate XML configuration file using the configure method in the manner shown in the following code:

Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");

The org.hibernate.tool.hbm2ddl.SchemaExport class is a command-line tool to export a table schema to a database and can also be invoked from an application. Create an instance of SchemaExport using the constructor that takes a Configuration object as an argument. Specify the Configuration object we created using the hibernate.cfg.xml file. The following is the line of code to accomplish this:

SchemaExport schemaExport =new  SchemaExport(cfg);

Set the output file for the DDL script used to create the database table. Use the following line of code to accomplish this:

schemaExport.setOutputFile("hbd2ddl.sql");

The output file gets generated in the bin directory of the WildFly installation. Export the schema to the database using the create(boolean script,boolean export) method. The script parameter specifies whether the DDL script used to create the database table is to be output to the console. The export parameter specifies whether the schema is to be exported. The create method can be run with export set to false to test the DDL script. Here's the code that encapsulates the discussion in this paragraph:

schemaExport.create(true, true);

Optionally, add an out statement to output a message that the schema has been exported. The schemaExport.jsp file is listed in the following code:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="org.hibernate.*,org.hibernate.cfg.Configuration,org.hibernate.tool.hbm2ddl.SchemaExport"%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/xml; charset=windows-1252" />
    <title>Export Schema</title>
  </head>
  <body>
    <%
      Configuration cfg=new Configuration();
      cfg.configure("hibernate.cfg.xml"); 
      SchemaExport schemaExport =new  SchemaExport(cfg);
      schemaExport.setOutputFile("hbd2ddl.sql");
      schemaExport.create(true, true);
      out.println("Schema Exported");
  %>
    </body>
</html>
..................Content has been hidden....................

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