Hibernate学习之四-------The Persistence Life Cycle



 1. Life Cycle

Given an instance of an object that is mapped to Hibernate, it can be in any one of three different states: transient, persistent, or detached.

映射到Hibernate中的类可以是以下三种状态中的一种:瞬时的,持久的,分离的。

Transient objects exist in memory, Hibernate does not manage transient objects or persist changes to transient objects.

 

Persistent objects exist in the database, and Hibernate manages the persistence for persistent objects. If fields or propertieschange on a persistent object, Hibernate will keep the database representation up-to-date.

 

Detached objects have a representation in the database, but changes to the object will not be reflected in the database, and vice versa(反之亦然). A detached object can be created by closing the session that it was associated with, or by evicting it from the session with a call to the session’ s evict() method.

当关闭当前的连接此时的对象和数据库立之间的联系的断开了。

 

In order to persist changes made to a detached object, the application must reattach it to a valid Hibernate session. A detachedinstance can be associated with a new Hibernate session when your application calls one of the load(), refresh(), merge(), update(), or save()methods on the new session with a reference to the detached object. After the call, the detached object would be a persistent object managed by the new Hibernate session.重新获得连接后,分离的对象就会转变成持久的对象。
 

2. Associations 关系


 

 

 3.Saving Entities 保存实体对象

也就是将瞬时对象持久化到数据库中去,提供的方法:

public Serializable save(Object object) throws HibernateException
public void save(Object object, Serializable id) throws HibernateException
public Serializable save(String entityName,Object object) throws HibernateException

参数中的对象均是瞬时对象(必须要有对应的映射文件)

public void saveOrUpdate(Object object) throws HibernateException  Hibernate自动判断是更新还是插入新记录。

 

4.Object Equality and Identity 对象的相等性和同一性

从一个Hibernate会话中重复请求一个持久化对象,就会返回一个Java类的相同实例,可以用==来比较。但是,如果是从多个Hibernate会话中请求一个持久化对象,Hibernate就会从每个会话中提供不同的实例,==会返回fase,这时要比较对象,就应该用equals()方法了。

数据库中的一个表中的一行记录相当于 对应的实体类的一个对象实例。

 

5.Updating Entities ,Deleting Entities ,Refreshing Entities ,Loading Entities

 

6. Cascading Operations

 

7.Lazy Loading,Proxies,and Collection Wrappers(需要深入学习)

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值