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

📄 tutorial.po

📁 hibernate 开源框架的代码 jar包希望大家能喜欢
💻 PO
📖 第 1 页 / 共 5 页
字号:
#. Tag: programlisting#: tutorial.xml:494#, 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:496#, 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 """我们在这里是用一个HQL(Hibernate Query Language-Hibernate查询语言)查询语句""来从数据库中加载所有存在的<literal>Event</literal>对象。Hibernate会生成适当的""SQL,把它发送到数据库,并操作从查询得到数据的<literal>Event</literal>对象。当""然,你可以使用HQL来创建更加复杂的查询。"#. Tag: para#: tutorial.xml:503#, no-c-formatmsgid "Now, to execute and test all of this, follow these steps:"msgstr "现在,根据以下步骤来执行并测试以上各项:"#. Tag: para#: tutorial.xml:509#, 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 """运行<literal>ant run -Daction=store</literal>来保存一些内容到数据库。当然,先""得用hbm2ddl来生成数据库schema。"#. Tag: para#: tutorial.xml:515#, 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 continous 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 """现在把<literal>hibernate.cfg.xml</literal>文件中hbm2ddl属性注释掉,这样我们就""取消了在启动时用hbm2ddl来生成数据库schema。通常只有在不断重复进行单元测试的时""候才需要打开它,但再次运行hbm2ddl会把你保存的一切都删掉(<emphasis>drop</""emphasis>)——<literal>create</literal>配置的真实含义是:“在创建SessionFactory""的时候,从schema 中drop 掉所有的表,再重新创建它们”。"#. Tag: para#: tutorial.xml:525#, 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 """如果你现在使用命令行参数<literal>-Daction=list</literal>运行Ant,你会看到那些""至今为止我们所储存的events。当然,你也可以多调用几次<literal>store</literal>""以保存更多的envents。"#. Tag: para#: tutorial.xml:531#, 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 """注意,很多Hibernate新手在这一步会失败,我们不时看到关于<emphasis>Table not ""found</emphasis>错误信息的提问。但是,只要你根据上面描述的步骤来执行,就不会""有这个问题,因为hbm2ddl会在第一次运行的时候创建数据库schema,后继的应用程序重""起后还能继续使用这个schema。假若你修改了映射,或者修改了数据库schema,你必须""把hbm2ddl重新打开一次。"#. Tag: title#: tutorial.xml:544#, no-c-formatmsgid "Part 2 - Mapping associations"msgstr "第二部分 - 关联映射"#. Tag: para#: tutorial.xml:546#, 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 """我们已经映射了一个持久化实体类到表上。让我们在这个基础上增加一些类之间的关""联。首先我们往应用程序里增加人(people)的概念,并存储他们所参与的一个Event列""表。(译者注:与Event一样,我们在后面将直接使用person来表示“人”而不是它的中文""翻译)"#. Tag: title#: tutorial.xml:552#, no-c-formatmsgid "Mapping the Person class"msgstr "映射Person类"#. Tag: para#: tutorial.xml:554#, no-c-formatmsgid "The first cut of the <literal>Person</literal> class is simple:"msgstr "最初简单的<literal>Person</literal>类:"#. Tag: programlisting#: tutorial.xml:558#, 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""\n""    // Accessor methods for all properties, private setter for 'id'\n""\n""}]]>"msgstr ""#. Tag: para#: tutorial.xml:560#, no-c-formatmsgid """Create a new mapping file called <literal>Person.hbm.xml</literal> (don't ""forget the DTD reference at the top):"msgstr """创建一个名为<literal>Person.hbm.xml</literal>的新映射文件(别忘了最上面的DTD""引用):"#. Tag: programlisting#: tutorial.xml:565#, no-c-formatmsgid """<![CDATA[<hibernate-mapping>\n""\n""    <class name=\"events.Person\" table=\"PERSON\">\n""        <id name=\"id\" column=\"PERSON_ID\">\n""            <generator class=\"native\"/>\n""        </id>\n""        <property name=\"age\"/>\n""        <property name=\"firstname\"/>\n""        <property name=\"lastname\"/>\n""    </class>\n""\n""</hibernate-mapping>]]>"msgstr ""#. Tag: para#: tutorial.xml:567#, no-c-formatmsgid "Finally, add the new mapping to Hibernate's configuration:"msgstr "最后,把新的映射加入到Hibernate的配置中:"#. Tag: programlisting#: tutorial.xml:571#, no-c-formatmsgid """<![CDATA[<mapping resource=\"events/Event.hbm.xml\"/>\n""<mapping resource=\"events/Person.hbm.xml\"/>]]>"msgstr ""#. Tag: para#: tutorial.xml:573#, no-c-formatmsgid """We'll now create an association between these two entities. Obviously, ""persons can participate in events, and events have participants. The design ""questions we have to deal with are: directionality, multiplicity, and ""collection behavior."msgstr """现在我们在这两个实体之间创建一个关联。显然,persons可以参与一系列events,而""events也有不同的参加者(persons)。我们需要处理的设计问题是关联方向""(directionality),阶数(multiplicity)和集合(collection)的行为。"#. Tag: title#: tutorial.xml:583#, no-c-formatmsgid "A unidirectional Set-based association"msgstr "单向Set-based的关联"#. Tag: para#: tutorial.xml:585#, no-c-formatmsgid """We'll add a collection of events to the <literal>Person</literal> class. ""That way we can easily navigate to the events for a particular person, ""without executing an explicit query - by calling <literal>aPerson.getEvents()""</literal>. We use a Java collection, a <literal>Set</literal>, because the ""collection will not contain duplicate elements and the ordering is not ""relevant for us."msgstr """我们将向<literal>Person</literal>类增加一连串的events。那样,通过调用""<literal>aPerson.getEvents()</literal>,就可以轻松地导航到特定person所参与的""events,而不用去执行一个显式的查询。我们使用Java的集合类(collection):""<literal>Set</literal>,因为set 不包含重复的元素及与我们无关的排序。"#. Tag: para#: tutorial.xml:592#, no-c-formatmsgid """We need a unidirectional, many-valued associations, implemented with a ""<literal>Set</literal>. Let's write the code for this in the Java classes ""and then map it:"msgstr """我们需要用set 实现一个单向多值关联。让我们在Java类里为这个关联编码,接着映射""它:"#. Tag: programlisting#: tutorial.xml:597#, no-c-formatmsgid """<![CDATA[public class Person {\n""\n""    private Set events = new HashSet();\n""\n""    public Set getEvents() {\n""        return events;\n""    }\n""\n""    public void setEvents(Set events) {\n""        this.events = events;\n""    }\n""}]]>"msgstr ""#. Tag: para#: tutorial.xml:599#, no-c-formatmsgid """Before we map this association, think about the other side. Clearly, we ""could just keep this unidirectional. Or, we could create another collection ""on the <literal>Event</literal>, if we want to be able to navigate it bi-""directional, i.e. <literal>anEvent.getParticipants()</literal>. This is not ""necessary, from a functional perspective. You could always execute an ""explicit query to retrieve the participants for a particular event. This is ""a design choice left to you, but what is clear from this discussion is the ""multiplicity of the association: \"many\" valued on both sides, we call this ""a <emphasis>many-to-many</emphasis> association. Hence, we use Hibernate's ""many-to-many mapping:"msgstr """在映射这个关联之前,先考虑一下此关联的另外一端。很显然,我们可以保持这个关联""是单向的。或者,我们可以在<literal>Event</literal>里创建另外一个集合,如果希""望能够双向地导航,如:<literal>anEvent.getParticipants()</literal>。从功能的""角度来说,这并不是必须的。因为你总可以显式地执行一个查询,以获得某个特定event""的所有参与者。这是个在设计时需要做出的选择,完全由你来决定,但此讨论中关于关""联的阶数是清楚的:即两端都是“多”值的,我们把它叫做<emphasis>多对多(many-to-""many)</emphasis>关联。因而,我们使用Hibernate的多对多映射:"#. Tag: programlisting#: tutorial.xml:610#, no-c-formatmsgid """<![CDATA[<class name=\"events.Person\" table=\"PERSON\">\n""    <id name=\"id\" column=\"PERSON_ID\">\n""        <generator class=\"native\"/>\n""    </id>\n""    <property name=\"age\"/>\n""    <property name=\"firstname\"/>\n""    <property name=\"lastname\"/>\n""\n""    <set name=\"events\" table=\"PERSON_EVENT\">\n""        <key column=\"PERSON_ID\"/>\n""        <many-to-many column=\"EVENT_ID\" class=\"events.Event\"/>\n""    </set>\n""\n""</class>]]>"msgstr ""#. Tag: para#: tutorial.xml:612#, no-c-formatmsgid """Hibernate supports all kinds of collection mappings, a <literal>&lt;set&gt;</""literal> being most common. For a many-to-many association (or <emphasis>n:""m</emphasis> entity relationship), an association table is needed. Each row ""in this table represents a link between a person and an event. The table ""name is configured with the <literal>table</literal> attribute of the ""<literal>set</literal> element. The identifier column name in the ""association, for the person's side, is defined with the <literal>&lt;key&gt;""</literal> element, the column name for the event's side with the ""<literal>column</literal> attribute of the <literal>&lt;many-to-many&gt;</""literal>. You also have to tell Hibernate the class of the objects in your ""collection (correct: the class on the other side of the collection of ""references)."msgstr """Hibernate支持各种各样的集合映射,<literal>&lt;set&gt;</literal>使用的最

⌨️ 快捷键说明

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