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

📄 tutorial.pot

📁 hibernate-distribution-3.3.1.GA-dist.zip源码
💻 POT
📖 第 1 页 / 共 5 页
字号:
msgstr ""#. Tag: para#: tutorial.xml:419#, no-c-formatmsgid "Place <literal>HibernateUtil.java</literal> in the development source directory, in a package next to <literal>events</literal>:"msgstr ""#. Tag: programlisting#: tutorial.xml:424#, no-c-formatmsgid ""      "<![CDATA[.\n"      "+lib\n"      "  <Hibernate and third-party libraries>\n"      "+src\n"      "  +events\n"      "    Event.java\n"      "    Event.hbm.xml\n"      "  +util\n"      "    HibernateUtil.java\n"      "  hibernate.cfg.xml\n"      "+data\n"      "build.xml]]>"msgstr ""#. Tag: para#: tutorial.xml:426#, no-c-formatmsgid "This should again compile without problems. We finally need to configure a logging system - Hibernate uses commons logging and leaves you the choice between Log4j and JDK 1.4 logging. Most developers prefer Log4j: copy <literal>log4j.properties</literal> from the Hibernate distribution (it's in the <literal>etc/</literal> directory) to your <literal>src</literal> directory, next to <literal>hibernate.cfg.xml</literal>. Have a look at the example configuration and change the settings if you like to have more verbose output. By default, only Hibernate startup message are shown on stdout."msgstr ""#. Tag: para#: tutorial.xml:436#, no-c-formatmsgid "The tutorial infrastructure is complete - and we are ready to do some real work with Hibernate."msgstr ""#. Tag: title#: tutorial.xml:444#, no-c-formatmsgid "Loading and storing objects"msgstr ""#. Tag: para#: tutorial.xml:446#, no-c-formatmsgid "Finally, we can use Hibernate to load and store objects. We write an <literal>EventManager</literal> class with a <literal>main()</literal> method:"msgstr ""#. Tag: programlisting#: tutorial.xml:451#, no-c-formatmsgid ""      "<![CDATA[package events;\n"      "import org.hibernate.Session;\n"      "\n"      "import java.util.Date;\n"      "\n"      "import util.HibernateUtil;\n"      "\n"      "public class EventManager {\n"      "\n"      "    public static void main(String[] args) {\n"      "        EventManager mgr = new EventManager();\n"      "\n"      "        if (args[0].equals(\"store\")) {\n"      "            mgr.createAndStoreEvent(\"My Event\", new Date());\n"      "        }\n"      "\n"      "        HibernateUtil.getSessionFactory().close();\n"      "    }\n"      "\n"      "    private void createAndStoreEvent(String title, Date theDate) {\n"      "\n"      "        Session session = HibernateUtil.getSessionFactory().getCurrentSession();\n"      "\n"      "        session.beginTransaction();\n"      "\n"      "        Event theEvent = new Event();\n"      "        theEvent.setTitle(title);\n"      "        theEvent.setDate(theDate);\n"      "\n"      "        session.save(theEvent);\n"      "\n"      "        session.getTransaction().commit();\n"      "    }\n"      "\n"      "}]]>"msgstr ""#. Tag: para#: tutorial.xml:453#, no-c-formatmsgid "We create a new <literal>Event</literal> object, and hand it over to Hibernate. Hibernate now takes care of the SQL and executes <literal>INSERT</literal>s on the database. Let's have a look at the <literal>Session</literal> and <literal>Transaction</literal>-handling code before we run this."msgstr ""#. Tag: para#: tutorial.xml:460#, no-c-formatmsgid "A <literal>Session</literal> is a single unit of work. For now we'll keep things simple and assume a one-to-one granularity between a Hibernate <literal>Session</literal> and a database transaction. To shield our code from the actual underlying transaction system (in this case plain JDBC, but it could also run with JTA) we use the <literal>Transaction</literal> API that is available on the Hibernate <literal>Session</literal>."msgstr ""#. Tag: para#: tutorial.xml:468#, no-c-formatmsgid "What does <literal>sessionFactory.getCurrentSession()</literal> do? First, you can call it as many times and anywhere you like, once you get hold of your <literal>SessionFactory</literal> (easy thanks to <literal>HibernateUtil</literal>). The <literal>getCurrentSession()</literal> method always returns the \"current\" unit of work. Remember that we switched the configuration option for this mechanism to \"thread\" in <literal>hibernate.cfg.xml</literal>? Hence, the current unit of work is bound to the current Java thread that executes our application. However, this is not the full picture, you also have to consider scope, when a unit of work begins and when it ends."msgstr ""#. Tag: para#: tutorial.xml:479#, no-c-formatmsgid "A <literal>Session</literal> begins when it is first needed, when the first call to <literal>getCurrentSession()</literal> is made. It is then bound by Hibernate to the current thread. When the transaction ends, either through commit or rollback, Hibernate automatically unbinds the <literal>Session</literal> from the thread and closes it for you. If you call <literal>getCurrentSession()</literal> again, you get a new <literal>Session</literal> and can start a new unit of work. This <emphasis>thread-bound</emphasis> programming model is the most popular way of using Hibernate, as it allows flexible layering of your code (transaction demarcation code can be separated from data access code, we'll do this later in this tutorial)."msgstr ""#. Tag: para#: tutorial.xml:490#, no-c-formatmsgid "Related to the unit of work scope, should the Hibernate <literal>Session</literal> be used to execute one or several database operations? The above example uses one <literal>Session</literal> for one operation. This is pure coincidence, the example is just not complex enough to show any other approach. The scope of a Hibernate <literal>Session</literal> is flexible but you should never design your application to use a new Hibernate <literal>Session</literal> for <emphasis>every</emphasis> database operation. So even if you see it a few more times in the following (very trivial) examples, consider <emphasis>session-per-operation</emphasis> an anti-pattern. A real (web) application is shown later in this tutorial."msgstr ""#. Tag: para#: tutorial.xml:501#, no-c-formatmsgid "Have a look at <xref linkend=\"transactions\"/> for more information about transaction handling and demarcation. We also skipped any error handling and rollback in the previous example."msgstr ""#. Tag: para#: tutorial.xml:507#, no-c-formatmsgid "To run this first routine we have to add a callable target to the Ant build file:"msgstr ""#. Tag: programlisting#: tutorial.xml:511#, no-c-formatmsgid ""      "<![CDATA[<target name=\"run\" depends=\"compile\">\n"      "    <java fork=\"true\" classname=\"events.EventManager\" classpathref=\"libraries\">\n"      "        <classpath path=\"${targetdir}\"/>\n"      "        <arg value=\"${action}\"/>\n"      "    </java>\n"      "</target>]]>"msgstr ""#. Tag: para#: tutorial.xml:513#, no-c-formatmsgid "The value of the <literal>action</literal> argument is set on the command line when calling the target:"msgstr ""#. Tag: programlisting#: tutorial.xml:518#, no-c-formatmsgid "<![CDATA[C:\\hibernateTutorial\\>ant run -Daction=store]]>"msgstr ""#. Tag: para#: tutorial.xml:520#, no-c-formatmsgid "You should see, after compilation, Hibernate starting up and, depending on your configuration, lots of log output. At the end you will find the following line:"msgstr ""#. Tag: programlisting#: tutorial.xml:525#, no-c-formatmsgid "<![CDATA[[java] Hibernate: insert into EVENTS (EVENT_DATE, title, EVENT_ID) values (?, ?, ?)]]>"msgstr ""#. Tag: para#: tutorial.xml:527#, no-c-formatmsgid "This is the <literal>INSERT</literal> executed by Hibernate, the question marks represent JDBC bind parameters. To see the values bound as arguments, or to reduce the verbosity of the log, check your <literal>log4j.properties</literal>."msgstr ""#. Tag: para#: tutorial.xml:533#, no-c-formatmsgid "Now we'd like to list stored events as well, so we add an option to the main method:"msgstr ""#. Tag: programlisting#: tutorial.xml:537#, no-c-formatmsgid ""      "<![CDATA[if (args[0].equals(\"store\")) {\n"      "    mgr.createAndStoreEvent(\"My Event\", new Date());\n"      "}\n"      "else if (args[0].equals(\"list\")) {\n"      "    List events = mgr.listEvents();\n"      "    for (int i = 0; i < events.size(); i++) {\n"      "        Event theEvent = (Event) events.get(i);\n"      "        System.out.println(\"Event: \" + theEvent.getTitle() +\n"      "                           \" Time: \" + theEvent.getDate());\n"      "    }\n"      "}]]>"msgstr ""#. Tag: para#: tutorial.xml:539#, no-c-formatmsgid "We also add a new <literal>listEvents() method</literal>:"msgstr ""#. Tag: programlisting#: tutorial.xml:543#, no-c-formatmsgid ""      "<![CDATA[private List listEvents() {\n"      "\n"      "    Session session = HibernateUtil.getSessionFactory().getCurrentSession();\n"      "\n"      "    session.beginTransaction();\n"      "\n"      "    List result = session.createQuery(\"from Event\").list();\n"      "\n"      "    session.getTransaction().commit();\n"      "\n"      "    return result;\n"      "}]]>"msgstr ""#. Tag: para#: tutorial.xml:545#, no-c-formatmsgid "What we do here is use an HQL (Hibernate Query Language) query to load all existing <literal>Event</literal> objects from the database. Hibernate will generate the appropriate SQL, send it to the database and populate <literal>Event</literal> objects with the data. You can create more complex queries with HQL, of course."msgstr ""#. Tag: para#: tutorial.xml:552#, no-c-formatmsgid "Now, to execute and test all of this, follow these steps:"msgstr ""#. Tag: para#: tutorial.xml:558#, no-c-formatmsgid "Run <literal>ant run -Daction=store</literal> to store something into the database and, of course, to generate the database schema before through hbm2ddl."msgstr ""#. Tag: para#: tutorial.xml:564#, no-c-formatmsgid "Now disable hbm2ddl by commenting out the property in your <literal>hibernate.cfg.xml</literal> file. Usually you only leave it turned on in continuous unit testing, but another run of hbm2ddl would <emphasis>drop</emphasis> everything you have stored - the <literal>create</literal> configuration setting actually translates into \"drop all tables from the schema, then re-create all tables, when the SessionFactory is build\"."msgstr ""#. Tag: para#: tutorial.xml:574#, no-c-formatmsgid "If you now call Ant with <literal>-Daction=list</literal>, you should see the events you have stored so far. You can of course also call the <literal>store</literal> action a few times more."msgstr ""#. Tag: para#: tutorial.xml:580#, no-c-formatmsgid "Note: Most new Hibernate users fail at this point and we see questions about <emphasis>Table not found</emphasis> error messages regularly. However, if you follow the steps outlined above you will not have this problem, as hbm2ddl creates the database schema on the first run, and subsequent application restarts will use this schema. If you change the mapping and/or database schema, you have to re-enable hbm2ddl once again."msgstr ""#. Tag: title#: tutorial.xml:593#, no-c-formatmsgid "Part 2 - Mapping associations"msgstr ""#. Tag: para#: tutorial.xml:595#, no-c-formatmsgid "We mapped a persistent entity class to a table. Let's build on this and add some class associations. First we'll add people to our application, and store a list of events they participate in."msgstr ""#. Tag: title#: tutorial.xml:601#, no-c-formatmsgid "Mapping the Person class"msgstr ""#. Tag: para#: tutorial.xml:603#, no-c-formatmsgid "The first cut of the <literal>Person</literal> class is simple:"msgstr ""#. Tag: programlisting#: tutorial.xml:607#, no-c-formatmsgid ""      "<![CDATA[package events;\n"      "\n"      "public class Person {\n"      "\n"      "    private Long id;\n"      "    private int age;\n"      "    private String firstname;\n"      "    private String lastname;\n"      "\n"      "    public Person() {}\n"

⌨️ 快捷键说明

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