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

本文介绍Hibernate持久化配置的关键要素,包括默认构造方法、标识属性、非final类、访问器及可变标志等,强调了这些配置对于实现高效、可靠的Hibernate应用的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现一个默认的(即无参数的)构造方法(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方法对 的属性进行持久化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值