日期:2014-05-16  浏览次数:20630 次

HIbernate符合Java习惯的关系数据库持久化之Persistent

实现一个默认的(即无参数的)构造方法(constructor)

Cat has a no-argument constructor. All persistent classes must have a default constructor (which can be non-public) so that Hibernate can instantiate them using Constructor.newInstance() . It is recommended that you have a default constructor with at least package visibility for runtime proxy generation in Hibernate

所有持久化类必须有一个默认无参构造器,可以不是公共的以便HIbernate实例化。

提供一个标识属性(identifier property)(可选)

标识符属性是可选的。可以不用管它,让Hibernate内部来追踪对象的识别。 但是我们并不推荐这样做。

In fact, some functionality is available only to classes that declare an identifier property:

  • Transitive reattachment for detached objects (cascade update or cascade merge)?

  • Session.saveOrUpdate()

  • Session.merge()

We recommend that you declare consistently-named identifier properties on persistent classes and that you use a nullable (i.e., non-primitive) type.

推荐声明主键

使用非final的类 (可选)

代理(proxies) 是Hibernate的一个重要的功能,它依赖的条件是,持久 化类或者是非final的,或者是实现了一个所有方法都声明为public的接口。

You can persist final classes that do not implement an interface with Hibernate. You will not, however, be able to use proxies for lazy association fetching which will ultimately limit your options for ? performance tuning.

可以不通过接口实现持久化,

你也应该避免在非final类中声明 public final 的方法。如果你想使用一 个有public final 方法的类,你必须通过设置lazy="false" 来明确地禁用代理。

为持久化字段声明访问器(accessors)和是否可变的标志(mutators)(可选)

Cat declares accessor methods for all its persistent fields. Many other ORM tools directly persist instance variables. It is better to provide an indirection between the relational schema and internal data structures of the class. By default, Hibernate persists JavaBeans style properties and recognizes method names of the form getFoo , isFoo and setFoo . If required, you can switch to direct field access for particular properties.

属性不需要 要声明为public的。Hibernate可以持久化一个有 defaultprotectedprivate 的get/set方法对 的属性进行持久化。