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

📄 example_parentchild.po

📁 hibernate 开源框架的代码 jar包希望大家能喜欢
💻 PO
📖 第 1 页 / 共 2 页
字号:
#, no-c-formatmsgid """To tighten things up a bit, we could create an <literal>addChild()</literal> ""method of <literal>Parent</literal>."msgstr """为了让事情变得井井有条,可以为<literal>Parent</literal>加一个""<literal>addChild()</literal>方法。"#. Tag: programlisting#: example_parentchild.xml:141#, no-c-formatmsgid """<![CDATA[public void addChild(Child c) {\n""    c.setParent(this);\n""    children.add(c);\n""}]]>"msgstr ""#. Tag: para#: example_parentchild.xml:143#, no-c-formatmsgid "Now, the code to add a <literal>Child</literal> looks like"msgstr "现在,添加<literal>Child</literal>的代码就是这样"#. Tag: programlisting#: example_parentchild.xml:147#, no-c-formatmsgid """<![CDATA[Parent p = (Parent) session.load(Parent.class, pid);\n""Child c = new Child();\n""p.addChild(c);\n""session.save(c);\n""session.flush();]]>"msgstr ""#. Tag: title#: example_parentchild.xml:152#, no-c-formatmsgid "Cascading life cycle"msgstr "级联生命周期(Cascading life cycle)"#. Tag: para#: example_parentchild.xml:154#, no-c-formatmsgid """The explicit call to <literal>save()</literal> is still annoying. We will ""address this by using cascades."msgstr """需要显式调用<literal>save()</literal>仍然很麻烦,我们可以用级联来解决这个问""题。"#. Tag: programlisting#: example_parentchild.xml:159#, no-c-formatmsgid """<![CDATA[<set name=\"children\" inverse=\"true\" cascade=\"all\">\n""    <key column=\"parent_id\"/>\n""    <one-to-many class=\"Child\"/>\n""</set>]]>"msgstr ""#. Tag: para#: example_parentchild.xml:161#, no-c-formatmsgid "This simplifies the code above to"msgstr "这样上面的代码可以简化为:"#. Tag: programlisting#: example_parentchild.xml:165#, no-c-formatmsgid """<![CDATA[Parent p = (Parent) session.load(Parent.class, pid);\n""Child c = new Child();\n""p.addChild(c);\n""session.flush();]]>"msgstr ""#. Tag: para#: example_parentchild.xml:167#, no-c-formatmsgid """Similarly, we don't need to iterate over the children when saving or ""deleting a <literal>Parent</literal>. The following removes <literal>p</""literal> and all its children from the database."msgstr """同样的,保存或删除<literal>Parent</literal>对象的时候并不需要遍历其子对象。 ""下面的代码会删除对象<literal>p</literal>及其所有子对象对应的数据库记录。"#. Tag: programlisting#: example_parentchild.xml:172#, no-c-formatmsgid """<![CDATA[Parent p = (Parent) session.load(Parent.class, pid);\n""session.delete(p);\n""session.flush();]]>"msgstr ""#. Tag: para#: example_parentchild.xml:174#, no-c-formatmsgid "However, this code"msgstr "然而,这段代码"#. Tag: programlisting#: example_parentchild.xml:178#, no-c-formatmsgid """<![CDATA[Parent p = (Parent) session.load(Parent.class, pid);\n""Child c = (Child) p.getChildren().iterator().next();\n""p.getChildren().remove(c);\n""c.setParent(null);\n""session.flush();]]>"msgstr ""#. Tag: para#: example_parentchild.xml:180#, no-c-formatmsgid """will not remove <literal>c</literal> from the database; it will ony remove ""the link to <literal>p</literal> (and cause a <literal>NOT NULL</literal> ""constraint violation, in this case). You need to explicitly <literal>delete()""</literal> the <literal>Child</literal>."msgstr """不会从数据库删除<literal>c</literal>;它只会删除与<literal>p</literal>之间的""连接(并且会导致违反<literal>NOT NULL</literal>约束,在这个例子中)。你需要显""式调用<literal>delete()</literal>来删除<literal>Child</literal>。"#. Tag: programlisting#: example_parentchild.xml:186#, no-c-formatmsgid """<![CDATA[Parent p = (Parent) session.load(Parent.class, pid);\n""Child c = (Child) p.getChildren().iterator().next();\n""p.getChildren().remove(c);\n""session.delete(c);\n""session.flush();]]>"msgstr ""#. Tag: para#: example_parentchild.xml:188#, no-c-formatmsgid """Now, in our case, a <literal>Child</literal> can't really exist without its ""parent. So if we remove a <literal>Child</literal> from the collection, we ""really do want it to be deleted. For this, we must use <literal>cascade=""\"all-delete-orphan\"</literal>."msgstr """在我们的例子中,如果没有父对象,子对象就不应该存在,如果将子对象从collection""中移除,实际上我们是想删除它。要实现这种要求,就必须使用<literal>cascade=""\"all-delete-orphan\"</literal>。"#. Tag: programlisting#: example_parentchild.xml:194#, no-c-formatmsgid """<![CDATA[<set name=\"children\" inverse=\"true\" cascade=\"all-delete-orphan""\">\n""    <key column=\"parent_id\"/>\n""    <one-to-many class=\"Child\"/>\n""</set>]]>"msgstr ""#. Tag: para#: example_parentchild.xml:196#, no-c-formatmsgid """Note: even though the collection mapping specifies <literal>inverse=\"true""\"</literal>, cascades are still processed by iterating the collection ""elements. So if you require that an object be saved, deleted or updated by ""cascade, you must add it to the collection. It is not enough to simply call ""<literal>setParent()</literal>."msgstr """注意:即使在collection一方的映射中指定<literal>inverse=\"true\"</literal>,级""联仍然是通过遍历collection中的元素来处理的。如果你想要通过级联进行子对象的插""入、删除、更新操作,就必须把它加到collection中,只调用<literal>setParent()</""literal>是不够的。"#. Tag: title#: example_parentchild.xml:206#, no-c-formatmsgid "Cascades and <literal>unsaved-value</literal>"msgstr """级联与<literal>未保存值</literal>(Cascades and <literal>unsaved-value</""literal>)"#. Tag: para#: example_parentchild.xml:208#, no-c-formatmsgid """Suppose we loaded up a <literal>Parent</literal> in one <literal>Session</""literal>, made some changes in a UI action and wish to persist these changes ""in a new session by calling <literal>update()</literal>. The ""<literal>Parent</literal> will contain a collection of childen and, since ""cascading update is enabled, Hibernate needs to know which children are ""newly instantiated and which represent existing rows in the database. Lets ""assume that both <literal>Parent</literal> and <literal>Child</literal> have ""genenerated identifier properties of type <literal>Long</literal>. Hibernate ""will use the identifier and version/timestamp property value to determine ""which of the children are new. (See <xref linkend=\"objectstate-saveorupdate""\"/>.) <emphasis>In Hibernate3, it is no longer necessary to specify an ""<literal>unsaved-value</literal> explicitly.</emphasis>"msgstr """假设我们从<literal>Session</literal>中装入了一个<literal>Parent</literal>对""象,用户界面对其进行了修改,然后希望在一个新的Session里面调用<literal>update""()</literal>来保存这些修改。对象<literal>Parent</literal>包含了子对象的集合,""由于打开了级联更新,Hibernate需要知道哪些Child对象是新实例化的,哪些代表数据""库中已经存在的记录。我们假设<literal>Parent</literal>和<literal>Child</""literal>对象的标识属性都是自动生成的,类型为<literal>java.lang.Long</""literal>。Hibernate会使用标识属性的值,和version 或 timestamp 属性,来判断哪""些子对象是新的。(参见<xref linkend=\"objectstate-saveorupdate\"/>.) ""<emphasis>在 Hibernate3 中,显式指定<literal>unsaved-value</literal>不再是必须""的了。</emphasis>"#. Tag: para#: example_parentchild.xml:220#, no-c-formatmsgid """The following code will update <literal>parent</literal> and <literal>child</""literal> and insert <literal>newChild</literal>."msgstr """下面的代码会更新<literal>parent</literal>和<literal>child</literal>对象,并且""插入<literal>newChild</literal>对象。"#. Tag: programlisting#: example_parentchild.xml:225#, no-c-formatmsgid """<![CDATA[//parent and child were both loaded in a previous session\n""parent.addChild(child);\n""Child newChild = new Child();\n""parent.addChild(newChild);\n""session.update(parent);\n""session.flush();]]>"msgstr ""#. Tag: para#: example_parentchild.xml:227#, no-c-formatmsgid """Well, that's all very well for the case of a generated identifier, but what ""about assigned identifiers and composite identifiers? This is more ""difficult, since Hibernate can't use the identifier property to distinguish ""between a newly instantiated object (with an identifier assigned by the ""user) and an object loaded in a previous session. In this case, Hibernate ""will either use the timestamp or version property, or will actually query ""the second-level cache or, worst case, the database, to see if the row ""exists."msgstr """这对于自动生成标识的情况是非常好的,但是自分配的标识和复合标识怎么办呢?这是""有点麻烦,因为Hibernate没有办法区分新实例化的对象(标识被用户指定了)和前一个""Session装入的对象。在这种情况下,Hibernate会使用timestamp或version属性,或者""查询第二级缓存,或者最坏的情况,查询数据库,来确认是否此行存在。"#. Tag: title#: example_parentchild.xml:260#, no-c-formatmsgid "Conclusion"msgstr "结论"#. Tag: para#: example_parentchild.xml:262#, no-c-formatmsgid """There is quite a bit to digest here and it might look confusing first time ""around. However, in practice, it all works out very nicely. Most Hibernate ""applications use the parent / child pattern in many places."msgstr """这里有不少东西需要融会贯通,可能会让新手感到迷惑。但是在实践中它们都工作地非""常好。大部分Hibernate应用程序都会经常用到父子对象模式。"#. Tag: para#: example_parentchild.xml:267#, no-c-formatmsgid """We mentioned an alternative in the first paragraph. None of the above issues ""exist in the case of <literal>&lt;composite-element&gt;</literal> mappings, ""which have exactly the semantics of a parent / child relationship. ""Unfortunately, there are two big limitations to composite element classes: ""composite elements may not own collections, and they should not be the child ""of any entity other than the unique parent."msgstr """在第一段中我们曾经提到另一个方案。上面的这些问题都不会出现在<literal>&lt;""composite-element&gt;</literal>映射中,它准确地表达了父子关系的语义。很不幸复""合元素还有两个重大限制:复合元素不能拥有collections,并且,除了用于惟一的父对""象外,它们不能再作为其它任何实体的子对象。"

⌨️ 快捷键说明

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