📄 session_api.po
字号:
"请注意如果没有匹配的数据库记录,<literal>load()</literal>方法可能抛出无法恢复""的异常(unrecoverable exception)。 如果类的映射使用了代理(proxy),""<literal>load()</literal>方法会返回一个未初始化的代理,直到你调用该代理的某方""法时才会去访问数据库。 若你希望在某对象中创建一个指向另一个对象的关联,又不想""在从数据库中装载该对象时同时装载相关联的那个对象,那么这种操作方式就用得上的""了。 如果为相应类映射关系设置了<literal>batch-size</literal>, 那么使用这种操""作方式允许多个对象被一批装载(因为返回的是代理,无需从数据库中抓取所有对象的""数据)。"#. Tag: para#: session_api.xml:203#, no-c-formatmsgid """If you are not certain that a matching row exists, you should use the ""<literal>get()</literal> method, which hits the database immediately and ""returns null if there is no matching row."msgstr """如果你不确定是否有匹配的行存在,应该使用<literal>get()</literal>方法,它会立""刻访问数据库,如果没有对应的记录,会返回null。"#. Tag: programlisting#: session_api.xml:209#, no-c-formatmsgid """<![CDATA[Cat cat = (Cat) sess.get(Cat.class, id);\n""if (cat==null) {\n"" cat = new Cat();\n"" sess.save(cat, id);\n""}\n""return cat;]]>"msgstr ""#. Tag: para#: session_api.xml:211#, no-c-formatmsgid """You may even load an object using an SQL <literal>SELECT ... FOR UPDATE</""literal>, using a <literal>LockMode</literal>. See the API documentation for ""more information."msgstr """你甚至可以选用某个<literal>LockMode</literal>,用SQL的<literal>SELECT ... ""FOR UPDATE</literal>装载对象。 请查阅API文档以获取更多信息。"#. Tag: programlisting#: session_api.xml:216#, no-c-formatmsgid "<![CDATA[Cat cat = (Cat) sess.get(Cat.class, id, LockMode.UPGRADE);]]>"msgstr ""#. Tag: para#: session_api.xml:218#, no-c-formatmsgid """Note that any associated instances or contained collections are ""<emphasis>not</emphasis> selected <literal>FOR UPDATE</literal>, unless you ""decide to specify <literal>lock</literal> or <literal>all</literal> as a ""cascade style for the association."msgstr """注意,任何关联的对象或者包含的集合都<emphasis>不会</emphasis>被以""<literal>FOR UPDATE</literal>方式返回, 除非你指定了<literal>lock</literal>或""者<literal>all</literal>作为关联(association)的级联风格(cascade style)。"#. Tag: para#: session_api.xml:225#, no-c-formatmsgid """It is possible to re-load an object and all its collections at any time, ""using the <literal>refresh()</literal> method. This is useful when database ""triggers are used to initialize some of the properties of the object."msgstr """任何时候都可以使用<literal>refresh()</literal>方法强迫装载对象和它的集合。如""果你使用数据库触发器功能来处理对象的某些属性,这个方法就很有用了。"#. Tag: programlisting#: session_api.xml:231#, no-c-formatmsgid """<![CDATA[sess.save(cat);\n""sess.flush(); //force the SQL INSERT\n""sess.refresh(cat); //re-read the state (after the trigger executes)]]>"msgstr ""#. Tag: para#: session_api.xml:233#, no-c-formatmsgid """An important question usually appears at this point: How much does Hibernate ""load from the database and how many SQL <literal>SELECT</literal>s will it ""use? This depends on the <emphasis>fetching strategy</emphasis> and is ""explained in <xref linkend=\"performance-fetching\"/>."msgstr """此处通常会出现一个重要问题: Hibernate会从数据库中装载多少东西?会执行多少条相""应的SQL<literal>SELECT</literal>语句? 这取决于<emphasis>抓取策略(fetching ""strategy)</emphasis>,会在<xref linkend=\"performance-fetching\"/>中解释。"#. Tag: title#: session_api.xml:243#, no-c-formatmsgid "Querying"msgstr "查询"#. Tag: para#: session_api.xml:245#, no-c-formatmsgid """If you don't know the identifiers of the objects you are looking for, you ""need a query. Hibernate supports an easy-to-use but powerful object oriented ""query language (HQL). For programmatic query creation, Hibernate supports a ""sophisticated Criteria and Example query feature (QBC and QBE). You may also ""express your query in the native SQL of your database, with optional support ""from Hibernate for result set conversion into objects."msgstr """如果不知道所要寻找的对象的持久化标识,那么你需要使用查询。Hibernate支持强大且""易于使用的面向对象查询语言(HQL)。 如果希望通过编程的方式创建查询,Hibernate提""供了完善的按条件(Query By Criteria, QBC)以及按样例(Query By Example, QBE)进行""查询的功能。 你也可以用原生SQL(native SQL)描述查询,Hibernate额外提供了将结果""集(result set)转化为对象的支持。"#. Tag: title#: session_api.xml:255#, no-c-formatmsgid "Executing queries"msgstr "执行查询"#. Tag: para#: session_api.xml:257#, no-c-formatmsgid """HQL and native SQL queries are represented with an instance of <literal>org.""hibernate.Query</literal>. This interface offers methods for parameter ""binding, result set handling, and for the execution of the actual query. You ""always obtain a <literal>Query</literal> using the current <literal>Session</""literal>:"msgstr """HQL和原生SQL(native SQL)查询要通过为<literal>org.hibernate.Query</literal>的""实例来表达。 这个接口提供了参数绑定、结果集处理以及运行实际查询的方法。 你总""是可以通过当前<literal>Session</literal>获取一个<literal>Query</literal>对""象:"#. Tag: programlisting#: session_api.xml:264#, no-c-formatmsgid """<![CDATA[List cats = session.createQuery(\n"" \"from Cat as cat where cat.birthdate < ?\")\n"" .setDate(0, date)\n"" .list();\n""\n""List mothers = session.createQuery(\n"" \"select mother from Cat as cat join cat.mother as mother where cat.name ""= ?\")\n"" .setString(0, name)\n"" .list();\n""\n""List kittens = session.createQuery(\n"" \"from Cat as cat where cat.mother = ?\")\n"" .setEntity(0, pk)\n"" .list();\n""\n""Cat mother = (Cat) session.createQuery(\n"" \"select cat.mother from Cat as cat where cat = ?\")\n"" .setEntity(0, izi)\n"" .uniqueResult();]]\n""\n""Query mothersWithKittens = (Cat) session.createQuery(\n"" \"select mother from Cat as mother left join fetch mother.kittens\");\n""Set uniqueMothers = new HashSet(mothersWithKittens.list());]]>"msgstr ""#. Tag: para#: session_api.xml:266#, no-c-formatmsgid """A query is usually executed by invoking <literal>list()</literal>, the ""result of the query will be loaded completely into a collection in memory. ""Entity instances retrieved by a query are in persistent state. The ""<literal>uniqueResult()</literal> method offers a shortcut if you know your ""query will only return a single object. Note that queries that make use of ""eager fetching of collections usually return duplicates of the root objects ""(but with their collections initialized). You can filter these duplicates ""simply through a <literal>Set</literal>."msgstr """一个查询通常在调用<literal>list()</literal>时被执行,执行结果会完全装载进内存""中的一个集合(collection)。 查询返回的对象处于持久(persistent)状态。如果你知道""的查询只会返回一个对象,可使用<literal>list()</literal>的快捷方式""<literal>uniqueResult()</literal>。 注意,使用集合预先抓取的查询往往会返回多""次根对象(他们的集合类都被初始化了)。你可以通过一个集合来过滤这些重复对象。"#. Tag: title#: session_api.xml:278#, no-c-formatmsgid "Iterating results"msgstr "迭代式获取结果(Iterating results)"#. Tag: para#: session_api.xml:280#, no-c-formatmsgid """Occasionally, you might be able to achieve better performance by executing ""the query using the <literal>iterate()</literal> method. This will only ""usually be the case if you expect that the actual entity instances returned ""by the query will already be in the session or second-level cache. If they ""are not already cached, <literal>iterate()</literal> will be slower than ""<literal>list()</literal> and might require many database hits for a simple ""query, usually <emphasis>1</emphasis> for the initial select which only ""returns identifiers, and <emphasis>n</emphasis> additional selects to ""initialize the actual instances."msgstr """某些情况下,你可以使用<literal>iterate()</literal>方法得到更好的性能。 这通常""是你预期返回的结果在session,或二级缓存(second-level cache)中已经存在时的情""况。 如若不然,<literal>iterate()</literal>会比<literal>list()</literal>慢,""而且可能简单查询也需要进行多次数据库访问: <literal>iterate()</literal>会首先""使用<emphasis>1</emphasis>条语句得到所有对象的持久化标识(identifiers),再根据""持久化标识执行<emphasis>n</emphasis>条附加的select语句实例化实际的对象。"#. Tag: programlisting#: session_api.xml:292#, no-c-formatmsgid """<![CDATA[// fetch ids\n""Iterator iter = sess.createQuery(\"from eg.Qux q order by q.likeliness\").""iterate();\n""while ( iter.hasNext() ) {\n"" Qux qux = (Qux) iter.next(); // fetch the object\n"" // something we couldnt express in the query\n"" if ( qux.calculateComplicatedAlgorithm() ) {\n"" // delete the current instance\n"" iter.remove();\n"" // dont need to process the rest\n"" break;\n"" }\n""}]]>"msgstr ""#. Tag: title#: session_api.xml:296#, no-c-formatmsgid "Queries that return tuples"msgstr "返回元组(tuples)的查询"#. Tag: para#: session_api.xml:298#, no-c-formatmsgid """Hibernate queries sometimes return tuples of objects, in which case each ""tuple is returned as an array:"msgstr """(译注:元组(tuples)指一条结果行包含多个对象) Hibernate查询有时返回元组""(tuples),每个元组(tuples)以数组的形式返回:"#. Tag: programlisting#: session_api.xml:303#, no-c-formatmsgid """<![CDATA[Iterator kittensAndMothers = sess.createQuery(\n"" \"select kitten, mother from Cat kitten join kitten.mother mother""\")\n"" .list()\n"" .iterator();\n""\n""while ( kittensAndMothers.hasNext() ) {\n"" Object[] tuple = (Object[]) kittensAndMothers.next();\n"" Cat kitten = (Cat) tuple[0];\n"" Cat mother = (Cat) tuple[1];\n"" ....\n""}]]>"msgstr ""#. Tag: title#: session_api.xml:308#, no-c-formatmsgid "Scalar results"msgstr "标量(Scalar)结果"#. Tag: para#: session_api.xml:310#, no-c-formatmsgid """Queries may specify a property of a class in the <literal>select</literal> ""clause. They may even call SQL aggregate functions. Properties or aggregates ""are considered \"scalar\" results (and not entities in persistent state)."msgstr """查询可在<literal>select</literal>从句中指定类的属性,甚至可以调用SQL统计""(aggregate)函数。 属性或统计结果被认定为\"标量(Scalar)\"的结果(而不是持久""(persistent state)的实体)。"#. Tag: programlisting#: session_api.xml:316#, no-c-formatmsgid """<![CDATA[Iterator results = sess.createQuery(\n"" \"select cat.color, min(cat.birthdate), count(cat) from Cat cat \" ""+\n"" \"group by cat.color\")\n"" .list()\n"" .iterator();\n""\n""while ( results.hasNext() ) {\n"" Object[] row = (Object[]) results.next();\n"" Color type = (Color) row[0];\n"" Date oldest = (Date) row[1];\n"" Integer count = (Integer) row[2];\n"" .....\n""}]]>"msgstr ""#. Tag: title#: session_api.xml:321#, no-c-formatmsgid "Bind parameters"msgstr "绑定参数"#. Tag: para#: session_api.xml:323#, no-c-formatmsgid """Methods on <literal>Query</literal> are provided for binding values to named ""parameters or JDBC-style <literal>?</literal> parameters. <emphasis>Contrary ""to JDBC, Hibernate numbers parameters from zero.</emphasis> Named parameters ""are identifiers of the form <literal>:name</literal> in the query string. ""The advantages of named parameters are:"msgstr """接口<literal>Query</literal>提供了对命名参数(named parameters)、JDBC风格的""<literal>问号(?)</literal>参数进行绑定的方法。 <emphasis>不同于JDBC,""Hibernate对参数从0开始计数。</emphasis> 命名参数(named parameters)在查询字符""串中是形如<literal>:name</literal>的标识符。 命名参数(named parameters)的优点""是:"#. Tag: para#: session_api.xml:333#, no-c-formatmsgid """named parameters are insensitive to the order they occur in the query string"msgstr "命名参数(named parameters)与其在查询串中出现的顺序无关"#. Tag: para#: session_api.xml:339#, no-c-formatmsgid "they may occur multiple times in the same query"msgstr "它们可在同一查询串中多次出现"#. Tag: para#: session_api.xml:344
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -