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

📄 tutorial.po

📁 hibernate 开源框架的代码 jar包希望大家能喜欢
💻 PO
📖 第 1 页 / 共 5 页
字号:
"为了保存Hibernate的配置,我们可以使用一个简单的<literal>hibernate.""properties</literal>文件,或者一个稍微复杂的<literal>hibernate.cfg.xml</""literal>,甚至可以完全使用程序来配置Hibernate。多数用户更喜欢使用XML配置文""件:"#. Tag: programlisting#: tutorial.xml:273#, 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:275#, 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 """注意这个XML配置使用了一个不同的DTD。在这里,我们配置了Hibernate的""<literal>SessionFactory</literal>-一个关联于特定数据库全局的工厂""(factory)。如果你要使用多个数据库,就要用多个的<literal>&lt;session-""factory&gt;</literal>,通常把它们放在多个配置文件中(为了更容易启动)。"#. Tag: para#: tutorial.xml:283#, 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 """最开始的4个<literal>property</literal>元素包含必要的JDBC连接信息。方言""(dialect)的<literal>property</literal>元素指明Hibernate 生成的特定SQL变量。""你很快会看到,Hibernate对持久化上下文的自动session管理就会派上用场。 打开""<literal>hbm2ddl.auto</literal>选项将自动生成数据库模式(schema)- 直接加入""数据库中。当然这个选项也可以被关闭(通过去除这个配置选项)或者通过Ant任务""<literal>SchemaExport</literal>的帮助来把数据库schema重定向到文件中。最后,在""配置中为持久化类加入映射文件。"#. Tag: para#: tutorial.xml:296#, 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 """把这个文件拷贝到源代码目录下面,这样它就位于classpath的根目录的最后。""Hibernate在启动时会自动在classpath的根目录查找名为<literal>hibernate.cfg.""xml</literal>的配置文件。"#. Tag: title#: tutorial.xml:305#, no-c-formatmsgid "Building with Ant"msgstr "用Ant构建"#. Tag: para#: tutorial.xml:307#, 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 """现在我们用Ant来构建应用程序。你必须先安装Ant-可以从<ulink url=\"http://ant.""apache.org/bindownload.cgi\">Ant 下载页面</ulink>得到它。怎样安装Ant就不在这""里介绍了,请参考<ulink url=\"http://ant.apache.org/manual/index.html\">Ant 用""户手册</ulink>。当你安装完了Ant,就可以开始创建<literal>build.xml</literal>文""件,把它直接放在开发目录下面。"#. Tag: para#: tutorial.xml:316#, no-c-formatmsgid "A basic build file looks like this:"msgstr "一个简单的build文件看起来像这样:"#. Tag: programlisting#: tutorial.xml:320#, 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:322#, 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 """这将告诉Ant把所有在lib目录下以<literal>.jar</literal>结尾的文件拷贝到""classpath中以供编译之用。它也把所有的非Java源代码文件,例如配置和Hibernate映""射文件,拷贝到目标目录。如果你现在运行Ant,会得到以下输出:"#. Tag: programlisting#: tutorial.xml:329#, 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:334#, no-c-formatmsgid "Startup and helpers"msgstr "启动和辅助类"#. Tag: para#: tutorial.xml:336#, 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 """是时候来加载和储存一些<literal>Event</literal>对象了,但首先我们得编写一些基""础的代码以完成设置。我们必须启动Hibernate,此过程包括创建一个全局的""<literal>SessoinFactory</literal>,并把它储存在应用程序代码容易访问的地方。""<literal>SessionFactory</literal>可以创建并打开新的<literal>Session</""literal>。一个<literal>Session</literal>代表一个单线程的单元操作,""<literal>SessionFactory</literal>则是个线程安全的全局对象,只需要被实例化一""次。"#. Tag: para#: tutorial.xml:346#, 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 """我们将创建一个<literal>HibernateUtil</literal>辅助类(helper class)来负责启""动Hibernate和更方便地操作<literal>SessionFactory</literal>。让我们来看一下它""的实现:"#. Tag: programlisting#: tutorial.xml:352#, 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:354#, 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 """这个类不但在它的静态初始化过程(仅当加载这个类的时候被JVM执行一次)中产生全局""的<literal>SessionFactory</literal>,而且隐藏了它使用了静态singleton的事实。""它也可能在应用程序服务器中的JNDI查找<literal>SessionFactory</literal>。"#. Tag: para#: tutorial.xml:361#, 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."msgstr """如果你在配置文件中给<literal>SessionFactory</literal>一个名字,在""<literal>SessionFactory</literal>创建后,Hibernate会试着把它绑定到JNDI。要完""全避免这样的代码,你也可以使用JMX部署,让具有JMX能力的容器来实例化""<literal>HibernateService</literal>并把它绑定到JNDI。这些高级可选项在后面的章""节中会讨论到。"#. Tag: para#: tutorial.xml:370#, no-c-formatmsgid """Place <literal>HibernateUtil.java</literal> in the development source ""directory, in a package next to <literal>events</literal>:"msgstr """把<literal>HibernateUtil.java</literal>放在开发目录的源代码路径下,与放""<literal>events</literal>的包并列:"#. Tag: programlisting#: tutorial.xml:375#, no-c-formatmsgid ""

⌨️ 快捷键说明

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