| | 60 | |
| | 61 | <heading>Persistence with Hibernate</heading> |
| | 62 | |
| | 63 | <para>The above examples provide basic persistence via a file based |
| | 64 | persistence layer. To use a persistence layer based on Hibernate a small |
| | 65 | amount of configuration is required. First copy across the hibernate |
| | 66 | configuration file - <path>hibernate.cfg.xml</path> - from the |
| | 67 | distribution's resource directory. In its newly copied state this file |
| | 68 | specifies that the database is HSQLDB, but it does contain other |
| | 69 | configurations that can be uncommented so you can use other databases such |
| | 70 | as MySQL and SQLServer.</para> |
| | 71 | |
| | 72 | <command-listing>resources/ |
| | 73 | <em>hibernate.cfg.xml</em> |
| | 74 | :</command-listing> |
| | 75 | |
| | 76 | <para>The only other task is to list the classes that need to be mapped to |
| | 77 | database tables so that object store can set up the database and the |
| | 78 | Hibernate mappings for you. This list need only name the root objects as any |
| | 79 | reachable class will also be mapped. This can be added to the |
| | 80 | <path>nakedobjects.properties</path> file for simplicity, or a new file, |
| | 81 | <path>hibernate.properties</path>, can be added to the <path>config</path> |
| | 82 | directory, which will only be loaded when the Hibernate persistor is used. |
| | 83 | The following properties setting simply register two classes that are to be |
| | 84 | persisted using Hibernate.</para> |
| | 85 | |
| | 86 | <property-listing>nakedobjects.persistence.hibernate.classes = example.dom.Contact, example.dom.PhoneNumber</property-listing> |
| | 87 | |
| | 88 | <para>To run Naked Objects with the Hibernate persistor change the persistor |
| | 89 | type to <em>hibernate</em> when starting up the NOF. The following shows the |
| | 90 | integrated web server from above being run with the Hibernate |
| | 91 | persistor.</para> |
| | 92 | |
| | 93 | <command-listing>$ run.sh --type server --viewer html <em>--persistor hibernate</em></command-listing> |
| | 94 | |
| | 95 | <para>During start up the Hibernate persistor will generate Hibernate |
| | 96 | mapping files and add them to a newly created directory called |
| | 97 | <path>mappings</path>. Assuming that you used the default configuration in |
| | 98 | <path>hibernate.cfg.xml</path> the HSQLDB engine will be used and a |
| | 99 | directory, <path>hsql</path>, for its files will be created. </para> |
| | 100 | |
| | 101 | <heading>Adding support for internationalisation</heading> |
| | 102 | |
| | 103 | <para>All the text that appears to the user comes from the domain object |
| | 104 | model. Text within the code can be externalised and so localised via the |
| | 105 | resource bundles that Java allows you to work with. However as all field and |
| | 106 | menu names are derived from the method names the framework provides a |
| | 107 | mechanism to adapt these names using resource bundles as well.</para> |
| | 108 | |
| | 109 | <subheading>Localisation files</subheading> |
| | 110 | |
| | 111 | <para>Create a file for each language to be provided for, e.g., |
| | 112 | <path>i18n_en_GB.properties</path> for English in Great Britain, and place |
| | 113 | them into project where they will be included in the class path. For the |
| | 114 | project structures we have already seen the files could be added to the |
| | 115 | <path>resources</path> directory as the following example shows.</para> |
| | 116 | |
| | 117 | <command-listing>resources/ |
| | 118 | <em>i18n_en_GB.properties |
| | 119 | i18n_de_DE.properties |
| | 120 | i18n_fr_FR.properties</em> |
| | 121 | </command-listing> |
| | 122 | |
| | 123 | <para>The following example is part of one of those translation file and |
| | 124 | shows how property and action names and descriptions can be |
| | 125 | specified.</para> |
| | 126 | |
| | 127 | <property-listing>example.dom.Contact.property.FullName.name=Nom |
| | 128 | example.dom.Contact.property.Phone.name=Téléphones |
| | 129 | example.dom.Contact.action.CreatePhone.name=Nouveau téléphone |
| | 130 | example.dom.Contact.property.FullName.description=Le nom complet du client</property-listing> |
| | 131 | |
| | 132 | <subheading>Configuration</subheading> |
| | 133 | |
| | 134 | <para>To make this work the reflector needs to be decorated to look up the |
| | 135 | translated names every time the framework needs information about the fields |
| | 136 | and menus. To include the peer add the following to a properties |
| | 137 | files.</para> |
| | 138 | |
| | 139 | <property-listing>nakedobjects.reflector.peers=resource-i18n</property-listing> |
| | 140 | |
| | 141 | <para>If the server does not need to do any localisation - that is it is not |
| | 142 | running the web server - then this property is best specified in the |
| | 143 | <path>client.properties</path> so it is only read in by the client.</para> |