nhibernate源码分析之五: 对象标识

对象标识相当于数据表中的主键,在持久化中起着十分重要的作用,nhibernate通过对象标识来辨别两个持久对象是否相等。

在映射文件中,通过id属性来定义对象标识,内容如下:

1<id column="order_id" name="orderId" type="Int32" unsaved-value="0">
2<generator class="identity"></generator>
3</id>

其中unsaved-value属性用来指明对象未持久化时的值,如果此值与未持久化的对象标识值不符,将无法save对象,generator用于指定标识对象的类型,常用的有identity, assigned等。

标识对象为实现IIdentitierGenerator接口的类,由IdentitierGeneratorFactory类根据映射文件的标识类型来创建,IIdentifierGenerator定义了Generate方法,用于产生对象标识。

**1. 标识对象的建立

** 标识对象在持久化类AbstractEntityPersister中创建,通过它我们就可以对持久对象的标识进行操作了。

//*** AbstractEntityPersister.cs ***

public virtual IIdentifierGenerator IdentifierGenerator {
get {
if (idgen==null) {
throw new HibernateException("...");
}
return idgen;
}
}
idgen在构造函数中被赋值。

protected AbstractEntityPersister(PersistentClass model, ISessionFactoryImplementor factory) {
// ...

// GENERATOR
idgen = model.Identifier.CreateIdentifierGenerator(dialect);
useIdentityColumn = idgen is IdentityGenerator;
identitySelectString = useIdentityColumn ? dialect.IdentitySelectString : null;

// ...
}
其中model为PersistentClass或其子类,Identifier为Value类型的属性。

// *** Value.cs ***

public IIdentifierGenerator CreateIdentifierGenerator(Dialect.Dialect dialect) {
if ( uniqueIdentifierGenerator==null ) {
uniqueIdentifierGenerator = IdentifierGeneratorFactory.Create(identifierGeneratorStrategy, type, identifierGeneratorProperties, dialect);
}
return uniqueIdentifierGenerator;
}

//*** IdentitifierGeneratorFactory ***

public static IIdentifierGenerator Create(string strategy, IType type, IDictionary parms, Dialect.Dialect dialect) {
try {
System.Type clazz = (System.Type) idgenerators[strategy];
// ...
if (clazz==null) clazz = System.Type.GetType(strategy);
IIdentifierGenerator idgen = (IIdentifierGenerator) Activator.CreateInstance(clazz);
if (idgen is IConfigurable) ((IConfigurable) idgen).Configure(type, parms, dialect);
return idgen;
}
catch (Exception e) {
throw new MappingException("could not instantiate id generator", e);
}
}

Create方法通过标识对象类名来创建标识对象。

2. 标识对象在持久化中的使用

在会话和持久化操作一文,我曾提到当前会话会把要持久化的对象存储起来,直到调用Flush或关闭会话。存储持久对象的集合为entitiesByKey,这是一个Hashtable,它的key为一个Key对象, value为持久对象,Key对象简单的存储持久对象的id和IdentifierSpace。

在进行持久化操作时,nhibernate必须首先检查对象是否在entitiesByKey中,这由GetEntity方法完成,然后再根据对象是否在集合中作后续处理。

//*** SessionImpl.cs ***

public object GetEntity(Key key) {
return entitiesByKey[key];
}

下面来看看DoUpdate中的处理:

private void DoUpdate(object obj, object id) {
// ...

Key key = new Key(id, persister);
object old = GetEntity(key);
if (old==obj) {
throw new AssertionFailure("Hibernate has a bug in Update() ... or you are using an illegal id type");
}
else if ( old!=null ) {
throw new HibernateException("Another object was associated with this id ( the object with the given id was already loaded)");
);

// ...

AddEntity(key, obj);
AddEntry(obj, Status.Loaded, null, id, persister.GetVersion(obj), LockMode.None, true, persister);

// ...
}
如果首次对持久对象执行Update,此时old为空,操作顺利执行,并且对象被加入到集合中,
当再次调用Update时(在同一会话中,并且没有调用会导致Flush的操作),此时old不为空,将引发一个异常。

Published At
Categories with Web编程
Tagged with
comments powered by Disqus