Archive for September, 2007

APPENDIX B n HIBERNATE TOOLS 279 n (Web hosting mysql) HIBERNATE

Sunday, September 16th, 2007

APPENDIX B n HIBERNATE TOOLS 279 n HIBERNATE TOOLS 279 With the query still in the scratch pad, click the icon shaped like the letter P (shown in Figure B-20, just to the right of the Outline tab). A new row will be added to the view, representing the parameter in the query. By editing the values in this row, you can change the details of the parameter (both the value assigned to it and the type of that value). Figure B-20. Assigning a typed value to the id query parameter If you edit the query parameter row so that it contains the data shown in Figure B-20, running the query will return results equivalent to those produced by the following code fragment: Query query = session.createQuery( “select owner from Notepad where owner = :username”); query.setString(”username”,”Dave Minter”); List results = query.list(); All the standard Hibernate types are available to you, and the console will log appropriate errors when you try to execute queries that combine types in inappropriate ways for the object model. The Entity Model If you select the Hibernate Entity Model view (usually located as a tab option in the view at the bottom right-hand corner of the Hibernate Console perspective) for the project, and then select the Configuration node of the configuration view, a graphical representation of the entities in the model will be displayed in the Entity Model view, as shown in Figure B-21. Figure B-21. The Entity Model view of our model The view allows you to place the entities using the mouse or lay them out automatically. You can zoom into or out of the view, and you can print its contents. At the time of writing, this feature is quite primitive and is therefore of limited use.
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

Web hosting support - 278 APPENDIX B n HIBERNATE TOOLS APPENDIX B

Saturday, September 15th, 2007

278 APPENDIX B n HIBERNATE TOOLS APPENDIX B n HIBERNATE TOOLS The generated SQL will be visible whenever the HQL query is syntactically valid, allowing you to confirm the basic correctness of the HQL and confirm the sanity of the underlying query at the same time. Once you have a satisfactory HQL query, it can be executed against the database to validate the returned results. Enter the following query in the scratch pad window, and then click the green play button above the editor window. select note as Note_Entry from Note whereid > 10andid < 20 The view shown in Figure B-19 will list the results of the query. Note that the results have been given the correct column heading. Figure B-19. The output from the query Query Parameters While the scratch pad alone is adequate for running stand-alone queries, much of the HQL that you will use in practice in your applications will be parameterized. Consider the following typical query: select owner from Notepad where owner = :username This query uses a named parameter, username, as a placeholder for the actual value. When converted to SQL, this will become a parameter in a prepared statement, and will not be specified until the application is actually running. The Hibernate Console provides the novel feature of allowing you to specify the values of parameters in Hibernate queries of this type.2 This means that you can cut and paste an HQL query from your code into the scratch pad, and test it there without having to edit any of the parameters themselves. If you paste the preceding query into a scratch pad window and try to run it as is, the console will log an appropriate error, warning you that the query parameters have not all been set (currently this appears in the Hibernate Query Result view). On the right-hand side of the perspective s default layout, there is a view called Query Parameters. Adjust the layout so that you can see the three columns within this view, labeled Name, Type, and Value. 2. If you ve ever encountered problems debugging complex SQL queries, in which the process of manually substituting in parameters disguised the cause of the problem during testing, you will understand why we are extremely enthusiastic about this ingenious feature!
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

Yahoo web space - APPENDIX B n HIBERNATE TOOLS 277 n HIBERNATE

Friday, September 14th, 2007

APPENDIX B n HIBERNATE TOOLS 277 n HIBERNATE TOOLS 277 Testing HQL Queries HQL is a powerful way to interact with the database; but traditionally, debugging errors in these queries has been a long-winded process. Typically, a developer would end up creating a unit test for the query and then spending considerable time tweaking it into working correctly. Hibernate Tools now provides a query editor that allows you to prototype your HQL queries and run them against the database without needing to write any code. To run an HQL query, select HQL Scratchpad from the context menu for the project in the configuration view (by default, this is to the left of the main window in the Hibernate Console perspective). A new editor window will be created and labeled with HQL and the name of the project in our example, it s HQL: ToolsExample. This is an HQL editor with some context sensitivity. Context sensitivity is activated with the Ctrl+Space key combination, as is usual for Eclipse editors. For example, enter the following query in the scratch pad editor and press Ctrl+Space: from Note This will display a context menu offering the two class names that are legal at this point in the query (as shown in Figure B-17). Figure B-17. The context-sensitive scratch pad nCaution At the time of writing, there are various bugs related to saving scratch pads to physical files, and to running HQL queries loaded-in directly from text files. As a workaround, it is possible to copy and paste between a scratch pad window and a normal text editor view in order to run queries. It is likely that these problems will have been addressed by the time you read this. As you create the query, the corresponding SQL query against the underlying relational database is updated in the Hibernate Dynamic SQL Preview window shown in Figure B-18. Figure B-18. The Hibernate Dynamic SQL Preview window
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

276 APPENDIX B n HIBERNATE TOOLS APPENDIX B (Web hosting rating)

Thursday, September 13th, 2007

276 APPENDIX B n HIBERNATE TOOLS APPENDIX B n HIBERNATE TOOLS Now run the application. You should see the following output, which confirms that a Notepad entity and 100 associated Note entities have been persisted to the database: Creating test notepad… Test notepad created. You can now switch back to the Hibernate Console perspective in order to browse this data. Browsing the Model Back in the Hibernate Console perspective, you can access the contents of the object model using the Session Factory node of the console configuration view. This node is named after the SessionFactory object created and maintained internally by the Hibernate Eclipse plug-in tools when you drill down into the console configuration view. nTip If you manage to put the internal SessionFactory object into a bad state, it is possible to close and re-create it from the configuration view s context menu when the configuration view is collapsed to a single node. Opening the Session factory node, you will see nodes representing each of the mapped classes. Drilling down further, you can see a representation of the mapped fields and attributes (along with a graphical representation of the association rules, primary keys, and so on, where appropriate). If you double-click the mapped class, all the instances of the class in the database will be retrieved, and a toString representation will be displayed in the Hibernate Query Result view. As you can see from Figure B-16, the generated HQL query to produce this information is shown in a second tabbed pane (further queries will be added as new tabs within this window). The column is numbered, as no column name can be specified. Note that we have overridden the Notepad class s toString()method to ensure that a human-readable representation of the class contents is displayed in this view. Figure B-16. The Hibernate Query Result view of the mapped Notepadclass All results of queries generated manually or automatically through the Hibernate Console perspective will be displayed in the Hibernate Query Result view.
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.

Sri lanka web server - APPENDIX B n HIBERNATE TOOLS 275 n HIBERNATE

Wednesday, September 12th, 2007

APPENDIX B n HIBERNATE TOOLS 275 n HIBERNATE TOOLS 275 nCaution There are currently no safeguards built in here. If the configured database user has table deletion privileges, running SchemaExport in this way will drop and re-create all of your tables with no further warning! If you select Run SchemaExport from this menu, the database view will be updated to reflect the changes, and you will be able to drill down to view the individual table column names and data types, as shown in Figure B-15. Figure B-15. A column-level view of the newly generated database nNote When you run SchemaExport for the first time, you may see error messages in the event log view related to attempts to drop nonexistent tables. These result from unconditional DROP commands to remove preexisting tables. They should go away if you run SchemaExport a second time and assuming this is the case, they can be safely ignored. As always, given that the tools are still beta versions at the time of writing, this behavior may have improved by the time you read this book. This view does not offer the sort of comprehensive access to the database metadata that is available from some tools, and it does not permit you to directly change any of the features. It does, however, offer a useful visual confirmation that the appropriate tables and columns have been created. Running the Test Program For the purpose of generating some test data for manipulation in the remainder of this section, we have created a test application (available with the other source code for this book on the Apress web site) to populate the database with some suitable records. Now that you have created an appropriate database schema, it is possible to run this application. Switch back to the Java perspective and add a new Java Application run configuration for the PopulateNotesclass. You will need to add the path to your JDBC driver to the configuration on the Classpath tab.
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

274 APPENDIX B n HIBERNATE TOOLS APPENDIX B (Web site builder)

Tuesday, September 11th, 2007

274 APPENDIX B n HIBERNATE TOOLS APPENDIX B n HIBERNATE TOOLS Click the Finish button, and you should now be able to see the configuration name in the console configuration view. Generating the Schema Expand the ToolsExample entry that you created in the console configuration window. There will be three nodes beneath it, as shown in Figure B-12. Figure B-12. The three nodes beneath ToolsExample The third of these is a view of the tables in the database that correspond to the entities mapped in your console configuration (either explicitly or via the hibernate.cfg.xml file). Unless your database contains preexisting tables, the database view will be empty when you attempt to drill down into it, as shown in Figure B-13. Figure B-13. The empty database view The Hibernate Console now has all of the entity mapping and database connection information. It therefore offers a Run SchemaExport context menu option (accessed from the Configuration node, not the Database node) to generate the appropriate tables (see Figure B-14). Figure B-14. The Run SchemaExport context menu option
If you are in need for chaep and reliable webhost to host your website, our recommendation is http web server services.

APPENDIX B n HIBERNATE TOOLS 273 n HIBERNATE (Web site translator)

Tuesday, September 11th, 2007

APPENDIX B n HIBERNATE TOOLS 273 n HIBERNATE TOOLS 273 Figure B-9. The Name and Configuration file settings The middle section of the form shown in Figure B-10 allows you to specify mapping files for inclusion in the console configuration that are not explicitly included in the configuration file. You would add entries here if your application added these options to the configuration at run time instead of drawing the information from the configuration file (for example, if it only used the properties file for its other configuration settings). Figure B-10. The additional mapping file settings The last part of the form shown in Figure B-11 allows you to set the classpath entries that will be used by the Hibernate Console. This must include the JDBC driver to access your database and the location of your mapped POJO classes. Figure B-11. Setting the classpath
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.

272 APPENDIX B n HIBERNATE TOOLS APPENDIX B (Web hosting)

Monday, September 10th, 2007

272 APPENDIX B n HIBERNATE TOOLS APPENDIX B n HIBERNATE TOOLS
org.hibernate.dialect.HSQLDialect sa 0 false If you are using the HSQLDB database in conjunction with Hibernate Tools, we recommend that you use fully qualified path names (shown in bold in the preceding code) to avoid possible clashes between the working directories and classpaths used by Ant, the Eclipse IDE, and the Hibernate Console. In our example, we will be working with the annotations-based mappings, so we use the mapping element with a class attribute to determine the mapped classes. However, the console configuration will automatically find any correctly annotated entity classes using reflection, so it is possible to omit these entries. If you choose to use XML-based mappings, you can use the file attribute to specify the mapping file instead, or specify the paths to the mapping files in the Hibernate configuration entry used by the Hibernate Console. Hibernate Tools provides a wizard to create the Hibernate configuration entry. To use the wizard, select the File New Hibernate Console Configuration menu option, as shown in Figure B-8. Figure B-8. Creating a new console configuration This will bring up the Create Hibernate Console Configuration dialog page. This consists of a simple form with three basic sections. In the first part of the form shown in Figure B-9, set a suitable name for this configuration and the path to the Hibernate configuration file. You should also tick the check box, as you will be using annotations for your mappings.
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

APPENDIX B n HIBERNATE TOOLS 271 n (Cheapest web hosting) HIBERNATE

Sunday, September 9th, 2007

APPENDIX B n HIBERNATE TOOLS 271 n HIBERNATE TOOLS 271 To open a new Eclipse perspective, you can either select the Window Open Perspective menu option or click the Open Perspective icon. Select the Other option from the resulting menu. This will present you with the list of available perspectives. Select the Hibernate Console option from the resulting dialog. Much as when entering the Eclipse debugging perspective, the layout of Eclipse will change, with various Hibernate-specific views being opened (although you will still have access to the Package Explorer as one of the tabbed options behind the new Hibernate configuration view). To switch between the perspectives, you can select the Java or Hibernate Console perspective icons on the top right of the main Eclipse window (you may need to adjust the menu layout slightly if these are obscured). Figure B-7 shows the icons for the Hibernate Console and Java perspectives, with the Java perspective selected. Figure B-7. The Java and Hibernate Console perspective icons, with the Java perspective currently selected Using the Hibernate Console With the project set up and the Hibernate Console perspective added to Eclipse, you can now start using some of the Hibernate Tools plug-in features more directly. Creating a Hibernate Console Configuration The Hibernate Console represents a view onto the Hibernate object store. It therefore needs the same basic configuration information as any other client of Hibernate. You will need a Hibernate XML configuration file and/or a properties file (see Listing B-1). Listing B-1. The Configuration File Used in Our Example: hibernate.cfg.xml
org.hsqldb.jdbcDriver jdbc:hsqldb:file:/workspace/ToolsExample/db/annotationsdb;shutdown=true If you are in need for chaep and reliable webhost to host your website, our recommendation is http web server services.

270 APPENDIX B n HIBERNATE TOOLS APPENDIX B (Adult web hosting)

Saturday, September 8th, 2007

270 APPENDIX B n HIBERNATE TOOLS APPENDIX B n HIBERNATE TOOLS Having created it initially, Hibernate allows you to export your user library configuration so that it does not need to be created afresh on each machine that you work on, or when creat ing a new Eclipse workspace. You should now create a new Java project called ToolsExample. The source code for the example is available from the Apress web site (www.apress.com). Copy the files into the ToolsExample folder in the workspace directory, and refresh the project. nTip If Eclipse is running under a 1.4 JVM or earlier, and you want to take advantage of annotations, you must reconfigure it to run under Java 5. Add the -vm flag to the Eclipse command line to force the named JVM to load Eclipse. For example, the shortcut to Eclipse on my Windows desktop contains the following target: C:eclipseeclipse.exe -vm c:homejdk1.5.0_03binjavaw At the time of writing, Hibernate Tools contains a minor bug that causes some problems if the configuration file is in the project root. We have therefore configured the project to contain separate src and bin directories. You should configure the project to use these as the source folder and default output folder, respectively. We expect this bug to be fixed by the time you read this book, but it is good practice to separate the directories in this way anyway. You now need to add the two user libraries to the project. Select the Build Path Configure Build Path context menu option on the project. Select the Libraries tab on the resulting dialog. Click the Add Library button, and you will be presented with a list of library types. Select the User Library option from the list, and click Next. This will in turn present you with a list of the available user libraries. Check both of the Hibernate libraries that you configured previously, and click Finish. The libraries will be added to the list in the Libraries tab of the Java Build Path dialog; you should click OK to accept the changes to the path. Figure B-6 shows the resulting uncluttered project view. Figure B-6. The configured Java project So far in this section, we have configured a Java project in a conventional way. None of the steps we have taken so far would be unusual in preparing to use a third-party library such as the Spring Framework. The next few steps open a Hibernate Console perspective that will allow you to manage the Hibernate-specific aspects of your project.
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.