📄 tutorial.pot
字号:
# SOME DESCRIPTIVE TITLE.# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.##, fuzzymsgid ""msgstr """Project-Id-Version: PACKAGE VERSION\n""Report-Msgid-Bugs-To: http://bugs.kde.org\n""POT-Creation-Date: 2008-08-14 15:28+0000\n""PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n""Last-Translator: FULL NAME <EMAIL@ADDRESS>\n""Language-Team: LANGUAGE <kde-i18n-doc@kde.org>\n""MIME-Version: 1.0\n""Content-Type: application/x-xml2pot; charset=UTF-8\n""Content-Transfer-Encoding: 8bit\n"#. Tag: title#: tutorial.xml:33#, no-c-formatmsgid "Introduction to Hibernate"msgstr ""#. Tag: title#: tutorial.xml:36#, no-c-formatmsgid "Preface"msgstr ""#. Tag: para#: tutorial.xml:38#, no-c-formatmsgid "This chapter is an introduction to Hibernate by way of a tutorial, intended for new users of Hibernate. We start with a simple application using an in-memory database. We build the application in small, easy to understand steps. The tutorial is based on another, earlier one developed by Michael Gloegl. All code is contained in the <filename>tutorials/web</filename> directory of the project source."msgstr ""#. Tag: para#: tutorial.xml:51#, no-c-formatmsgid "This tutorial expects the user have knowledge of both Java and SQL. If you are new or uncomfortable with either, it is advised that you start with a good introduction to that technology prior to attempting to learn Hibernate. It will save time and effort in the long run."msgstr ""#. Tag: para#: tutorial.xml:61#, no-c-formatmsgid "There is another tutorial/example application in the <filename>/tutorials/eg</filename> directory of the project source. That example is console based and as such would not have the dependency on a servlet container to execute. The basic setup is the same as the instructions below."msgstr ""#. Tag: title#: tutorial.xml:71#, no-c-formatmsgid "Part 1 - The first Hibernate Application"msgstr ""#. Tag: para#: tutorial.xml:73#, no-c-formatmsgid "Let's assume we need a small database application that can store events we want to attend, and information about the host(s) of these events. We will use an in-memory, Java database named HSQLDB to avoid describing installation/setup of any particular database servers. Feel free to tweak this tutorial to use whatever database you feel comfortable using."msgstr ""#. Tag: para#: tutorial.xml:82#, no-c-formatmsgid "The first thing we need to do is set up our development environment, and specifically to setup all the required dependencies to Hibernate as well as other libraries. Hibernate is built using Maven which amongst other features provides <literal>dependecy management</literal>; moreover it provides <emphasis>transitive</emphasis> <literal>dependecy management</literal> which simply means that to use Hibernate we can simply define our dependency on Hibernate, Hibernate itself defines the dependencies it needs which then become transitive dependencies of our project."msgstr ""#. Tag: programlisting#: tutorial.xml:94#, no-c-formatmsgid "" "<![CDATA[.\n" "<project xmlns=\"http://maven.apache.org/POM/4.0.0\"\n" " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" " xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" "\n" " ...\n" "\n" " <dependencies>\n" " <dependency>\n" " <groupId>${groupId}</groupId>\n" " <artifactId>hibernate-core</artifactId>\n" " </dependency>\n" "\n" " <!-- Because this is a web app, we also have a dependency on the servlet api. -->\n" " <dependency>\n" " <groupId>javax.servlet</groupId>\n" " <artifactId>servlet-api</artifactId>\n" " </dependency>\n" " </dependencies>\n" "\n" "</project>]]>"msgstr ""#. Tag: para#: tutorial.xml:97#, no-c-formatmsgid "Essentially we are describing here the <filename>/tutorials/web/pom.xml</filename> file. See the <ulink url=\"http://maven.org\">Maven</ulink> site for more information."msgstr ""#. Tag: para#: tutorial.xml:105#, no-c-formatmsgid "While not strictly necessary, most IDEs have integration with Maven to read these POM files and automatically set up a project for you which can save lots of time and effort."msgstr ""#. Tag: para#: tutorial.xml:112#, no-c-formatmsgid "Next we create a class that represents the event we want to store in database."msgstr ""#. Tag: title#: tutorial.xml:117#, no-c-formatmsgid "The first class"msgstr ""#. Tag: para#: tutorial.xml:119#, no-c-formatmsgid "Our first persistent class is a simple JavaBean class with some properties:"msgstr ""#. Tag: programlisting#: tutorial.xml:123#, no-c-formatmsgid "" "<![CDATA[package org.hibernate.tutorial.domain;\n" "\n" "import java.util.Date;\n" "\n" "public class Event {\n" " private Long id;\n" "\n" " private String title;\n" " private Date date;\n" "\n" " public Event() {}\n" "\n" " public Long getId() {\n" " return id;\n" " }\n" "\n" " private void setId(Long id) {\n" " this.id = id;\n" " }\n" "\n" " public Date getDate() {\n" " return date;\n" " }\n" "\n" " public void setDate(Date date) {\n" " this.date = date;\n" " }\n" "\n" " public String getTitle() {\n" " return title;\n" " }\n" "\n" " public void setTitle(String title) {\n" " this.title = title;\n" " }\n" "}]]>"msgstr ""#. Tag: para#: tutorial.xml:125#, no-c-formatmsgid "You can see that this class uses standard JavaBean naming conventions for property getter and setter methods, as well as private visibility for the fields. This is a recommended design - but not required. Hibernate can also access fields directly, the benefit of accessor methods is robustness for refactoring. The no-argument constructor is required to instantiate an object of this class through reflection."msgstr ""#. Tag: para#: tutorial.xml:133#, no-c-formatmsgid "The <literal>id</literal> property holds a unique identifier value for a particular event. All persistent entity classes (there are less important dependent classes as well) will need such an identifier property if we want to use the full feature set of Hibernate. In fact, most applications (esp. web applications) need to distinguish objects by identifier, so you should consider this a feature rather than a limitation. However, we usually don't manipulate the identity of an object, hence the setter method should be private. Only Hibernate will assign identifiers when an object is saved. You can see that Hibernate can access public, private, and protected accessor methods, as well as (public, private, protected) fields directly. The choice is up to you and you can match it to fit your application design."msgstr ""#. Tag: para#: tutorial.xml:145#, no-c-formatmsgid "The no-argument constructor is a requirement for all persistent classes; Hibernate has to create objects for you, using Java Reflection. The constructor can be private, however, package visibility is required for runtime proxy generation and efficient data retrieval without bytecode instrumentation."msgstr ""#. Tag: para#: tutorial.xml:152#, no-c-formatmsgid "Place this Java source file in a directory called <literal>src</literal> in the development folder, and in its correct package. The directory should now look like this:"msgstr ""#. Tag: programlisting#: tutorial.xml:157#, no-c-formatmsgid "" "<![CDATA[.\n" "+lib\n" " <Hibernate and third-party libraries>\n" "+src\n" " +events\n" " Event.java]]>"msgstr ""#. Tag: para#: tutorial.xml:159#, no-c-formatmsgid "In the next step, we tell Hibernate about this persistent class."msgstr ""#. Tag: title#: tutorial.xml:166#, no-c-formatmsgid "The mapping file"msgstr ""#. Tag: para#: tutorial.xml:168#, no-c-formatmsgid "Hibernate needs to know how to load and store objects of the persistent class. This is where the Hibernate mapping file comes into play. The mapping file tells Hibernate what table in the database it has to access, and what columns in that table it should use."msgstr ""#. Tag: para#: tutorial.xml:175#, no-c-formatmsgid "The basic structure of a mapping file looks like this:"msgstr ""#. Tag: programlisting#: tutorial.xml:179#, no-c-formatmsgid "" "<![CDATA[<?xml version=\"1.0\"?>\n" "<!DOCTYPE hibernate-mapping PUBLIC\n" " \"-//Hibernate/Hibernate Mapping DTD 3.0//EN\"\n" " \"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">\n" "\n" "<hibernate-mapping>\n" "[...]\n" "</hibernate-mapping>]]>"msgstr ""#. Tag: para#: tutorial.xml:181#, no-c-formatmsgid "Note that the Hibernate DTD is very sophisticated. You can use it for auto-completion of XML mapping elements and attributes in your editor or IDE. You also should open up the DTD file in your text editor - it's the easiest way to get an overview of all elements and attributes and to see the defaults, as well as some comments. Note that Hibernate will not load the DTD file from the web, but first look it up from the classpath of the application. The DTD file is included in <literal>hibernate3.jar</literal> as well as in the <literal>src/</literal> directory of the Hibernate distribution."msgstr ""#. Tag: para#: tutorial.xml:192#, no-c-formatmsgid "We will omit the DTD declaration in future examples to shorten the code. It is of course not optional."msgstr ""#. Tag: para#: tutorial.xml:197#, no-c-formatmsgid "Between the two <literal>hibernate-mapping</literal> tags, include a <literal>class</literal> element. All persistent entity classes (again, there might be dependent classes later on, which are not first-class entities) need such a mapping, to a table in the SQL database:"msgstr ""#. Tag: programlisting#: tutorial.xml:204#, no-c-formatmsgid "" "<![CDATA[<hibernate-mapping>\n" "\n" " <class name=\"events.Event\" table=\"EVENTS\">\n" "\n" " </class>\n" "\n" "</hibernate-mapping>]]>"msgstr ""#. Tag: para#: tutorial.xml:206#, no-c-formatmsgid "So far we told Hibernate how to persist and load object of class <literal>Event</literal> to the table <literal>EVENTS</literal>, each instance represented by a row in that table. Now we continue with a mapping of the unique identifier property to the tables primary key. In addition, as we don't want to care about handling this identifier, we configure Hibernate's identifier generation strategy for a surrogate primary key column:"msgstr ""#. Tag: programlisting#: tutorial.xml:214#, no-c-formatmsgid "" "<![CDATA[<hibernate-mapping>\n" "\n" " <class name=\"events.Event\" table=\"EVENTS\">\n" " <id name=\"id\" column=\"EVENT_ID\">\n" " <generator class=\"native\"/>\n" " </id>\n" " </class>\n" "\n" "</hibernate-mapping>]]>"msgstr ""#. Tag: para#: tutorial.xml:216#, no-c-formatmsgid "The <literal>id</literal> element is the declaration of the identifier property, <literal>name=\"id\"</literal> declares the name of the Java property - Hibernate will use the getter and setter methods to access the property. The column attribute tells Hibernate which column of the <literal>EVENTS</literal> table we use for this primary key. The nested <literal>generator</literal> element specifies the identifier generation strategy, in this case we used <literal>native</literal>, which picks the best strategy depending on the configured database (dialect). Hibernate supports database generated, globally unique, as well as application assigned identifiers (or any strategy you have written an extension for)."msgstr ""#. Tag: para
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -