Anda di halaman 1dari 13

cc c

c
¯  

~c ¯    
 
~c              
~c ×                          
      ¯ × !
~c ¯   "  ¯      #              
                    
~c Ô    $   %    

& ¯  

<?xml version="1.0" encoding="utf-8" ?>


<hibernate-configuration xmlns="urn:nhibernate -configuration-2.2">
<session-factory>
<property name="dialect">
NHibernate.Dialect.MsSql2005Dialect </property>
<property
name="connection.provider" >NHibernate.Connection.DriverConnection Provider</propert
y>
<property name="connection.connection_string" >
Server=(local)\SQLExpress;initial catalog=NH_MappingExercise2;Integrated
Security=SSPI
</property>
<property name='proxyfactory.factory_class' >
NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
</property>
<mapping assembly="MappingExercise" />
</session-factory>
</hibernate-configuration>

'"   

~c ¯ 
Æc ¯   (%) )¯   "*
~c %   +    #              
~c "  
~c   "  %       '"    $      , 
    
~c &   "  '       $        $ ,  $ ,

        $        - #    "   (* 


   

var cfg = new Configuration ().Configure("hibernate.cfg.xml" );


var sessionFactory = cfg.BuildSessionFactory ();

" & 

   $                  

  $   " &       ¯   

var cfg = new Configuration ().Configure("hibernate.cfg.xml" );


new SchemaExport (cfg).Create(true, true);
var sessionFactory = cfg.BuildSessionFactory ();

      " & ¯  (*        $    "./ 


            0 "  $ $

          $             

   +    $            

'   $         +   " 1 &  (*

var cfg = new Configuration ().Configure("hibernate.cfg.xml" );


new SchemaUpdate (cfg).Execute(true,true);
var sessionFactory = cfg.BuildSessionFactory ();

"  

~c ¯    
~c
2   
~c
2    
~c 1   ,      $ $          
Æc Ô $   #  ,    
~c ¯    +  
Æc '    #               
       
Æc
        
~c '       $   2
&                2
&
   "         0& 0& Ô   
      
 +     $     +     

var session = sessionFactory .OpenSession()

 
~c    %Ô
Ô 2
~c Ô    , $            $   
 +  $    '  # +               
  #      $  ( #  #    *
~c   $  3    $  #   ,       #
$  +            $  #     
     (         $  *#    
  
~c
             +   $   $ ,     
~c " ))  )/ )Ô )
1 '        

using (var transaction = session.BeginTransaction ())


{
Console.WriteLine("Deleting a car" );
var car = session.Get<Car>(1);
session.Delete(car);
transaction.Commit();
}

â c c
c c  c
    $    -      

~c "  / 4 5(*


Æc         $               
              +    - 
Æc  $       
~c "  6 4 5(*
Æc       $           #       
      
Æc                #  $ $  
           +  
~c "  . (*
Æc ¯  #     (      *
Æc  $ $      #  $         +   
~c /   6        $      '
&0&       
~c '                            $
$ +   -        

var car = session.Load<Car>(1); // will not hit the db

var carName = car.Name; // will load the all the entity's properties

var car2 = session.Get<Car>(1); // this will not hit the db as it has already been
loaded previously by accessing the Name property

var cars = session.CreateCriteria <Car>().List<Car>(); // will hit the db for every


car.

Î c
~c / / #  $    +           $       
   
~c   +              #       $ 
   
~c
  $       (           *   
      # $         $     Ô    
      $             $   - 
~c    / 4 5(*    $      -  $      #  $
     -            7     , $    - 
   +  '   $       -     6 4 5(*
~c '      / (*          $        
(      *
~c '+          #           
"  6  $           $     $    
~c
         

c
Ô     

~c Ô  
~c
 
Æc '            7           
~c ¯
~c '

<?xml version="1.0" encoding="utf-8" ?>


<hibernate-mapping xmlns="urn:nhibernate -mapping-2.2" assembly="MappingExercise"
namespace="MappingExercise.Domain" >
<class name="Car">
<id name="Id">
<generator class="native"/>
</id>
</class>
</hibernate-mapping> c

ü c
~c 6
Æc + 
Uc
       $    
Uc /     
Uc         
~c Ô 
Æc %        $    
~c ' 
Æc 6                  (   *
Æc
      
~c ¯   '
Æc 1           
Æc %    (*    ¯  (*
Æc
     
~c  ) / $
Æc
     '    
Æc ¯          " $               
         89#:::           77
   $        - 
Æc " &  $     
Æc  
Æc                
Æc Ô $ ,          -   $ "./   +   
      $ "./ (       *

<id name="Id">
<generator class="hilo">
<!-- Optional paramaters for specifying which table/column to use -->
<param name="table">hibernate_unique_key </param>
<param name="column">classB_nexthi </param>
<param name="max_lo">20</param>
</generator>
</id>c

c
×

 c

1        +         #            


 

<property name="PropertyName"
column="column_name"
type="typename"
update="true|false"
insert="true|false"
formula="arbitary sql expression"
access="field|property|ClassName"
optimistic-lock="true|false"
generated="never|insert|always"
/>

~c
             - 
~c ¯             
Æc
            
~c  
   #     #    $ 
Æc
    $       
~c 1
Æc
       
Æc      ;     
~c ' 
Æc
       
Æc     $ +    +   
~c  
Æc
   
Æc ¯        
~c Ô 
Æc
         
Æc '         
Æc ¯
    $      
~c 2;/ ,
Æc
        ¯ 

 

Æc '         $      ,


~c 6   
Æc
        + 
Æc ' $ +  + $         


  c

1       $  $  #            
      ,    

<many-to-one name="PropertyName"
column="column_name"
class="ClassName"
cascade="all|none|save -update|delete"
fetch="join|select"
update="true|false"
insert="true|false"
property-ref="PropertyNameFromAssociatedClass"
access="field|property|nosetter|ClassName"
unique="true|false"
optimistic-lock="true|false"
not-found="ignore|exception"

ccccccccccccccccc c
~c
 
        
~c ¯  
     $     
Æc
          
Æc " &  $  
~c ¯  ¯      
Æc
   #
  $     
~c ¯
Æc
   #     
Æc     + # $      
     +  
3   #             # 
 +         $ + ;  $    + 
  $    - # $             
Uc   ;       #             + 
Uc + ; ; $   -   + ) #  ,     
+ )  -      ( + ) 
    ; ;   *
Uc    ; $   -      #      -      
Uc    ;  ; $   -      #      -   
   '    # $   -     +    
         $    -  (  *#    

Uc  ; $   -   + ) )   #  ,     
+ ) )     -   
Uc ;   ;  ; $   -   + ) )   #  , 
    + ) )     -    '   
# $   -     +             
$    -  (  *#     
~c  
Æc
   #      
Æc      -    ;   $   ;     
Æc 2 $ , $       
~c 1
Æc
   #    
Æc  '     $  '   
Æc &+           $   + 
~c ' 
Æc     $         
~c ×  ; 
Æc
   #     
Æc /,                 '      
Æc Ô+   
~c Ô 
~c 1
Æc     +     
          $ 
       $  $     /
~c 2  ,
Æc   
~c
; 
Æc
   #     
Æc     # $   <        $   

¯   c

   =          

~c " 
Æc Ô            
Æc        
Æc            #  $   +           
   
Æc   $             
Æc '" 
Æc 
 +    $ "  ¯   # 
     '"     
  ' ¯   
Æc   " 4 5(*>
Æc        ü #
   3 +     $ $ 
     ? ? ¯    ü  $ $ , + ü 
   
(  +  $* 2 #              
  
Æc
<set
name="PropertyName"
table="TableName"
schema="SchemaName"
lazy="true|false"
inverse ="true|false"
cascade="all|none|save -update|delete|all -delete-orphan"
sort="unsorted|natural|comparatorClass"
order-by="ColumnName asc|desc"
where="arbitrary sql where condition"
fetch="select|join"
batch-size="N"
access="field|property|className"
optimistic-lock="true|false"
outer-join="true|false">
<key />
<one-to-many/>
<many-to-many/>
</set>
~c
 ; ×  
    
~c   ;  
              
~c "   '              &  1 
~c /      '     $         
Æc '         -   $  $        
~c '+     
Æc ' +       #            $    
  '                     
$    
Æc 1    $ %; ;         $  ; ;
   
~c ¯
Æc
   #     
Æc     + # $      
     +  
3   #             # 
 +         $ + ;  $    + 
  $    - # $             
Uc   ;       #             + 
Uc + ; ; $   -   + ) #  ,     
+ )  -      ( + ) 
    ; ;   *
Uc    ; $   -      #      -      
Uc    ;  ; $   -      #      -   
   '    # $   -     +    
         $    -  (  *#    

Uc  ; $   -   + ) )   #  ,     
+ ) )     -   
Uc ;   ;  ; $   -   + ) )   #  , 
    + ) )     -    '   
# $   -     +             
$    -  (  *#     
~c " 
Æc   
~c  
Æc
         
Æc &   -  $    +     #    $       
~c ;
Æc    
Æc ×    
Æc & '      @           #       ;  =#
=      $      #  A    
Æc       $              
Æc 2    
~c Ô 
Æc        
~c 2;/ ,       
~c 2 ;B  C
~c < 
Æc          
Æc ¯ D4
   <  5
~c 2 ; ; 
~c %; ;

   c
    ; ;    & Ô         ,  ,    Ô $
   +         $     ,             ; ;
     $  2   %; ;  $   Ô    $   +  
        

<set name="Cars">
<key column="Driver" />
<one-to-many class="Car"/>
</set>
           ; ;            

  ,

Ô      ; ;      

<many-to-one name="Driver" />


  c
%; ;    +      '            
            

"         ; ;        ,        


$       
~c '   #         $  C

† 
'      $      $      

~c     $           +            


~c "2 $                 $    $  $   
   1   6  Ô   $ ,          6 
 $ +     #      $ +      
~c  +      ‘ E      $         

  
      ; ;          /     "   + 


 
'       $  #     '

¦ 
c
x   
 
  

 



  c
      ; ;      $  0    + 

ü ccc

 c
<set name=ŪVehiclesŪ cascade=ŪallŪ table=ŪDriverVehiclesŪ >
<key column="DriverId" />
<many-to-many class="Vehicle" column="VehicleId" />
</set>c

ü cc c

 c
<bag name="Drivers" inverse="true" table="DriverVehicles" >
<key column="VehicleId" />
<many-to-many class="Driver" column="DriverId"/>
</bag>c

~c '
Æc Ô             
Æc Ô $     ; ;          +     
$    , 
Æc        G         G
 
   +  $           +# 
  #    
~c /
Æc Ô           
Æc      #  $              
Æc Ô $  +      
Æc                 #
    
  
Æc    $ /4 5(*>
~c 
Æc Ô              
Æc
 , 
Æc B 
Æc '¯   
Æc        $  (  *       
          $             
$      ( *     E    ü 
 $        ü  (,  ü *
Æc    $ /4 5(*>
~c %
Æc ¯     ,  + 
Æc ' 
Æc '  $  4 , # 0 5(*>

ü


c
'        $  
            
                               #
     $          $   1' $      1'   
,#       

2
2
ü 
1' /  
"    
  

' / 
 
/ 
  / 

 Ô    
/ 

c×
 c
2        

1   6   "  /        '


c×
 c
"       #          

+            '#        

 c¦ c
c c¦c

     $   $      # $      


" 1           
% 

¯    7                  ' ×%   


Ô  

Ô $        ¯   #   '.      +         
 1'