📄 example_parentchild.po
字号:
msgid ""msgstr """Project-Id-Version: PACKAGE VERSION\n""Report-Msgid-Bugs-To: http://bugs.kde.org\n""POT-Creation-Date: 2007-10-25 01:01+0000\n""PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n""Last-Translator: FULL NAME <EMAIL@ADDRESS>\n""Language-Team: LANGUAGE <LL@li.org>\n""MIME-Version: 1.0\n""Content-Type: text/plain; charset=UTF-8\n""Content-Transfer-Encoding: 8bit\n"#. Tag: title#: example_parentchild.xml:5#, no-c-formatmsgid "Example: Parent/Child"msgstr "示例:父子关系(Parent Child Relationships)"#. Tag: para#: example_parentchild.xml:7#, no-c-formatmsgid """One of the very first things that new users try to do with Hibernate is to ""model a parent / child type relationship. There are two different approaches ""to this. For various reasons the most convenient approach, especially for ""new users, is to model both <literal>Parent</literal> and <literal>Child</""literal> as entity classes with a <literal><one-to-many></literal> ""association from <literal>Parent</literal> to <literal>Child</literal>. (The ""alternative approach is to declare the <literal>Child</literal> as a ""<literal><composite-element></literal>.) Now, it turns out that ""default semantics of a one to many association (in Hibernate) are much less ""close to the usual semantics of a parent / child relationship than those of ""a composite element mapping. We will explain how to use a ""<emphasis>bidirectional one to many association with cascades</emphasis> to ""model a parent / child relationship efficiently and elegantly. It's not at ""all difficult!"msgstr """刚刚接触Hibernate的人大多是从父子关系(parent / child type relationship)的建""模入手的。父子关系的建模有两种方法。由于种种原因,最方便的方法是把""<literal>Parent</literal>和<literal>Child</literal>都建模成实体类,并创建一个""从<literal>Parent</literal>指向<literal>Child</literal>的<one-to-many>""关联,对新手来说尤其如此。还有一种方法,就是将<literal>Child</literal>声明为""一个<literal><composite-element></literal>(组合元素)。 事实上在""Hibernate中one to many关联的默认语义远没有composite element贴近parent / child""关系的通常语义。下面我们会阐述如何使用<emphasis>带有级联的双向一对多关联""(bidirectional one to many association with cascades)</emphasis>去建立有效、""优美的parent / child关系。这一点也不难!"#. Tag: title#: example_parentchild.xml:21#, no-c-formatmsgid "A note about collections"msgstr "关于collections需要注意的一点"#. Tag: para#: example_parentchild.xml:23#, no-c-formatmsgid """Hibernate collections are considered to be a logical part of their owning ""entity; never of the contained entities. This is a crucial distinction! It ""has the following consequences:"msgstr """Hibernate collections被当作其所属实体而不是其包含实体的一个逻辑部分。这非常重""要!它主要体现为以下几点:"#. Tag: para#: example_parentchild.xml:30#, no-c-formatmsgid """When we remove / add an object from / to a collection, the version number of ""the collection owner is incremented."msgstr "当删除或增加collection中对象的时候,collection所属者的版本值会递增。"#. Tag: para#: example_parentchild.xml:36#, no-c-formatmsgid """If an object that was removed from a collection is an instance of a value ""type (eg, a composite element), that object will cease to be persistent and ""its state will be completely removed from the database. Likewise, adding a ""value type instance to the collection will cause its state to be immediately ""persistent."msgstr """如果一个从collection中移除的对象是一个值类型(value type)的实例,比如""composite element,那么这个对象的持久化状态将会终止,其在数据库中对应的记录会""被删除。同样的,向collection增加一个value type的实例将会使之立即被持久化。"#. Tag: para#: example_parentchild.xml:44#, no-c-formatmsgid """On the other hand, if an entity is removed from a collection (a one-to-many ""or many-to-many association), it will not be deleted, by default. This ""behaviour is completely consistent - a change to the internal state of ""another entity should not cause the associated entity to vanish! Likewise, ""adding an entity to a collection does not cause that entity to become ""persistent, by default."msgstr """另一方面,如果从一对多或多对多关联的collection中移除一个实体,在缺省情况下这""个对象并不会被删除。这个行为是完全合乎逻辑的--改变一个实体的内部状态不应该""使与它关联的实体消失掉!同样的,向collection增加一个实体不会使之被持久化。"#. Tag: para#: example_parentchild.xml:54#, no-c-formatmsgid """Instead, the default behaviour is that adding an entity to a collection ""merely creates a link between the two entities, while removing it removes ""the link. This is very appropriate for all sorts of cases. Where it is not ""appropriate at all is the case of a parent / child relationship, where the ""life of the child is bound to the life cycle of the parent."msgstr """实际上,向Collection增加一个实体的缺省动作只是在两个实体之间创建一个连接而""已,同样移除的时候也只是删除连接。这种处理对于所有的情况都是合适的。对于父子""关系则是完全不适合的,在这种关系下,子对象的生存绑定于父对象的生存周期。"#. Tag: title#: example_parentchild.xml:64#, no-c-formatmsgid "Bidirectional one-to-many"msgstr "双向的一对多关系(Bidirectional one-to-many)"#. Tag: para#: example_parentchild.xml:66#, no-c-formatmsgid """Suppose we start with a simple <literal><one-to-many></literal> ""association from <literal>Parent</literal> to <literal>Child</literal>."msgstr "假设我们要实现一个简单的从Parent到Child的<one-to-many>关联。"#. Tag: programlisting#: example_parentchild.xml:71#, no-c-formatmsgid """<![CDATA[<set name=\"children\">\n"" <key column=\"parent_id\"/>\n"" <one-to-many class=\"Child\"/>\n""</set>]]>"msgstr ""#. Tag: para#: example_parentchild.xml:73#, no-c-formatmsgid "If we were to execute the following code"msgstr "如果我们运行下面的代码"#. Tag: programlisting#: example_parentchild.xml:77#, no-c-formatmsgid """<![CDATA[Parent p = .....;\n""Child c = new Child();\n""p.getChildren().add(c);\n""session.save(c);\n""session.flush();]]>"msgstr ""#. Tag: para#: example_parentchild.xml:79#, no-c-formatmsgid "Hibernate would issue two SQL statements:"msgstr "Hibernate会产生两条SQL语句:"#. Tag: para#: example_parentchild.xml:85#, no-c-formatmsgid """an <literal>INSERT</literal> to create the record for <literal>c</literal>"msgstr "一条<literal>INSERT</literal>语句,为<literal>c</literal>创建一条记录"#. Tag: para#: example_parentchild.xml:88#, no-c-formatmsgid """an <literal>UPDATE</literal> to create the link from <literal>p</literal> to ""<literal>c</literal>"msgstr """一条<literal>UPDATE</literal>语句,创建从<literal>p</literal>到<literal>c</""literal>的连接"#. Tag: para#: example_parentchild.xml:95#, no-c-formatmsgid """This is not only inefficient, but also violates any <literal>NOT NULL</""literal> constraint on the <literal>parent_id</literal> column. We can fix ""the nullability constraint violation by specifying <literal>not-null=\"true""\"</literal> in the collection mapping:"msgstr """这样做不仅效率低,而且违反了列<literal>parent_id</literal>非空的限制。我们可""以通过在集合类映射上指定<literal>not-null=\"true\"</literal>来解决违反非空约""束的问题:"#. Tag: programlisting#: example_parentchild.xml:101#, no-c-formatmsgid """<![CDATA[<set name=\"children\">\n"" <key column=\"parent_id\" not-null=\"true\"/>\n"" <one-to-many class=\"Child\"/>\n""</set>]]>"msgstr ""#. Tag: para#: example_parentchild.xml:103#, no-c-formatmsgid "However, this is not the recommended solution."msgstr "然而,这并非是推荐的解决方法。"#. Tag: para#: example_parentchild.xml:106#, no-c-formatmsgid """The underlying cause of this behaviour is that the link (the foreign key ""<literal>parent_id</literal>) from <literal>p</literal> to <literal>c</""literal> is not considered part of the state of the <literal>Child</literal> ""object and is therefore not created in the <literal>INSERT</literal>. So the ""solution is to make the link part of the <literal>Child</literal> mapping."msgstr """这种现象的根本原因是从<literal>p</literal>到<literal>c</literal>的连接(外键""parent_id)没有被当作<literal>Child</literal>对象状态的一部分,因而没有在""INSERT语句中被创建。因此解决的办法就是把这个连接添加到Child的映射中。"#. Tag: programlisting#: example_parentchild.xml:113#, no-c-formatmsgid """<![CDATA[<many-to-one name=\"parent\" column=\"parent_id\" not-null=\"true\"/"">]]>"msgstr ""#. Tag: para#: example_parentchild.xml:115#, no-c-formatmsgid """(We also need to add the <literal>parent</literal> property to the ""<literal>Child</literal> class.)"msgstr """(我们还需要为类<literal>Child</literal>添加<literal>parent</literal>属性)"#. Tag: para#: example_parentchild.xml:119#, no-c-formatmsgid """Now that the <literal>Child</literal> entity is managing the state of the ""link, we tell the collection not to update the link. We use the ""<literal>inverse</literal> attribute."msgstr """现在实体<literal>Child</literal>在管理连接的状态,为了使collection不更新连""接,我们使用<literal>inverse</literal>属性。"#. Tag: programlisting#: example_parentchild.xml:124#, no-c-formatmsgid """<![CDATA[<set name=\"children\" inverse=\"true\">\n"" <key column=\"parent_id\"/>\n"" <one-to-many class=\"Child\"/>\n""</set>]]>"msgstr ""#. Tag: para#: example_parentchild.xml:126#, no-c-formatmsgid "The following code would be used to add a new <literal>Child</literal>"msgstr "下面的代码是用来添加一个新的<literal>Child</literal>"#. Tag: programlisting#: example_parentchild.xml:130#, no-c-formatmsgid """<![CDATA[Parent p = (Parent) session.load(Parent.class, pid);\n""Child c = new Child();\n""c.setParent(p);\n""p.getChildren().add(c);\n""session.save(c);\n""session.flush();]]>"msgstr ""#. Tag: para#: example_parentchild.xml:132#, no-c-formatmsgid "And now, only one SQL <literal>INSERT</literal> would be issued!"msgstr "现在,只会有一条<literal>INSERT</literal>语句被执行!"#. Tag: para#: example_parentchild.xml:136
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -