📄 tutorial.po
字号:
"<![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:377#, 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 """再次编译这个应用程序应该不会有问题。最后我们需要配置一个日志(logging)系统 ""- Hibernate使用通用日志接口,允许你在Log4j和JDK 1.4 日志之间进行选择。多数开""发者更喜欢Log4j:从Hibernate的发布包中(它在<literal>etc/</literal>目录下)拷""贝<literal>log4j.properties</literal>到你的<literal>src</literal>目录,与""<literal>hibernate.cfg.xml</literal>.放在一起。看一下配置示例,如果你希望看到""更加详细的输出信息,你可以修改配置。默认情况下,只有Hibernate的启动信息才会显""示在标准输出上。"#. Tag: para#: tutorial.xml:387#, no-c-formatmsgid """The tutorial infrastructure is complete - and we are ready to do some real ""work with Hibernate."msgstr "示例的基本框架完成了 - 现在我们可以用Hibernate来做些真正的工作。"#. Tag: title#: tutorial.xml:395#, no-c-formatmsgid "Loading and storing objects"msgstr "加载并存储对象"#. Tag: para#: tutorial.xml:397#, 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 """我们终于可以使用Hibernate来加载和存储对象了,编写一个带有<literal>main()</""literal>方法的<literal>EventManager</literal>类:"#. Tag: programlisting#: tutorial.xml:402#, 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:404#, 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 """我们创建了个新的<literal>Event</literal>对象并把它传递给Hibernate。现在""Hibernate负责与SQL打交道,并把<literal>INSERT</literal>命令传给数据库。在运行""之前,让我们看一下处理<literal>Session</literal>和<literal>Transaction</""literal>的代码。"#. Tag: para#: tutorial.xml:411#, 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 """一个<literal>Session</literal>就是个单一的工作单元。我们暂时让事情简单一些,""并假设Hibernate<literal>Session</literal>和数据库事务是一一对应的。为了让我们""的代码从底层的事务系统中脱离出来(此例中是JDBC,但也可能是JTA),我们使用""Hibernate <literal>Session</literal>中的<literal>Transaction</literal> API。"#. Tag: para#: tutorial.xml:419#, 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 """<literal>sessionFactory.getCurrentSession()</literal>是干什么的呢?首先,只要""你持有<literal>SessionFactory</literal>(幸亏我们有<literal>HibernateUtil</""literal>,可以随时获得),大可在任何时候、任何地点调用这个方法。""<literal>getCurrentSession()</literal>方法总会返回“当前的”工作单元。记得我们""在<literal>hibernate.cfg.xml</literal>中把这一配置选项调整为\"thread\"了吗?""因此,因此,当前工作单元被绑定到当前执行我们应用程序的Java线程。但是,这并非""是完全准确的,你还得考虑工作单元的生命周期范围 (scope),它何时开始,又何时结束."#. Tag: para#: tutorial.xml:430#, 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 """<literal>Session</literal>在第一次被使用的时候,即第一次调用""<literal>getCurrentSession()</literal>的时候,其生命周期就开始。然后它被""Hibernate绑定到当前线程。当事务结束的时候,不管是提交还是回滚,Hibernate会自""动把<literal>Session</literal>从当前线程剥离,并且关闭它。假若你再次调用""<literal>getCurrentSession()</literal>,你会得到一个新的<literal>Session</""literal>,并且开始一个新的工作单元。这种<emphasis>线程绑定(thread-bound)</""emphasis>的编程模型(model)是使用Hibernate的最广泛的方式,因为它支持对你的代""码灵活分层(事务划分可以和你的数据访问代码分离开来,在本教程的后面部分就会这么""做)。"#. Tag: para#: tutorial.xml:441#, 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 """和工作单元的生命周期这个话题相关,Hibernate <literal>Session</literal>是否被""应该用来执行多次数据库操作?上面的例子对每一次操作使用了一个""<literal>Session</literal>,这完全是巧合,这个例子不是很复杂,无法展示其他方""式。Hibernate <literal>Session</literal>的生命周期可以很灵活,但是你绝不要把""你的应用程序设计成为<emphasis>每一次</emphasis>数据库操作都用一个新的""Hibernate <literal>Session</literal>。因此就算下面的例子(它们都很简单)中你""可以看到这种用法,记住<emphasis>每次操作一个session</emphasis>是一个反模式。""在本教程的后面会展示一个真正的(web)程序。"#. Tag: para#: tutorial.xml:452#, 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 """关于事务处理及事务边界界定的详细信息,请参看<xref linkend=\"transactions\"/"">。在上面的例子中,我们也忽略了所有的错误与回滚的处理。"#. Tag: para#: tutorial.xml:458#, no-c-formatmsgid """To run this first routine we have to add a callable target to the Ant build ""file:"msgstr """为第一次运行我们的程序,我们得在Ant的build文件中增加一个可以调用得到的""target。"#. Tag: programlisting#: tutorial.xml:462#, 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:464#, no-c-formatmsgid """The value of the <literal>action</literal> argument is set on the command ""line when calling the target:"msgstr """<literal>action</literal>参数(argument)的值是通过命令行调用这个target的时候""设置的:"#. Tag: programlisting#: tutorial.xml:469#, no-c-formatmsgid "<![CDATA[C:\\hibernateTutorial\\>ant run -Daction=store]]>"msgstr ""#. Tag: para#: tutorial.xml:471#, 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 """你应该会看到,编译以后,Hibernate根据你的配置启动,并产生一大堆的输出日志。在""日志最后你会看到下面这行:"#. Tag: programlisting#: tutorial.xml:476#, no-c-formatmsgid """<![CDATA[[java] Hibernate: insert into EVENTS (EVENT_DATE, title, EVENT_ID) ""values (?, ?, ?)]]>"msgstr ""#. Tag: para#: tutorial.xml:478#, 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 """这是Hibernate执行的<literal>INSERT</literal>命令,问号代表JDBC的绑定参数。如""果想要看到绑定参数的值或者减少日志的长度,就要调整你在<literal>log4j.""properties</literal>文件里的设置。"#. Tag: para#: tutorial.xml:484#, no-c-formatmsgid """Now we'd like to list stored events as well, so we add an option to the main ""method:"msgstr """我们想要列出所有已经被存储的events,就要增加一个条件分支选项到main方法中去。"#. Tag: programlisting#: tutorial.xml:488#, 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:490#, no-c-formatmsgid "We also add a new <literal>listEvents() method</literal>:"msgstr "我们也增加一个新的<literal>listEvents()</literal>方法:"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -