2012-07-18 153 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属性。