Object identifiers are equivalent to primary keys in data tables and play an important role in persistence. Nhibernate identifies two persistent objects by using object identifiers.
In the ing file, the ID attribute is used to define the object identity. The content is as follows:
The unsaved-value attribute is used to specify the value when the object is not persistent. If this value does not match the Identifier value of the non-persistent object, the object cannot be saved. Generator is used to specify the object type, commonly used include identity and assigned.
The identifier object is a class that implements the iidentitiergenerator interface. The identitiergeneratorfactory class is created based on the ID type of the ing file. The iidentifiergenerator defines the generate method to generate an object ID.
1. Identify object Creation
The identifier object is created in the persistent class uploactentitypersister, through which we can operate on the identifier of the Persistent Object.
// *** Registractentitypersister. CS ***
Public Virtual iidentifiergenerator identifiergenerator {
Get {
If (idgen = NULL ){
Throw new hibernateexception ("...");
}
Return idgen;
}
}
Idgen is assigned a value in the constructor.
Protected extends actentitypersister (persistentclass model, isessionfactoryimplementor factory ){
//...
// Generator
Idgen = model. identifier. createidentifiergenerator (dialect );
Useidentitycolumn = idgen is identitygenerator;
Identityselectstring = useidentitycolumn? Dialect. identityselectstring: NULL;
//...
}
Model is a persistentclass or its subclass, and identifier is a property of the value type.
// *** Value. CS ***
Public iidentifiergenerator createidentifiergenerator (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 ){
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 icyclable) (icyclable) idgen). Configure (type, parms, dialect );
Return idgen;
}
Catch (exception e ){
Throw new mappingexception ("cocould not instantiate ID generator", e );
}
}
The create method creates an object by identifying the object class name.
2. Identify the use of objects in persistence
In session and persistence operations, I mentioned that the current session will store the objects to be persisted until the session is flush or closed. The set of persistent objects to be stored is entitiesbykey, which is a hashtable. Its key is a key object and its value is a persistent object. The key object simply stores the ID and identifierspace of the Persistent Object.
During the persistence operation, Nhibernate must first check whether the object is in the entitiesbykey, which is completed by the getentity method, and then perform subsequent processing based on whether the object is in the set.
// *** Sessionimpl. CS ***
Public object getentity (Key key ){
Return entitiesbykey [Key];
}
Let's take a look at the processing in 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 );
//...
}
If the object is updated for the first time, the old object is empty, and the operation is executed smoothly, and the object is added to the set,
When update is called again (in the same session, and is not called, The flush operation is triggered). If old is not empty, an exception is thrown.