1.配置文件如下:
 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
 3                     namespace="XXXX.XXXXXXX.Entities.MemberModule"
 4                     assembly="XXXX.XXXX.XXXXX"
 5                     default-lazy="false">
 6   <class name="ScoreConfig" table="member_scoreconfig">
 7     <composite-id class="ScoreConfigID" name="ID">
 8       <key-property name="MemberRate" column="MemberRate" type="Int32"/>
 9       <key-property name="ScoreGetType" column="ScoreGetType" type="Int32"/>
10     </composite-id>
11     <property name="Score" column="Score" type="Int32"/>
12     <property name="UpdateDate" column="UpdateDate" type="DateTime"/>
13   </class>
14 </hibernate-mapping>

2.我們不僅要添加ScoreConfig這個Entity還要添加"ScoreConfigID"這個entity

  

代码
public class ScoreConfig
    {

        
public ScoreConfigID ID
        {
            
get;
            
set;
        }
        
public int Score
        {
            
get;
            
set;
        }
        
public DateTime UpdateDate
        {
            
get;
            
set;
        }
    }

 

 

代码
public class ScoreConfigID
    {

        
public int MemberRate
        {
            
get;
            
set;
        }
        
public ScoreGetType ScoreGetType
        {
            
get;
            
set;
        }
        
public override bool Equals(object obj)
        {
            
if (ReferenceEquals(null, obj))
            {
                
return false;
            }
            
if (ReferenceEquals(this, obj))
            {
                
return true;
            }
            
if (obj.GetType() != typeof(ScoreConfigID))
            {
                
return false;
            }
            
return Equals((ScoreConfigID)obj);
        }
        
public override int GetHashCode()
   
     {
            
return base.GetHashCode();
        }
    }

注意:ID的這一個類裏面要重寫Equal()方法與GetHashCode()方法,否則會報錯

在ScoreConfigID裏面出現在的字段不必在ScoreConfig裏面出現,(以免出現重復的字段)
添加entity的代碼

 

1  public void AddScoreConfig(ScoreConfig config)
2         {
3             if (config.Score <= 0)
4             {
5                 throw new Exception("分數不能為零!");
6             }
7             config.UpdateDate = System.DateTime.Now;
8             _provider.AddScoreConfig(config);
9         }

 


 

單元測試代碼:

 

1 [TestMethod]
2         public void AddScoreConfigTest()
3         {
4             ScoreConfig config = new ScoreConfig();
5             config.ID = new ScoreConfigID() { MemberRate = 1, ScoreGetType = 1 };
6             config.Score = 20;
7             target.AddScoreConfig(config);
8         }

 獲取某個entity的代碼

 

代码
public int GetScoreConfigScore(int MemberRate, ScoreGetType type)
        {
            
using (ISession session = GetSession())
            {
                ScoreConfigID id 
= new ScoreConfigID();
                id.MemberRate 
= MemberRate;
                id.ScoreGetType 
= type;
                ScoreConfig config 
= session.Get<ScoreConfig>(id);
                
return config.Score;
            }
        }

 

單元測試代碼:

 

[TestMethod]
        
public void GetScoreConfigScoreTest()
        {
            
int i = target.GetScoreConfigScore(1, ScoreGetType.MemberLogin);
            Assert.AreEqual(
20, i);
        }

 

 

 

posted on 2010-01-12 16:29  不高很瘦  阅读(407)  评论(0编辑  收藏  举报