2012-07-18 181 views
0

我有實體的房子,除了圖像的其他屬性列表。 每個圖像將作爲一個單獨的動作上傳,使用js裁剪技術逐個上傳。nhibernate映射,多對一

更新: 所以一個房子可以有很多圖像。

public House 
{ 
    public Guid Id {get; set;} 
    .... 
    List<Image> Images {get; set;} 
} 

public class Images 
{ 
    public House House {get; set;} 
    public string Path {get;set;} 
    public string Name {get;set;} 
    public string Description {get;set;}  
} 

我的數據庫表如下:

House 
Id 
Name, ... 
On this side I don't have relation to the Image table 

Image table 
Id 
HouseId 
Path 
Name 
Description 

是這種方法好不好? 如何使用nhibernate orm映射這些對象?

感謝

+0

在多對多的關係船上,你應該有另一張表,使你的房子和圖像表之間的連接,並將包含你的房子ID和圖像ID。所以這樣你的房子可以有很多圖像,並且圖像可以有很多房子(如果我正確地理解了你的問題)。 – Freeman 2012-07-18 08:40:17

+0

剛更新的問題。我認爲我需要多種方法。抱歉。 – Grunf 2012-07-18 08:44:25

回答

0

聲明您的實體,然後在映射文件中將圖像HouseIdHouse類的實體關聯。你應該在db中有外鍵。由於xml的答案已經在這裏,我會添加流利的nhibernate方式。

public class House 
{ 
    public virtual int Id { get; set; } 
    public virtual string Name { get; set; } 
    .... 
} 

public class Image 
{ 
    public virtual int Id { get; set; } 
    public virtual House HouseEntity { get; set; } 
    .... 
} 

public class HouseMap : ClassMap<House> 
{ 
    public HouseMap() 
    { 
     Table("House"); 

     Id(x => x.Id).GeneratedBy.Identity();    
     Map(x => x.Name); 
     ..... 
    } 
} 

public class ImageMap : ClassMap<Image> 
{ 
    public ImageMap() 
    { 
     Table("Image"); 

     Id(x => x.Id).GeneratedBy.Identity(); 
     References(x => x.House); 
     //References(x => x.House, "foreignKeyName"); There is also way to reference with specifying foreign key field 
     Map(x => x.Name); 
     .... 
    } 
} 
0

House.hbm.xml:

<bag name="Images" cascade="all-delete-orphan" inverse="true"> 
    <key column="HouseId" /> 
    <one-to-many class="Image" /> 
</bag> 

Image.hbm.xml:

<id type="int" column="Id"> 
    <generator ... /> 
</id> 

<many-to-one name="House" column="HouseId" cascade="none" /> 

既然你已經在圖像表的主鍵,並沒有標識屬性在你的域對象中,id映射應該沒有name屬性。