Archive for September, 2007

Web server extensions - APPENDIX B n HIBERNATE TOOLS 289 n HIBERNATE

Sunday, September 30th, 2007

APPENDIX B n HIBERNATE TOOLS 289 n HIBERNATE TOOLS 289 The task shown in Listing B-2, which completes the simple example that we ve been building up in this section, creates a schema generation script from the annotation-based mappings referenced in the project s hibernate.cfg.xml file. Listing B-2. A Complete Hibernate Mapping Target The element generates a Hibernate XML configuration file from the metamodel information. Table B-4 shows the attributes that can be supplied. Table B-4. The Attributes Available to the Element Property Default Description destdir If set, overrides, for this exporter only, the destination directory specified on the tools task. ejb3 false By default, causes entities to be mapped using entries in the configuration file. If set to true, the entities will be mapped using the approach to pick up EJB 3 annotations in the mapped classes. This setting does not cause a persistence.xml file to be generated! Typically, the element is used when the configuration task has been configured from a properties file for example, when using , you would typically start with a normalized database schema and a properties file containing the connection details, and use this exporter to create a hibernate.cfg.xml file containing both the connection details and the details of the mapped entities. The element generates the Java source code for POJOs for each of the entities held in the metamodel. Table B-5 shows the attributes that can be supplied. Table B-5. The Attributes Available to the Element Property Default Description destdir If set, overrides, for this exporter only, the destination directory specified on the tools task ejb3 false If set to true, causes the POJOs to be generated with EJB 3 annotations jdk5 false If set to true, causes the POJOs to be generated with Java 5 constructs (generics, enumerations, etc.)
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

Vps web hosting - 288 APPENDIX B n HIBERNATE TOOLS APPENDIX B

Saturday, September 29th, 2007

288 APPENDIX B n HIBERNATE TOOLS APPENDIX B n HIBERNATE TOOLS Table B-2. Continued Element Description Generates HTML documentation for the database schema from the metamodel Generates standard DAOs from the metamodel Generates arbitrary user-defined output from the metamodel Runs arbitrary HQL queries against the database using the mapping information in the metamodel You may notice that the exporters available as Ant tasks correspond fairly closely to the exporters available in the Hibernate Code Generation tool largely because they rely upon the same underlying implementations. The two most commonly used tasks are , which can generate a database schema directly from the *.hbm.xml mapping files, and , which, conversely, can generate mapping files directly from the database. The element generates DDL scripts from the metamodel. These can be written to a file or, if the configuration object is provided with database connection details, they can be run directly against the database. Table B-3 shows the attributes that can be supplied. Table B-3. The Attributes Available to the Element Property Default Description create true If set to true, causes the generated DDL to include commands to create database objects. This allows to distinct tasks to be created: one to drop all relevant database objects (using the drop attribute) and the other to create them. console true If set to true, causes the generated DDL to be displayed on the console. delimiter ; Specifies the delimiter to be used to separate DDL statements. destdir If set, overrides, for this exporter only, the destination directory specified on the tools task. drop false If set to true, causes the generated DDL to include commands to drop preexisting database objects before it tries to create them. This may cause warning messages, depending upon the preexisting state of the database; and it of course has the potential to destroy existing data. export true If set to true, causes the DDL to be run directly against the database (this has the potential to delete data do not use carelessly). format false If set to true, causes the generated DDL to be formatted using whitespace in a more readable fashion. We recommend using this option if you will be writing the DDL to a file. haltonerror false If set to true, causes the script to halt if an error is encountered while generating the DDL (typically, this is used while exporting directly to the database to increase the visibility of any problems encountered while setting up the schema). outputfilename Specifies the name of the file name that the generated DDL should be stored in. If left unset, the generated DDL will not be stored. update false Indicates that the tool should attempt to generate the appropriate statements to bring the existing schema inline with the model. We don t recommend using this option.
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

APPENDIX B n HIBERNATE TOOLS 287 n HIBERNATE (Most popular web site)

Friday, September 28th, 2007

APPENDIX B n HIBERNATE TOOLS 287 n HIBERNATE TOOLS 287 : Mapping relationships are generated from the EJB 3 and Hibernate 3 annotations in conjunction with a *.cfg.xml or *.properties file. Identical to . : Mapping relationships are generated from the schema metadata obtained over a JDBC connection. The connection details are configured from a properties file. All those from , plus the following: packagename: The name of the package that entities should belong to. reversestrategy: The fully qualified name of a class implementing the org. hibernate.cfg.reveng.ReverseEngineeringStrategy interface. This is the programmatic equivalent of the reveng.xmlfile approach. revengfile: The name of a reverse engineering file to use when processing metadata information. See the discussion later in this section. : Mapping relationships are generated from the EJB 3 and Hibernate 3 annotations in conjunction with an EJB 3 compliant persistence.xml file. entityresolver: The name of the SAX EntityResolver to use when resolving external XML entities. namingstrategy: A naming strategy to use (see Chapter 3) to establish table names from entity names. The element also allows you to specify a standard Ant of *.hbm.xml mapping files. If you use this in conjunction with a *.cfg.xml configuration file, you must not permit any mapping resources to be duplicated, as this will result in duplicate import mapping exceptions. Your choice of configuration element will be driven by the data sources that you have available to you. For example, if you have created your XML mapping files, you will want to use the standard configuration element, but if you have only a normalized database, you will want to generate the mapping information from this using the JDBC configuration (although you may well choose to create a reverse engineering file to control this). Once you have correctly configured an annotation object, however, you can generate any of the other resources that you might need using one or more exporter elements. These are listed in Table B-2. Table B-2. The Available Exporter Elements Element Description Generates tables from the metamodel Generates a *.cfg.xml configuration file from the metamodel Generates entity POJOs from the metamodel Generates Hibernate *.hbm.xml mapping files from the metamodel Continued
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

286 APPENDIX B n HIBERNATE TOOLS APPENDIX B (Affordable web design)

Thursday, September 27th, 2007

286 APPENDIX B n HIBERNATE TOOLS APPENDIX B n HIBERNATE TOOLS Table B-1. The Daughter Elements of the Hibernate Tools Task Attribute Description classpath The path to use when locating libraries and configuration files. destDir The base directory (relative to the build script s own base directory) into which any generated output will be written. templatePath The path containing user-created template files (see the further discussion of templates in the Templates section later in the chapter.) Within the declared Hibernate task, a number of additional standard elements can be created, consisting of a classpath declaration (an alternative to using the classpath attribute), a set of configuration elements, and a set of exporter elements. The classpath element and attribute are standard Ant features that allow you to bring in any necessary resources used by the Hibernate tasks. The clever bit of the Hibernate Ant task lies in the configuration elements. Declaring a configuration task causes an appropriate configuration object to be built in memory. These in-memory configuration objects all extend the standard org.hibernate.cfg.Configuration class. The Configuration class represents the mapping relationships between entities (combined with information from any configuration, properties, or reverse engineering files), and it is this information, the metamodel, that is then used to generate the various output files. The provided configuration elements can conjure up a Configuration object from the standard mapping files, from the metadata information gathered over a JDBC connection, and from the annotations discussed in Chapter 6. Within any given Hibernate Tools task, you can only have one configuration element configured normally, you would not want to generate output from two distinct representations of the mapping information, so the single declaration is shared between the generation tasks enclosed within the toolset task elements. The following list describes the configuration tasks and the attributes of each: : Mapping relationships are generated from conventional XML-based mapping files and information in a *.cfg.xmlor *.properties file. configurationfile: The name of the XML configuration file being used. propertyfile: The name of the properties file being used. entityresolver: The name of the SAX EntityResolver to use when resolving external XML entities (rarely used). namingstrategy: A naming strategy to use (see Chapter 3) to establish table names from entity names.
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

APPENDIX B n HIBERNATE TOOLS (Yahoo web space) 285 n HIBERNATE

Wednesday, September 26th, 2007

APPENDIX B n HIBERNATE TOOLS 285 n HIBERNATE TOOLS 285 of a project outside the IDE. This can be useful in maintaining automated builds for regression testing and of course, not all Java IDEs offer integrated support for Hibernate anyway, while most of them do offer support for the Ant build tool. And besides, you might not be using an IDE in the first place! The Ant tasks are part of the Hibernate Tools download, which is largely oriented toward use as an Eclipse plug-in. The Ant tools themselves rely upon a set of three JAR files: hibernate-tools.jar, freemarker.jar, and jtidy-r8-21122004.jar. The hibernatetools. jar file is currently only available as a download with the rest of the plug-in files. You will find the JAR file within the ZIP file in a directory called pluginsorg.hibernate. eclipse_X.x.x.xlibtools, where the Xs are substituted for the Hibernate Tools version number. The freemarker and jtidy JAR files will also be available from this directory. Since these JAR files have no dependencies upon the other parts of the plug-in, you can copy them to a directory with a less unwieldy name without further consequences. How the Ant Tasks Work Despite their diverse set of outputs, the Hibernate tasks actually all work in the same way. The task is imported into Ant using the standard TaskDef element. This makes the Hibernate tools libraries available to Ant itself, and allows you to select an appropriate element representing the Hibernate tools to use in your script. The Hibernate Tools JAR file must be made available to Ant on the classpath our example uses a preexisting classpath declaration (referenced through its id of classpath.tools see the Configuring the Classpath subsection later in this section). A standard Ant target is declared to contain the set of operations that you want to perform. Within the target with any other standard (or imported) Ant tasks, you can then include the element that you declared using the element. The other Hibernate task elements are only applicable within this task element. This outermost task element accepts three attributes, as listed in Table B-1.
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

284 APPENDIX B n (Free web hosting with ftp) HIBERNATE TOOLS APPENDIX B

Tuesday, September 25th, 2007

284 APPENDIX B n HIBERNATE TOOLS APPENDIX B n HIBERNATE TOOLS The only missing output is the database schema. The reverse engineering file can be generated from the database as we have described here, but it can also be created manually. We have already shown that it is possible to use the Hibernate Console s SchemaExport function to export an annotated POJO or mapping file into a database schema. So, using this functionality, it is possible to create pretty much everything from scratch, starting from a mapping, a database, or a set of POJOs. When you create your code generation configuration, you need to specify the location of your reverse engineering file, and the console configuration that will be used to establish the appropriate connection details. You need to specify the output directory into which the generated files should be written, and you can also specify a package that any generated classes should belong to. You will then need to select the types of output to be generated, as shown in Figure B-25. Figure B-25. Selecting the types of output to be generated In Figure B-25, we have selected everything except the Hibernate configuration file (we ve already got one), the JBoss Seam application (we re not interested in that), and because of a minor bug the EJB 3 annotations option. You would not normally elect to generate both annotations and XML mapping files anyway. nNote At the time of writing, the EJB3/JSR-220 annotations exporter option does not work. If this problem has not been fixed by the time you read this book, you can use an Ant task instead. The Ant Tasks As you will have noticed if you followed through the Eclipse discussion, our example project for this appendix includes build.xml and build.properties files. These are not strictly necessary when working with Eclipse, but it is often desirable to be able to build all the components
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.

APPENDIX B n HIBERNATE TOOLS 283 n HIBERNATE (X web hosting)

Thursday, September 20th, 2007

APPENDIX B n HIBERNATE TOOLS 283 n HIBERNATE TOOLS 283 In Figure B-23, we have selected all the tables anyway. Once you click Finish, the reverse engineering file will be generated, and an editor will be opened for it. This is a tabbed view of the XML file (the last tab shows the XML source code). This allows you to change the default mappings between the database and Hibernate types, to alter the table and column details, and to apply additional filters to the file itself that dictate which details will be ignored when generating output from the reverse engineering file. We would not recommend trying to get to grips too closely with the reverse engineering tool until you have some experience in creating mappings manually the various settings will seem quite opaque when taken out of context. Once you have created a few simple entity models from scratch, however, the need for the various options should become clearer. In order to actually generate output from the tool, you will need to create a Hibernate code generation configuration by selecting the option in the toolbar of either perspective, as shown in Figure B-24. Figure B-24. Selecting the Hibernate Code Generation configuration tool nNote At the time of writing, the Hibernate Code Generation icon does not seem to be added to the Eclipse toolbar when you install the plug-in. This will probably be fixed by the time you read this book, but if not, you may have to use the JBoss IDE download or an Ant task if you want to take advantage of these features. The Code Generation tool allows you to create various different configurations for generating files based upon your reverse engineering XML file(s). The tool can output the following: Ordinary POJOs POJOs using generics POJOs using EJB 3 annotations (with or without generics) DAO objects XML mapping files Hibernate configuration files HTML documentation of the database schema It also permits you to create a JBoss Seam skeleton application, but this functionality lies well outside the scope of this book.
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.

Web site development - 282 APPENDIX B n HIBERNATE TOOLS APPENDIX B

Wednesday, September 19th, 2007

282 APPENDIX B n HIBERNATE TOOLS APPENDIX B n HIBERNATE TOOLS jdbc:hsqldb:file:exampledb;SHUTDOWN=true sa org.hibernate.dialect.HSQLDialect The Reverse Engineering and Code Generation Tools The last Hibernate Tools wizard is for the reverse engineering tool. This allows you to extract the schema information from the database into an XML file. Additional information can be added to this file using a tabbed control panel, which allows it to be used in the generation of source code using the Hibernate Code Generation tool (accessed separately from the wizard). This wizard is accessed from the File New Hibernate Reverse Engineering File menu option from within the Hibernate Console perspective. This then prompts you to specify a path and file name for the generated reverse engineering file. Select the default file name and location within the ToolsExample project. When you are done, you have the option of selecting either Finish or Next. If you select Finish, the wizard will assume that you want to use the details of all the tables in your database when creating the reverse engineering file. Alternatively, if you click Next, you have the opportunity to specify the individual tables and columns that should be included (as shown in Figure B-23). Figure B-23. Selecting the tables to reverse engineer
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

APPENDIX B (Web hosting compare) n HIBERNATE TOOLS 281 n HIBERNATE

Tuesday, September 18th, 2007

APPENDIX B n HIBERNATE TOOLS 281 n HIBERNATE TOOLS 281 Figure B-22. Selecting default values for the configuration file All the values selected in the drop-down menus can be overtyped if the option you need is not listed. (Note that we have amended the connection URL with our preferred HSQLDB connection details.) You can type in the fully qualified dialect class name instead of choosing from the short names available from the drop-down list. At this point, you also have the option of selecting the Create a console configuration check box. If you do so, the Hibernate Configuration File wizard will pass you to the Hibernate Console Configuration wizard (and will automatically populate the configuration file field). However, since we ve already discussed this earlier in the chapter, leave the check box empty and click Finish. The resulting example.cfg.xml file is shown in the following code (slightly reformatted to ensure that it fits on the page). Unlike the generated mapping files, this configuration file is pretty much ready to use if you plan to use programmatic configuration of your application, or if you are using it purely as the basis of a Hibernate Console configuration to connect to an existing Hibernate database.
org.hsqldb.jdbcDriver
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

280 APPENDIX (Web server on xp) B n HIBERNATE TOOLS APPENDIX B

Monday, September 17th, 2007

280 APPENDIX B n HIBERNATE TOOLS APPENDIX B n HIBERNATE TOOLS Generating a Mapping File Hibernate Tools provides a wizard to make the creation of the boilerplate XML mapping file contents simpler. You can access the wizard via the File New Hibernate XML Mapping File menu option from within the Hibernate Console perspective. This brings up a standard Eclipse view in which you select the name and location of the generated mapping file. Clicking Next then brings you to a dialog that requires you to enter the fully qualified name of the class that the mapping file will represent. If you cannot recall the specific package and class name details, you can use the Browse button to select the class from the class hierarchy. The boilerplate mapping file will be created in the appropriate location. The wizard does not currently populate this with any of the property or association mapping details. The generated file is therefore incomplete and can only be used as a skeleton to be filled in manually. The sample output for our example Bookshelf class (which is not annotated) follows. The Hibernate Tools plug-in for Eclipse includes an editor for its various mapping files that provides context-sensitive completion this is based upon the Web Tools Platform s (WTP) support for XML files, but adds awareness of mapping file details beyond the support for the basic DTD description that WTP provides. Generating a Configuration File Hibernate Tools provides another wizard to aid in the creation of a Hibernate configuration XML file. This wizard is accessed from the File New Hibernate Configuration File menu option from within the Hibernate Console perspective. This wizard prompts you (as with the mapping file wizard) to specify a path and file name for the generated configuration file. You don t want to overwrite the configuration file used by our sample application, so for this exercise, specify a file name of example.hbm.xmlin the root of the project directory. Clicking the Next button will then take you to the dialog shown in Figure B-22, in which all the basic configuration properties can be specified. When possible, a set of default options are provided as combo box lists. The Database dialect field presents the dialects in a more human-readable format, and also filters the Driver class and Connection URL fields to the likely options.
If you are in need for chaep and reliable webhost to host your website, our recommendation is http web server services.