Archive for October, 2007

Christian web host - Hibernate and Spring APPENDIX C n n n

Monday, October 8th, 2007

Hibernate and Spring APPENDIX C n n n Hibernate and Spring APPENDIX C n n n The Spring Application Framework offers developers an environment that ties together numerous APIs into a coherent whole. Spring applies the philosophy of dependency injection by providing appropriate configurable wrapper classes for all sorts of popular Java libraries. The standard Spring API is immense, and its standardized approach to dependency management means that any existing API can in principle become a Spring API. If you want a good introduction to using Spring, then we recommend the excellent Pro Spring, by Rob Harrop and Jan Machacek (Apress, 2005). For an overview, visit the Spring web site at http://springframework.org. In view of its scope, we cannot and do not make any attempt to teach you even the basics of the Spring Framework in this appendix instead, we assume that you are already familiar with Spring in general, and offer a focused introduction to the Hibernate-related components. Throughout this appendix, we refer to a simple sample application that represents a newsstand of papers consisting of sets of articles. At the end of this appendix, we include the complete Spring bean configuration file for the example application; and as with all the examples in this book, the entire application itself can be downloaded from the Apress web site (www.apress.com). Spring Libraries The Spring Framework essentially provides wrappers and utility classes for working with various other frameworks, as well as some of its own implementations. For example, it provides its own model-view-controller (MVC) patterned web application framework, and also supports Struts and vanilla JSPs. Spring is distributed as a small set of JAR files that contain the Spring-specific functionality. The third-party components are made available with the including dependencies distribution of Spring, but can also be downloaded independently from their respective web sites. It is possible to copy all the libraries that Spring is capable of using into the classpath, but this is a somewhat inelegant approach. We prefer to pick the core Spring JARs and add to them only what is necessary to support the application being built. For our example application, we have therefore included the following sets of libraries: The Hibernate JAR and its required dependencies The Hibernate Annotations JAR and its required dependencies
You want to have a cheap webhost for your apache application, then check apache web hosting services.

APPENDIX B n (Free php web host) HIBERNATE TOOLS 297 n HIBERNATE

Sunday, October 7th, 2007

APPENDIX B n HIBERNATE TOOLS 297 n HIBERNATE TOOLS 297

The task definition (as shown earlier in this section) would use the classpath with the ID classpath.tools. The tasks themselves will need access to two additional sets of resources: the configuration file(s) and the compiled classes.
The configuration files will include the hibernate.cfg.xml and/or hibernate.properties files, along with any log4j configuration files, cache configuration files, and applicable XML mapping files. If you are using annotations in any of your tasks, you will need to ensure that the task is assigned a dependency upon the compiled POJOs annotations cannot be read at run time from Java source files, only from compiled classes. Summary In this appendix, we have discussed the installation and use of Hibernate Tools, including the Eclipse plug-in and the Ant tasks. Together, these remove most of the need to manually create boilerplate configuration code. In Appendix C, we discuss how Hibernate can be used as the data access layer within the Spring Framework.
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

296 APPENDIX B n HIBERNATE TOOLS APPENDIX (Web hosting reseller) B

Saturday, October 6th, 2007

296 APPENDIX B n HIBERNATE TOOLS APPENDIX B n HIBERNATE TOOLS
In addition to any properties you add to the template task yourself, you will have access to the scripting variables listed in Table B-12. Table B-12. The Standard Scripting Variables Available to a Template Task Variable Description artifacts An instance of org.hibernate.tool.hbm2x.ArtifactCollector that can be populated with values to reflect the actions taken during output generation c2h An instance of the org.hibernate.tool.hbm2x.Cfg2HbmTool class providing helper methods for converting configuration object values into Hibernate mapping files c2j An instance of the org.hibernate.tool.hbm2x.Cfg2JavaToolclass providing helper methods for converting configuration object values into Java class files cfg A reference to the configuration object outputdir The path specified as the element s destdir attribute template_path A list of the paths to directories containing FreeMarker templates The standard Hibernate Tools exporter tasks are implemented in much the same way. Although we haven t shown this when discussing their attributes earlier, all the exporter tasks support the templatepath and templateprefix attributes, allowing you to override their default behavior by instructing them to use a different set of FreeMarker macros than those included in the hibernate-tools.jar file. All attributes also support the use of property sets to pass in information that is required by your custom macros but isn t available from the configuration object. A very simple FreeMarker script is shown in the following code. This is not very useful in itself, as the first four variables simply display their hashcode representations from the default toString() implementation, but it provides you with a syntactically valid starting point for exploration of the code generation tools. Configuration object: ${cfg} Artifacts object: ${artifacts} Cfg2Hbm Helper: ${c2h} Cfg2Java Helper: ${c2j} Output Directory: ${outputdir} Template path: ${template_path[0]} Configuring the Classpath There are two distinct classpaths to consider when setting up the Hibernate Tools Ant tasks: the classpath of the task definition, and the classpath to be used by the tasks. The task definition needs to have in its classpath the Hibernate Tools JAR file, the Hibernate libraries, the Hibernate Annotations libraries, and the JDBC driver that will be used to access the database. A typical configuration of this classpath is as follows:
We recommend high quality webhost to host and run your jsp application: christian web host services.

Web server application - APPENDIX B n HIBERNATE TOOLS 295 n HIBERNATE

Friday, October 5th, 2007

APPENDIX B n HIBERNATE TOOLS 295 n HIBERNATE TOOLS 295 nNote Earlier versions of Hibernate Tools used the Velocity template language instead of FreeMarker, but in other respects, they behaved in the same way. If the existing exporters do not meet your needs, you can specify your own additional code generation tasks using the task. Table B-11 shows the attributes that can be supplied to this task. Table B-11. The Properties Available to the Exporter Element Property Default Description destdir If set, overrides, for this exporter only, the destination directory specified on the Hibernate tool task. exporterclass Specifies a class to use to generate output. It will be invoked once only, and the configuration object, output directory, template path and prefix, and any configuration properties will be passed into it. filepattern When using templates, represents the FreeMarker macro that should be used to determine the file name for the entity being processed. template Specifies the template to use to generate output. It will be invoked for each of the entities in the configuration metamodel. templatepath Specifies the path from which your template(s) will be loaded, overriding any default location. templateprefix Specifies an optional prefix to your template file name within the template path. It is set by the standard tasks, so overriding this allows you to import your own tasks instead. Again, you have two options when carrying out this process. You can set the exporterclass attribute to the name of the class to be used to carry out the export process. This class must implement the org.hibernate.tool.hbm2x.Exporterinterface. This is passed a reference to the current configuration object and any other attributes that were set on the task. Alternatively, you can specify the name of a FreeMarker template to be used in processing the configuration object and the name of a prefix. Note that filepattern contains FreeMarker macros that will be expanded at run time to determine the appropriate file names for the tool s output. The task will search for this file on the classpath, and then as a file resource. If the configuration object does not contain some of the information that you need in order to produce the desired output, you can also specify additional arbitrary details using a standard Ant property or property set. Here s an example:
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.

294 APPENDIX B n HIBERNATE TOOLS APPENDIX B (Email web hosting)

Thursday, October 4th, 2007

294 APPENDIX B n HIBERNATE TOOLS APPENDIX B n HIBERNATE TOOLS The
element allows you to include and exclude groups of tables from the mapping process on the basis of pattern matches on their names. Where the element allows you to specify a set of tables matching a single pattern to be reverse engineered, the
element operates within this and allows multiple patterns to be applied to include and exclude tables. Here s an example:
Although the previous element included all tables within the current schema that matched the pattern NOTE*, this table filter excludes any tables that match the pattern NOTEPAD_ARCHIVE* from reverse engineering. Table filters are applied in order, so using this technique, you can build up a filter that only includes a specific set of tables. The

task permits almost total control over the mapping details of the entity. You can select a specific table by catalog, schema, and name. You can specify the class name that it should take, how the primary key column(s) relate to that class s properties, the primary key generator that it should use, the column types and properties that they are associated with, and the details of the associations formed by foreign key relationships. Our simple example places the generated entity into an appropriate class, with a nonstandard primary key property name and a nonstandard type mapping for one of the columns (note). It also excludes one of the columns (audit) from the entity model.

If it looks like you will have to manage the reverse engineering process to this level of detail, it may in fact be more appropriate to create some or all of the mapping files manually, which gives you total control over the specification of those entities. Complex specification of mapping information in the reverse engineering file is really only appropriate if it is for exceptional classes when the general cases are common; or if you expect to need to regenerate the model from the schema very frequently in response to changes in the schema details. Templates With the exception of and , all the Ant exporter tasks take the metamodel information from the configuration, pass it to a set of FreeMarker templates, and write the output to files. For more information on the FreeMarker template scripting language, see the FreeMarker site at http://freemarker.sourceforge.net.
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

APPENDIX B n HIBERNATE TOOLS 293 n HIBERNATE (Web site templates)

Wednesday, October 3rd, 2007

APPENDIX B n HIBERNATE TOOLS 293 n HIBERNATE TOOLS 293
The reverse engineering file in the preceding code limits the tables to be used when generating the mapping information from the schema to the three explicitly named tables (NOTE, NOTEPAD, and NOTEPAD_NOTE) in the database s public schema. A reverse engineering file always consists of a single top-level element containing various additional elements. These daughter elements are given in order in Table B-10: Table B-10. The Elements Used in Configuring the Reverse Engineering File Element Cardinality Description Zero or more Allows the reverse engineering process to be limited by catalog, schema, and table name Zero or one Allows you to override the default mapping between database types and Java types
Zero or more Allows you to include or exclude tables by catalog name, schema name, and table name, and allows them to be grouped into a particular package

Zero or more Allows you to override the default mappings of tables into entities Rather than trying to exhaustively specify the syntax of a reverse engineering file, which is anyway available through the DTD at http://hibernate.sourceforge.net/hibernate-reverseengineering- 3.0.dtd, we think it is easier to follow the basic requirements of the file format with some examples of valid , ,
, and
elements. Our first example specifies the following rule: tables should only be used for reverse engineering if they are in the public schema and their names begin with NOTE: Our next example enforces a rule that database INTEGERtypes for which the column is specified as NOT NULL must be represented using Hibernate s inttype. It also enforces a rule that database VARCHAR types that have a specified length of 250 should be treated as Hibernate string types. These type mappings apply throughout the reverse engineering process you cannot specify them on a per-table basis using the element, but you can using the
element.
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

292 APPENDIX B n HIBERNATE TOOLS APPENDIX B (Web site)

Tuesday, October 2nd, 2007

292 APPENDIX B n HIBERNATE TOOLS APPENDIX B n HIBERNATE TOOLS The reversestrategy attribute allows you to specify the fully qualified class name of a custom implementation of the org.hibernate.cfg.reveng.ReverseEngineeringStrategy interface. This interface defines methods that can be used to select the following: Class names from table details Property names from column details One-to-many associations from foreign key relationships Many-to-one associations from foreign key relationships Inverse relationships from foreign key relationships Lazy loading details from foreign key relationships Collection attribute names from foreign key relationships Entity names from foreign key relationships Tables and columns to include and exclude Primary key columns from table details The column to use for optimistic locking strategies Composite ID details from table details It also allows you to provide information about how schemas should be generated from the resulting metamodel. This information is as follows: Additional foreign key relationships The table naming strategy to be used The disadvantage of this approach to managing the reverse engineering process is that it is not particularly flexible, and it requires a lot of coding. Reverse engineering is often carried out only once to establish the mappings, with the schema thereafter being driven from the mappings, rather than being used to create them. The Hibernate tools therefore provide a second mechanism for controlling the reverse engineering process by specifying an XML configuration file using the revengfileattribute of the task. This provides nearly the same degree of control, but is much simpler to create especially if you intend to manipulate only minor details of the process. The output of the reverse engineering tool described in the The Reverse Engineering and Code Generation Tools section is actually a reverse engineering XML file following this format. A very simple example reverse engineering file is shown in the following code:
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

Photo web hosting - APPENDIX B n HIBERNATE TOOLS 291 n HIBERNATE

Tuesday, October 2nd, 2007

APPENDIX B n HIBERNATE TOOLS 291 n HIBERNATE TOOLS 291 In principle, the element allows you to specify an arbitrary HQL query that will be run against the database using the configuration s mapping information. Table B-9 shows the attributes that can be supplied. Table B-9. The Properties Available to the Exporter Element Property Default Description destdir If set, overrides, for this exporter only, the destination directory specified on the tools task. destfile If set, specifies the file into which the output of the queries will be written. If left unset, the query is carried out, but the output is not saved. The HQL query itself is included as the body of the element. select n.owner from Notepad n If you want to include multiple SQL queries in the task, you can include multiple nested elements thus: select n.owner from Notepad n select n.owner from Note n Reverse Engineering As we discussed in the previous section, the task can be used to create a configuration metamodel directly from a database schema. However, a database schema is not necessarily an exact match for the entity mappings that we want to create. It may contain tables and columns that we would prefer to omit from our entities. It may also lack some of the information that we want to express. For example, database types such as VARCHAR do not map exactly to Java types such as char[]and java.lang.String, and the tables will have names that do not conform to the Java naming conventions. Used as is, the task will select sensible defaults for the type names, and will assign the reverse-engineered tables suitable Java names derived from the table names. It also provides an attribute to allow you to directly specify a suitable package name. Even so, we would really like more control over the specifics of the reverse engineering process. Note that while it also provides a naming strategy attribute, this has no effect during the reverse engineering process, as naming strategy classes can only be used to determine schema names from mapping information, not vice versa. Hibernate provides two ways in which this process can be controlled you can specify one of two attributes on the task to override the default behavior.
You want to have a cheap webhost for your apache application, then check apache web hosting services.

290 APPENDIX B n HIBERNATE TOOLS APPENDIX B (Photoshop web design)

Monday, October 1st, 2007

290 APPENDIX B n HIBERNATE TOOLS APPENDIX B n HIBERNATE TOOLS This exporter can be used to permit the mapping file based creation of suitable classes, or to create classes from the database schema when is used. The element generates the XML mapping files from the information con tained in the metamodel. Table B-6 shows the attributes that can be supplied. Table B-6. 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 This exporter is particularly terse because it only writes out the mapping information stored in the metamodel. This is all handled by the appropriate configuration element. The exporter just needs to know which path to write the XML files into. And that can be specified at the Hibernate tool level. A fairly typical invocation of this exporter is, in its entirety, the ridiculously simple . The element generates HTML documentation of the schema and entities in a style similar to the familiar javadoc output. Table B-7 shows the attributes that can be supplied. Table B-7. The Attributes Available to the Exporter Element Property Default Description destdir If set, overrides, for this exporter only, the destination directory specified on the tools task The element generates a set of basic DAO classes one for each of the entities in the metadata. Table B-8 shows the attributes that can be supplied. Table B-8. The Attributes Available to the Exporter Element Property Default Description destdir If set, overrides, for this exporter only, the destination directory specified on the tools task By default, these DAO classes will be named after your entity, suffixed with Home. The generated DAOs provide a set of methods roughly corresponding to the methods available on the Session interface, but strongly tied to the entity type. While the generated DAOs can provide a useful foundation for your own more specific DAOs, we generally find that they offer little value beyond that already offered from the standard Session interactions. nNote Nothing in any of the exporters intrinsically stops you from generating silly combinations of output but this has its advantages; for example, it is possible to use an configuration element with the exporter to generate POJOs. While that might seem pointless, given that you have to start with POJOs to use an annotations-based configuration in the first place, it actually provides the useful ability to generate Java 1.4 compatible source code from annotated Java 5 class files!
We recommend high quality webhost to host and run your jsp application: christian web host services.