📄 example_parentchild.po
字号:
#, no-c-formatmsgid "And now, only one SQL <literal>INSERT</literal> would be issued!"msgstr "And now, only one SQL <literal>INSERT</literal> would be issued!"#. Tag: para#: example_parentchild.xml:136#, no-c-formatmsgid """To tighten things up a bit, we could create an <literal>addChild()</literal> ""method of <literal>Parent</literal>."msgstr """To tighten things up a bit, we could create an <literal>addChild()</literal> ""method of <literal>Parent</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 "Now, the code to add a <literal>Child</literal> looks like"#. 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 """The explicit call to <literal>save()</literal> is still annoying. We will ""address this by using cascades."#. 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 "This simplifies the code above to"#. 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 """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."#. 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 "However, this code"#. 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 """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>."#. 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 """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>."#. 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 """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>."#. Tag: title#: example_parentchild.xml:206#, no-c-formatmsgid "Cascades and <literal>unsaved-value</literal>"msgstr "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 """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>"#. 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 """The following code will update <literal>parent</literal> and <literal>child</""literal> and insert <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 """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."#. Tag: title#: example_parentchild.xml:260#, no-c-formatmsgid "Conclusion"msgstr "Conclusion"#. 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 """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."#. 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><composite-element></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 """We mentioned an alternative in the first paragraph. None of the above issues ""exist in the case of <literal><composite-element></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."
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -