⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tutorial.pot

📁 hibernate-distribution-3.3.1.GA-dist.zip源码
💻 POT
📖 第 1 页 / 共 5 页
字号:
#: tutorial.xml:229#, no-c-formatmsgid "Finally we include declarations for the persistent properties of the class in the mapping file. By default, no properties of the class are considered persistent:"msgstr ""#. Tag: programlisting#: tutorial.xml:235#, no-c-formatmsgid ""      "<![CDATA[\n"      "<hibernate-mapping>\n"      "\n"      "    <class name=\"events.Event\" table=\"EVENTS\">\n"      "        <id name=\"id\" column=\"EVENT_ID\">\n"      "            <generator class=\"native\"/>\n"      "        </id>\n"      "        <property name=\"date\" type=\"timestamp\" column=\"EVENT_DATE\"/>\n"      "        <property name=\"title\"/>\n"      "    </class>\n"      "\n"      "</hibernate-mapping>]]>"msgstr ""#. Tag: para#: tutorial.xml:237#, no-c-formatmsgid "Just as with the <literal>id</literal> element, the <literal>name</literal> attribute of the <literal>property</literal> element tells Hibernate which getter and setter methods to use. So, in this case, Hibernate will look for <literal>getDate()/setDate()</literal>, as well as <literal>getTitle()/setTitle()</literal>."msgstr ""#. Tag: para#: tutorial.xml:244#, no-c-formatmsgid "Why does the <literal>date</literal> property mapping include the <literal>column</literal> attribute, but the <literal>title</literal> doesn't? Without the <literal>column</literal> attribute Hibernate by default uses the property name as the column name. This works fine for <literal>title</literal>. However, <literal>date</literal> is a reserved keyword in most database, so we better map it to a different name."msgstr ""#. Tag: para#: tutorial.xml:253#, no-c-formatmsgid "The next interesting thing is that the <literal>title</literal> mapping also lacks a <literal>type</literal> attribute. The types we declare and use in the mapping files are not, as you might expect, Java data types. They are also not SQL database types. These types are so called <emphasis>Hibernate mapping types</emphasis>, converters which can translate from Java to SQL data types and vice versa. Again, Hibernate will try to determine the correct conversion and mapping type itself if the <literal>type</literal> attribute is not present in the mapping. In some cases this automatic detection (using Reflection on the Java class) might not have the default you expect or need. This is the case with the <literal>date</literal> property. Hibernate can't know if the property (which is of <literal>java.util.Date</literal>) should map to a SQL <literal>date</literal>, <literal>timestamp</literal>, or <literal>time</literal> column. We preserve full date and time information by mapping the property with a <literal>timestamp</literal> converter."msgstr ""#. Tag: para#: tutorial.xml:269#, no-c-formatmsgid "This mapping file should be saved as <literal>Event.hbm.xml</literal>, right in the directory next to the <literal>Event</literal> Java class source file. The naming of mapping files can be arbitrary, however the <literal>hbm.xml</literal> suffix is a convention in the Hibernate developer community. The directory structure should now look like this:"msgstr ""#. Tag: programlisting#: tutorial.xml:277#, no-c-formatmsgid ""      "<![CDATA[.\n"      "+lib\n"      "  <Hibernate and third-party libraries>\n"      "+src\n"      "  +events\n"      "    Event.java\n"      "    Event.hbm.xml]]>"msgstr ""#. Tag: para#: tutorial.xml:279#, no-c-formatmsgid "We continue with the main configuration of Hibernate."msgstr ""#. Tag: title#: tutorial.xml:286#, no-c-formatmsgid "Hibernate configuration"msgstr ""#. Tag: para#: tutorial.xml:288#, no-c-formatmsgid "We now have a persistent class and its mapping file in place. It is time to configure Hibernate. Before we do this, we will need a database. HSQL DB, a java-based SQL DBMS, can be downloaded from the HSQL DB website(http://hsqldb.org/). Actually, you only need the <literal>hsqldb.jar</literal> from this download. Place this file in the <literal>lib/</literal> directory of the development folder."msgstr ""#. Tag: para#: tutorial.xml:296#, no-c-formatmsgid "Create a directory called <literal>data</literal> in the root of the development directory - this is where HSQL DB will store its data files. Now start the database by running <literal>java -classpath ../lib/hsqldb.jar org.hsqldb.Server</literal> in this data directory. You can see it start up and bind to a TCP/IP socket, this is where our application will connect later. If you want to start with a fresh database during this tutorial, shutdown HSQL DB (press <literal>CTRL + C</literal> in the window), delete all files in the <literal>data/</literal> directory, and start HSQL DB again."msgstr ""#. Tag: para#: tutorial.xml:306#, no-c-formatmsgid "Hibernate is the layer in your application which connects to this database, so it needs connection information. The connections are made through a JDBC connection pool, which we also have to configure. The Hibernate distribution contains several open source JDBC connection pooling tools, but will use the Hibernate built-in connection pool for this tutorial. Note that you have to copy the required library into your classpath and use different connection pooling settings if you want to use a production-quality third party JDBC pooling software."msgstr ""#. Tag: para#: tutorial.xml:316#, no-c-formatmsgid "For Hibernate's configuration, we can use a simple <literal>hibernate.properties</literal> file, a slightly more sophisticated <literal>hibernate.cfg.xml</literal> file, or even complete programmatic setup. Most users prefer the XML configuration file:"msgstr ""#. Tag: programlisting#: tutorial.xml:322#, no-c-formatmsgid ""      "<![CDATA[<?xml version='1.0' encoding='utf-8'?>\n"      "<!DOCTYPE hibernate-configuration PUBLIC\n"      "        \"-//Hibernate/Hibernate Configuration DTD 3.0//EN\"\n"      "        \"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd\">\n"      "\n"      "<hibernate-configuration>\n"      "\n"      "    <session-factory>\n"      "\n"      "        <!-- Database connection settings -->\n"      "        <property name=\"connection.driver_class\">org.hsqldb.jdbcDriver</property>\n"      "        <property name=\"connection.url\">jdbc:hsqldb:hsql://localhost</property>\n"      "        <property name=\"connection.username\">sa</property>\n"      "        <property name=\"connection.password\"></property>\n"      "\n"      "        <!-- JDBC connection pool (use the built-in) -->\n"      "        <property name=\"connection.pool_size\">1</property>\n"      "\n"      "        <!-- SQL dialect -->\n"      "        <property name=\"dialect\">org.hibernate.dialect.HSQLDialect</property>\n"      "\n"      "        <!-- Enable Hibernate's automatic session context management -->\n"      "        <property name=\"current_session_context_class\">thread</property>\n"      "\n"      "        <!-- Disable the second-level cache  -->\n"      "        <property name=\"cache.provider_class\">org.hibernate.cache.NoCacheProvider</property>\n"      "\n"      "        <!-- Echo all executed SQL to stdout -->\n"      "        <property name=\"show_sql\">true</property>\n"      "\n"      "        <!-- Drop and re-create the database schema on startup -->\n"      "        <property name=\"hbm2ddl.auto\">create</property>\n"      "\n"      "        <mapping resource=\"events/Event.hbm.xml\"/>\n"      "\n"      "    </session-factory>\n"      "\n"      "</hibernate-configuration>]]>"msgstr ""#. Tag: para#: tutorial.xml:324#, no-c-formatmsgid "Note that this XML configuration uses a different DTD. We configure Hibernate's <literal>SessionFactory</literal> - a global factory responsible for a particular database. If you have several databases, use several <literal>&lt;session-factory&gt;</literal> configurations, usually in several configuration files (for easier startup)."msgstr ""#. Tag: para#: tutorial.xml:332#, no-c-formatmsgid "The first four <literal>property</literal> elements contain the necessary configuration for the JDBC connection. The dialect <literal>property</literal> element specifies the particular SQL variant Hibernate generates. Hibernate's automatic session management for persistence contexts will come in handy as you will soon see. The <literal>hbm2ddl.auto</literal> option turns on automatic generation of database schemas - directly into the database. This can of course also be turned off (by removing the config option) or redirected to a file with the help of the <literal>SchemaExport</literal> Ant task. Finally, we add the mapping file(s) for persistent classes to the configuration."msgstr ""#. Tag: para#: tutorial.xml:345#, no-c-formatmsgid "Copy this file into the source directory, so it will end up in the root of the classpath. Hibernate automatically looks for a file called <literal>hibernate.cfg.xml</literal> in the root of the classpath, on startup."msgstr ""#. Tag: title#: tutorial.xml:354#, no-c-formatmsgid "Building with Ant"msgstr ""#. Tag: para#: tutorial.xml:356#, no-c-formatmsgid "We'll now build the tutorial with Ant. You will need to have Ant installed - get it from the <ulink url=\"http://ant.apache.org/bindownload.cgi\">Ant download page</ulink>. How to install Ant will not be covered here. Please refer to the <ulink url=\"http://ant.apache.org/manual/index.html\">Ant manual</ulink>. After you have installed Ant, we can start to create the buildfile. It will be called <literal>build.xml</literal> and placed directly in the development directory."msgstr ""#. Tag: para#: tutorial.xml:365#, no-c-formatmsgid "A basic build file looks like this:"msgstr ""#. Tag: programlisting#: tutorial.xml:369#, no-c-formatmsgid ""      "<![CDATA[<project name=\"hibernate-tutorial\" default=\"compile\">\n"      "\n"      "    <property name=\"sourcedir\" value=\"${basedir}/src\"/>\n"      "    <property name=\"targetdir\" value=\"${basedir}/bin\"/>\n"      "    <property name=\"librarydir\" value=\"${basedir}/lib\"/>\n"      "\n"      "    <path id=\"libraries\">\n"      "        <fileset dir=\"${librarydir}\">\n"      "            <include name=\"*.jar\"/>\n"      "        </fileset>\n"      "    </path>\n"      "\n"      "    <target name=\"clean\">\n"      "        <delete dir=\"${targetdir}\"/>\n"      "        <mkdir dir=\"${targetdir}\"/>\n"      "    </target>\n"      "\n"      "    <target name=\"compile\" depends=\"clean, copy-resources\">\n"      "      <javac srcdir=\"${sourcedir}\"\n"      "             destdir=\"${targetdir}\"\n"      "             classpathref=\"libraries\"/>\n"      "    </target>\n"      "\n"      "    <target name=\"copy-resources\">\n"      "        <copy todir=\"${targetdir}\">\n"      "            <fileset dir=\"${sourcedir}\">\n"      "                <exclude name=\"**/*.java\"/>\n"      "            </fileset>\n"      "        </copy>\n"      "    </target>\n"      "\n"      "</project>]]>"msgstr ""#. Tag: para#: tutorial.xml:371#, no-c-formatmsgid "This will tell Ant to add all files in the lib directory ending with <literal>.jar</literal> to the classpath used for compilation. It will also copy all non-Java source files to the target directory, e.g. configuration and Hibernate mapping files. If you now run Ant, you should get this output:"msgstr ""#. Tag: programlisting#: tutorial.xml:378#, no-c-formatmsgid ""      "<![CDATA[C:\\hibernateTutorial\\>ant\n"      "Buildfile: build.xml\n"      "\n"      "copy-resources:\n"      "     [copy] Copying 2 files to C:\\hibernateTutorial\\bin\n"      "\n"      "compile:\n"      "    [javac] Compiling 1 source file to C:\\hibernateTutorial\\bin\n"      "\n"      "BUILD SUCCESSFUL\n"      "Total time: 1 second ]]>"msgstr ""#. Tag: title#: tutorial.xml:383#, no-c-formatmsgid "Startup and helpers"msgstr ""#. Tag: para#: tutorial.xml:385#, no-c-formatmsgid "It's time to load and store some <literal>Event</literal> objects, but first we have to complete the setup with some infrastructure code. We have to startup Hibernate. This startup includes building a global <literal>SessionFactory</literal> object and to store it somewhere for easy access in application code. A <literal>SessionFactory</literal> can open up new <literal>Session</literal>'s. A <literal>Session</literal> represents a single-threaded unit of work, the <literal>SessionFactory</literal> is a thread-safe global object, instantiated once."msgstr ""#. Tag: para#: tutorial.xml:395#, no-c-formatmsgid "We'll create a <literal>HibernateUtil</literal> helper class which takes care of startup and makes accessing a <literal>SessionFactory</literal> convenient. Let's have a look at the implementation:"msgstr ""#. Tag: programlisting#: tutorial.xml:401#, no-c-formatmsgid ""      "<![CDATA[package util;\n"      "\n"      "import org.hibernate.*;\n"      "import org.hibernate.cfg.*;\n"      "\n"      "public class HibernateUtil {\n"      "\n"      "    private static final SessionFactory sessionFactory;\n"      "\n"      "    static {\n"      "        try {\n"      "            // Create the SessionFactory from hibernate.cfg.xml\n"      "            sessionFactory = new Configuration().configure().buildSessionFactory();\n"      "        } catch (Throwable ex) {\n"      "            // Make sure you log the exception, as it might be swallowed\n"      "            System.err.println(\"Initial SessionFactory creation failed.\" + ex);\n"      "            throw new ExceptionInInitializerError(ex);\n"      "        }\n"      "    }\n"      "\n"      "    public static SessionFactory getSessionFactory() {\n"      "        return sessionFactory;\n"      "    }\n"      "\n"      "}]]>"msgstr ""#. Tag: para#: tutorial.xml:403#, no-c-formatmsgid "This class does not only produce the global <literal>SessionFactory</literal> in its static initializer (called once by the JVM when the class is loaded), but also hides the fact that it uses a static singleton. It might as well lookup the <literal>SessionFactory</literal> from JNDI in an application server."msgstr ""#. Tag: para#: tutorial.xml:410#, no-c-formatmsgid "If you give the <literal>SessionFactory</literal> a name in your configuration file, Hibernate will in fact try to bind it to JNDI after it has been built. To avoid this code completely you could also use JMX deployment and let the JMX-capable container instantiate and bind a <literal>HibernateService</literal> to JNDI. These advanced options are discussed in the Hibernate reference documentation."

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -