11using System ;
22using NHibernate . Cfg . MappingSchema ;
33using NHibernate . Mapping ;
4+ using NHibernate . Type ;
45
56namespace NHibernate . Cfg . XmlHbmBinding
67{
78 public class ClassIdBinder : ClassBinder
89 {
9- public ClassIdBinder ( ClassBinder parent )
10- : base ( parent )
11- {
12- }
10+ public ClassIdBinder ( ClassBinder parent ) : base ( parent ) { }
1311
1412 public void BindId ( HbmId idSchema , PersistentClass rootClass , Table table )
1513 {
@@ -21,16 +19,20 @@ public void BindId(HbmId idSchema, PersistentClass rootClass, Table table)
2119 rootClass . Identifier = id ;
2220
2321 Func < HbmColumn > defaultColumn = ( ) => new HbmColumn
24- {
25- name = idSchema . name ?? RootClass . DefaultIdentifierColumnName ,
26- length = idSchema . length
27- } ;
22+ {
23+ name = idSchema . name ?? RootClass . DefaultIdentifierColumnName ,
24+ length = idSchema . length
25+ } ;
26+
2827 new ColumnsBinder ( id , Mappings ) . Bind ( idSchema . Columns , false , defaultColumn ) ;
2928
3029 CreateIdentifierProperty ( idSchema , rootClass , id ) ;
31- VerifiyIdTypeIsValid ( id , rootClass . EntityName ) ;
32- new IdGeneratorBinder ( Mappings ) . BindGenerator ( id , idSchema . generator ) ;
30+ VerifiyIdTypeIsValid ( id . Type , rootClass . EntityName ) ;
31+
32+ new IdGeneratorBinder ( Mappings ) . BindGenerator ( id , GetIdGenerator ( idSchema ) ) ;
33+
3334 id . Table . SetIdentifierValue ( id ) ;
35+
3436 BindUnsavedValue ( idSchema , id ) ;
3537 }
3638 }
@@ -40,10 +42,9 @@ private void CreateIdentifierProperty(HbmId idSchema, PersistentClass rootClass,
4042 if ( idSchema . name != null )
4143 {
4244 string access = idSchema . access ?? mappings . DefaultAccess ;
43- id . SetTypeUsingReflection ( rootClass . MappedClass == null ? null : rootClass . MappedClass . AssemblyQualifiedName ,
44- idSchema . name , access ) ;
45+ id . SetTypeUsingReflection ( rootClass . MappedClass == null ? null : rootClass . MappedClass . AssemblyQualifiedName , idSchema . name , access ) ;
4546
46- var property = new Property ( id ) { Name = idSchema . name } ;
47+ var property = new Property ( id ) { Name = idSchema . name } ;
4748
4849 if ( property . Value . Type == null )
4950 throw new MappingException ( "could not determine a property type for: " + property . Name ) ;
@@ -62,19 +63,23 @@ private void CreateIdentifierProperty(HbmId idSchema, PersistentClass rootClass,
6263 }
6364 }
6465
65- private static void VerifiyIdTypeIsValid ( IValue id , string className )
66+ private HbmGenerator GetIdGenerator ( HbmId idSchema )
67+ {
68+ return String . IsNullOrEmpty ( idSchema . generator1 ) ? idSchema . generator : new HbmGenerator ( ) { @class = idSchema . generator1 } ;
69+ }
70+
71+ private static void VerifiyIdTypeIsValid ( IType idType , string className )
6672 {
67- if ( id . Type == null )
73+ if ( idType == null )
6874 throw new MappingException ( string . Format ( "Must specify an identifier type: {0}." , className ) ) ;
6975
70- if ( id . Type . ReturnedClass . IsArray )
71- throw new MappingException (
72- "Illegal use of an array as an identifier (arrays don't reimplement equals)." ) ;
76+ if ( idType . ReturnedClass . IsArray )
77+ throw new MappingException ( "Illegal use of an array as an identifier (arrays don't reimplement equals)." ) ;
7378 }
7479
7580 private static void BindUnsavedValue ( HbmId idSchema , SimpleValue id )
7681 {
7782 id . NullValue = idSchema . unsavedvalue ?? ( id . IdentifierGeneratorStrategy == "assigned" ? "undefined" : null ) ;
7883 }
7984 }
80- }
85+ }
0 commit comments