alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Hibernate example source code file (best_practices.po)

This example Hibernate source code file (best_practices.po) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Hibernate tags/keywords

do, dto, hibernate, hibernate, in, jdbc, jdbc, session, tag, tag, this, this, use, you

The Hibernate best_practices.po source code

# translation of Collection_Mapping.po to
# Xi Huang <xhuang@redhat.com>, 2006.
# Xi HUANG <xhuang@redhat.com>, 2007.
msgid ""
msgstr ""
"Project-Id-Version: Collection_Mapping\n"
"Report-Msgid-Bugs-To: http://bugs.kde.org\n"
"POT-Creation-Date: 2010-02-11T05:38:15\n"
"PO-Revision-Date: 2010-03-16 10:01+1000\n"
"Last-Translator: Xi HUANG <xhuang@redhat.com>\n"
"Language-Team:  <en@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: KBabel 1.11.4\n"

#. Tag: title
#, no-c-format
msgid "Best Practices"
msgstr "最佳实践(Best Practices)"

#. Tag: term
#, no-c-format
msgid "Write fine-grained classes and map them using <literal><component>:"
msgstr "设计细颗粒度的持久类并且使用 <literal><component> 来实现映射:"

#. Tag: para
#, no-c-format
msgid "Use an <literal>Address class to encapsulate street, suburb, state, postcode. This encourages code reuse and simplifies refactoring."
msgstr "使用一个 <literal>Address 持久类来封装 streetsuburbstatepostcode。 这将有利于代码重用和简化代码重构(refactoring)的工作。"

#. Tag: term
#, no-c-format
msgid "Declare identifier properties on persistent classes:"
msgstr "对持久类声明标识符属性(identifier properties):"

#. Tag: para
#, no-c-format
msgid "Hibernate makes identifier properties optional. There are a range of reasons why you should use them. We recommend that identifiers be 'synthetic', that is, generated with no business meaning."
msgstr "Hibernate 中标识符属性是可选的,不过有很多原因来说明你应该使用标识符属性。我们建议标识符应该是“人造”的(自动生成,不涉及业务含义)。 "

#. Tag: term
#, no-c-format
msgid "Identify natural keys:"
msgstr "使用自然键(natural keys)标识:"

#. Tag: para
#, no-c-format
msgid "Identify natural keys for all entities, and map them using <literal><natural-id>. Implement equals() and hashCode() to compare the properties that make up the natural key."
msgstr "对所有的实体都标识出自然键,用 <literal><natural-id> 进行映射。实现 equals()hashCode(),在其中用组成自然键的属性进行比较。"

#. Tag: term
#, no-c-format
msgid "Place each class mapping in its own file:"
msgstr "为每个持久类写一个映射文件:"

#. Tag: para
#, no-c-format
msgid "Do not use a single monolithic mapping document. Map <literal>com.eg.Foo in the file com/eg/Foo.hbm.xml. This makes sense, particularly in a team environment."
msgstr "不要把所有的持久类映射都写到一个大文件中。把 <literal>com.eg.Foo 映射到 com/eg/Foo.hbm.xml 中。在团队开发环境中,这一点尤其重要。"

#. Tag: term
#, no-c-format
msgid "Load mappings as resources:"
msgstr "把映射文件作为资源加载:"

#. Tag: para
#, no-c-format
msgid "Deploy the mappings along with the classes they map."
msgstr "把映射文件和他们的映射类放在一起进行部署。"

#. Tag: term
#, no-c-format
msgid "Consider externalizing query strings:"
msgstr "考虑把查询字符串放在程序外面:"

#. Tag: para
#, no-c-format
msgid "This is recommended if your queries call non-ANSI-standard SQL functions. Externalizing the query strings to mapping files will make the application more portable."
msgstr "如果你的查询中调用了非 ANSI 标准的 SQL 函数,那么这条实践经验对你适用。把查询字符串放在映射文件中可以让程序具有更好的可移植性。 "

#. Tag: term
#, no-c-format
msgid "Use bind variables."
msgstr "使用绑定变量"

#. Tag: para
#, no-c-format
msgid "As in JDBC, always replace non-constant values by \"?\". Do not use string manipulation to bind a non-constant value in a query. You should also consider using named parameters in queries."
msgstr "就像在 JDBC 编程中一样,应该总是用占位符 \"?\" 来替换非常量值,不要在查询中用字符串值来构造非常量值。你也应该考虑在查询中使用命名参数。"

#. Tag: term
#, no-c-format
msgid "Do not manage your own JDBC connections:"
msgstr "不要自己来管理 JDBC 连接:"

#. Tag: para
#, no-c-format
msgid "Hibernate allows the application to manage JDBC connections, but his approach should be considered a last-resort. If you cannot use the built-in connection providers, consider providing your own implementation of <literal>org.hibernate.connection.ConnectionProvider."
msgstr "Hibernate 允许应用程序自己来管理 JDBC 连接,但是应该作为最后没有办法的办法。如果你不能使用 Hibernate 内建的 connections providers,那么考虑实现自己来实现 <literal>org.hibernate.connection.ConnectionProvider。"

#. Tag: term
#, no-c-format
msgid "Consider using a custom type:"
msgstr "考虑使用用户自定义类型(custom type):"

#. Tag: para
#, no-c-format
msgid "Suppose you have a Java type from a library that needs to be persisted but does not provide the accessors needed to map it as a component. You should consider implementing <literal>org.hibernate.UserType. This approach frees the application code from implementing transformations to/from a Hibernate type."
msgstr "假设你有一个 Java 类型,来自某些类库,需要被持久化,但是该类没有提供映射操作需要的存取方法。那么你应该考虑实现 <literal>org.hibernate.UserType 接口。这种办法使程序代码写起来更加自如,不再需要考虑类与 Hibernate type 之间的相互转换。 "

#. Tag: term
#, no-c-format
msgid "Use hand-coded JDBC in bottlenecks:"
msgstr "在性能瓶颈的地方使用硬编码的 JDBC:"

#. Tag: para
#, no-c-format
msgid "In performance-critical areas of the system, some kinds of operations might benefit from direct JDBC. Do not assume, however, that JDBC is necessarily faster. Please wait until you <emphasis>know something is a bottleneck. If you need to use direct JDBC, you can open a Hibernate Session, wrap your JDBC operation as a org.hibernate.jdbc.Work object and using that JDBC connection. This way you can still use the same transaction strategy and underlying connection provider."
msgstr "在系统中对性能要求很严格的一些部分,某些操作也许直接使用 JDBC 会更好。但是请先<emphasis>确认这的确是一个瓶颈,并且不要想当然认为 JDBC 一定会更快。如果确实需要直接使用 JDBC,那么最好打开一个 Hibernate Session 然后将 JDBC 操作包裹为 org.hibernate.jdbc.Work 并使用 JDBC 连接。按照这种办法你仍然可以使用同样的 transaction 策略和底层的 connection provider。 "

#. Tag: term
#, no-c-format
msgid "Understand <literal>Session flushing:"
msgstr "理解 <literal>Session 冲刷(flushing):"

#. Tag: para
#, no-c-format
msgid "Sometimes the Session synchronizes its persistent state with the database. Performance will be affected if this process occurs too often. You can sometimes minimize unnecessary flushing by disabling automatic flushing, or even by changing the order of queries and other operations within a particular transaction."
msgstr "Session 会不时的向数据库同步持久化状态,如果这种操作进行的过于频繁,性能会受到一定的影响。有时候你可以通过禁止自动 flushing,尽量最小化非必要的 flushing 操作,或者更进一步,在一个特定的 transaction 中改变查询和其它操作的顺序。 "

#. Tag: term
#, no-c-format
msgid "In a three tiered architecture, consider using detached objects:"
msgstr "在三层结构中,考虑使用脱管对象(detached object):"

#. Tag: para
#, no-c-format
msgid "When using a servlet/session bean architecture, you can pass persistent objects loaded in the session bean to and from the servlet/JSP layer. Use a new session to service each request. Use <literal>Session.merge() or Session.saveOrUpdate() to synchronize objects with the database."
msgstr "当使用一个 servlet / session bean 类型的架构的时候, 你可以把已加载的持久对象在 session bean 层和 servlet / JSP 层之间来回传递。使用新的 session 来为每个请求服务,使用 <literal>Session.merge() 或者 Session.saveOrUpdate() 来与数据库同步。 "

#. Tag: term
#, no-c-format
msgid "In a two tiered architecture, consider using long persistence contexts:"
msgstr "在两层结构中,考虑使用长持久上下文(long persistence contexts):"

#. Tag: para
#, no-c-format
msgid "Database Transactions have to be as short as possible for best scalability. However, it is often necessary to implement long running <emphasis>application transactions, a single unit-of-work from the point of view of a user. An application transaction might span several client request/response cycles. It is common to use detached objects to implement application transactions. An appropriate alternative in a two tiered architecture, is to maintain a single open persistence contact session for the whole life cycle of the application transaction. Then simply disconnect from the JDBC connection at the end of each request and reconnect at the beginning of the subsequent request. Never share a single session across more than one application transaction or you will be working with stale data."
msgstr "为了得到最佳的可伸缩性,数据库事务(Database Transaction)应该尽可能的短。但是,程序常常需要实现长时间运行的<emphasis>“应用程序事务(Application Transaction)”,包含一个从用户的观点来看的原子操作。这个应用程序事务可能跨越多次从用户请求到得到反馈的循环。用脱管对象(与 session 脱离的对象)来实现应用程序事务是常见的。或者,尤其在两层结构中,把 Hibernate Session 从 JDBC 连接中脱离开,下次需要用的时候再连接上。绝不要把一个 Session 用在多个应用程序事务(Application Transaction)中,否则你的数据可能会过期失效。"

#. Tag: term
#, no-c-format
msgid "Do not treat exceptions as recoverable:"
msgstr "不要把异常看成可恢复的:"

#. Tag: para
#, no-c-format
msgid "This is more of a necessary practice than a \"best\" practice. When an exception occurs, roll back the <literal>Transaction and close the Session. If you do not do this, Hibernate cannot guarantee that in-memory state accurately represents the persistent state. For example, do not use Session.load() to determine if an instance with the given identifier exists on the database; use Session.get() or a query instead."
msgstr "这一点甚至比“最佳实践”还要重要,这是“必备常识”。当异常发生的时候,必须要回滚 <literal>Transaction ,关闭 Session。如果你不这样做的话,Hibernate 无法保证内存状态精确的反应持久状态。尤其不要使用 Session.load() 来判断一个给定标识符的对象实例在数据库中是否存在,应该使用 Session.get() 或者进行一次查询。"

#. Tag: term
#, no-c-format
msgid "Prefer lazy fetching for associations:"
msgstr "对于关联优先考虑 lazy fetching:"

#. Tag: para
#, no-c-format
msgid "Use eager fetching sparingly. Use proxies and lazy collections for most associations to classes that are not likely to be completely held in the second-level cache. For associations to cached classes, where there is an a extremely high probability of a cache hit, explicitly disable eager fetching using <literal>lazy=\"false\". When join fetching is appropriate to a particular use case, use a query with a left join fetch."
msgstr "谨慎的使用主动抓取(eager fetching)。对于关联来说,若其目标是无法在第二级缓存中完全缓存所有实例的类,应该使用代理(proxies)与/或具有延迟加载属性的集合(lazy collections)。若目标是可以被缓存的,尤其是缓存的命中率非常高的情况下,应该使用 <literal>lazy=\"false\",明确的禁止掉 eager fetching。如果那些特殊的确实适合使用 join fetch 的场合,请在查询中使用 left join fetch。 "

#. Tag: term
#, no-c-format
msgid "Use the <emphasis>open session in view pattern, or a disciplined assembly phase to avoid problems with unfetched data:"
msgstr "使用 <emphasis>open session in view 模式,或者执行严格的装配期(assembly phase)策略来避免再次抓取数据带来的问题:"

#. Tag: para
#, no-c-format
msgid "Hibernate frees the developer from writing tedious <emphasis>Data Transfer Objects (DTO). In a traditional EJB architecture, DTOs serve dual purposes: first, they work around the problem that entity beans are not serializable; second, they implicitly define an assembly phase where all data to be used by the view is fetched and marshalled into the DTOs before returning control to the presentation tier. Hibernate eliminates the first purpose. Unless you are prepared to hold the persistence context (the session) open across the view rendering process, you will still need an assembly phase. Think of your business methods as having a strict contract with the presentation tier about what data is available in the detached objects. This is not a limitation of Hibernate. It is a fundamental requirement of safe transactional data access."
msgstr "Hibernate 让开发者们摆脱了繁琐的 <emphasis>Data Transfer Objects(DTO)。在传统的 EJB 结构中,DTO 有双重作用:首先,他们解决了 entity bean 无法序列化的问题;其次,他们隐含地定义了一个装配期,在此期间,所有在 view 层需要用到的数据,都被抓取、集中到了 DTO 中,然后控制才被装到表示层。Hibernate 终结了第一个作用。然而,除非你做好了在整个渲染过程中都维护一个打开的持久化上下文(session)的准备,你仍然需要一个装配期(想象一下,你的业务方法与你的表示层有严格的契约,数据总是被放置到脱管对象中)。这并非是 Hibernate 的限制,这是实现安全的事务化数据访问的基本需求。"

#. Tag: term
#, no-c-format
msgid "Consider abstracting your business logic from Hibernate:"
msgstr "考虑把 Hibernate 代码从业务逻辑代码中抽象出来:"

#. Tag: para
#, no-c-format
msgid "Hide Hibernate data-access code behind an interface. Combine the <emphasis>DAO and Thread Local Session patterns. You can even have some classes persisted by handcoded JDBC associated to Hibernate via a UserType. This advice is, however, intended for \"sufficiently large\" applications. It is not appropriate for an application with five tables."
msgstr "把 Hibernate 的数据存取代码隐藏到接口(interface)的后面,组合使用 <emphasis>DAO 和 Thread Local Session 模式。通过 Hibernate 的UserType,你甚至可以用硬编码的 JDBC 来持久化那些本该被 Hibernate 持久化的类。然而,该建议更适用于规模足够大应用软件中,对于那些只有 5 张表的应用程序并不适合。"

#. Tag: term
#, no-c-format
msgid "Do not use exotic association mappings:"
msgstr "不要用怪异的连接映射:"

#. Tag: para
#, no-c-format
msgid "Practical test cases for real many-to-many associations are rare. Most of the time you need additional information stored in the \"link table\". In this case, it is much better to use two one-to-many associations to an intermediate link class. In fact, most associations are one-to-many and many-to-one. For this reason, you should proceed cautiously when using any other association style."
msgstr "多对多连接用得好的例子实际上相当少见。大多数时候你在“连接表”中需要保存额外的信息。这种情况下,用两个指向中介类的一对多的连接比较好。实际上,我们认为绝大多数的连接是一对多和多对一的。i因此,你应该谨慎使用其它连接风格。"

#. Tag: term
#, no-c-format
msgid "Prefer bidirectional associations:"
msgstr "偏爱双向关联:"

#. Tag: para
#, no-c-format
msgid "Unidirectional associations are more difficult to query. In a large application, almost all associations must be navigable in both directions in queries."
msgstr "单向关联更加难于查询。在大型应用中,几乎所有的关联必须在查询中可以双向导航。"

Other Hibernate examples (source code examples)

Here is a short list of links related to this Hibernate best_practices.po source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.