NHibernate的关系

时间:2011-03-22 17:46:40

标签: c# nhibernate nhibernate-mapping

我是NHibernate世界的新手,所以请耐心等待。

我的表很简单

TB_Products
IdProduct

TB_Attributes
IdAttribute

TB_Attributes_Locale
IdAttribute
IdLanguage
AttributeValue

TB_Link_Products_Attributes
IdAttribute
IdProduct

TB_Products_Locale
IdProduct
IdLanguage
ProductName

我的课程是:

public class LocalizedStrings
{
    public string NameOrValue { get; set; }

    public LocalizedStrings() { }
}

public class Products
{
    private IList<Attributes> _productAttributes;
    private IDictionary<int, LocalizedStrings> _allLocalizedProduct;

    public virtual IDictionary<int, LocalizedStrings> AllLocalizedProduct
    {
        get { return _allLocalizedProduct; }
        set { _allLocalizedProduct = value; }
    }
    public virtual IList<Attributes> ProductAttributes
    {
        get { return _productAttributes; }
        set { _productAttributes = value; }
    }
    public Products() {}
}
public class Attributes
{
    private IDictionary<int, LocalizedStrings> _allLocalizedAttributes;

    public virtual IDictionary<int, LocalizedStrings> AllLocalizedAttributes 
    {
        get { return _allLocalizedAttributes; }
        set { _allLocalizedAttributes = value; }
    }

    public Attributes() { }
}

我的nhibernate映射:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Products" table="TB_Products">
      <id name="Id">
        <column name="IdProduct" not-null="true"/>
        <generator class="native" />
      </id>

      <bag name="ProductAttributes" table="TB_Link_Products_Attributes" cascade="all-delete-orphan" generic="true">
        <key column="IdProduct" />
        <many-to-many class="Attributes" column="IdAttribute" />
       </bag>
       <map name="AllLocalizedProduct" cascade="all-delete-orphan" table="TB_Products_Locale" lazy="false">
         <key column="IdProduct" />
         <index column="IdLanguage" type="System.Int32" />
         <composite-element class="LocalizedStrings">
           <property name="NameOrValue" column="ProductName" />
         </composite-element>
       </map>
   </class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="Attributes" table="TB_Attributes">
      <id name="Id">
         <column name="IdAttribute" not-null="true"/>
         <generator class="native" />
      </id>
      <map name="AllLocalizedAttributes" table="TB_Attributes_Locale" lazy="false">
         <key column="IdAttribute" />
         <index column="IdLanguage" type="System.Int32" />
         <composite-element class="LocalizedStrings">
           <property name="NameOrValue" column="AttributeValue" />
         </composite-element>
      </map>
   </class>
</hibernate-mapping>

好的,我在数据库中有什么:

TB_Products
IdProduct 
1
2
3

TB_Products_Locale
IdProduct   IdLanguage   ProductName
1           1            Bronze Statue
1           2            Statue en Bronze
2           1            Silver Statue
2           2            Statue en Argent
3           1            Wooden Statue

TB_Attributes
IdAttribute
11
12
13
14
15

TB_Attributes_Locale
IdAttribute  IdLanguage  AttributeName 
11            1           Randy Green
12            1           Thomas Martin
13            1           Bronze
13            2           Bronze in French
14            1           Silver
14            2           Argent (Silver in French) 
15            1           Wood

TB_Link_Products_Attributes
IdProduct    IdAttribute
1            11
2            12
1            13
2            14
3            11
3            15

在人类语言中,它将是:
青铜雕像(法语翻译)由兰迪格林雕刻而成,并以青铜制成(也用法语翻译)
银色雕像(法语翻译)由托马斯·马丁雕刻并用银制成(法语翻译)
木制雕像(未翻译成法语)由Randy Green雕刻并用Wood制作(未翻译成法语)

我想做的是:
1.如果我必须将Silver Statue的雕塑家从Thomas Martin改为Randy Green,我希望Thomas Martin也可以从TB_Attributes和TB_Attributes_Locale中删除,因为它变成孤儿,与任何其他产品无关。但当然,如果属性仍然与其他产品相关,请不要删除它 2.如果最后我们不想用法语翻译Silver(IdAttribute = 14),如果我从我的对象中删除了我的属性AllLocalizedAttributes中的法语翻译,它如何也可以从表TB_Attributes_Locale中自动删除?

如何仅在映射hibernate文件中进行更改?我尝试了几种选择,但无法使其正常工作。

提前致谢

0 个答案:

没有答案