m3lter
2017-10-11 05:06:30 UTC
Hi,
I am having problems with mappings. One class has single value surrogate
key and the other has natural key with two columns. One way of association
is working. Could you help me to get other direction working too?
public class Product
{
public Product()
{
HashSetProductCategory = new HashSet<ProductCategory>();
}
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual string Category { get; set; }
public virtual bool Discontinued { get; set; }
public virtual ISet<ProductCategory> HashSetProductCategory { get;
set; }
}
[Serializable]
public class ProductCategoryId
{
public ProductCategoryId()
{
}
public ProductCategoryId(string property1, string property2)
{
Property1 = property1;
Property2 = property2;
}
public virtual string Property1 { get; set; }
public virtual string Property2 { get; set; }
public override bool Equals(object obj)
{
if (obj == null)
return false;
var t = (ProductCategoryId)obj;
if (t == null)
return false;
if (Property1 == t.Property1 && Property2 == t.Property2)
return true;
return false;
}
public override int GetHashCode()
{
return (Property1 + "|" + Property2).GetHashCode();
}
}
public class ProductCategory
{
public ProductCategory()
{
}
public ProductCategory(ProductCategoryId id)
{
Id = id;
}
public ProductCategory(string property1, string property2)
{
Id = new ProductCategoryId(property1, property2);
}
public virtual ProductCategoryId Id {
get
{
return new ProductCategoryId(Property1, Property2);
}
set
{
Property1 = value.Property1;
Property2 = value.Property2;
}
}
public virtual string Property1 { get; set; }
public virtual string Property2 { get; set; }
public virtual string Name { get; set; }
public virtual Product Product
{
get
{
return _product;
}
set
{
_product = value;
}
}
private Product _product;
}
<class name="Product">
<id name="Id">
<generator class="guid" />
</id>
<property name="Name" />
<property name="Category" />
<property name="Discontinued" />
<set name="HashSetProductCategory" inverse="true" lazy="true"
cascade="all">
<key>
<column name="Id" />
</key>
<one-to-many class="ProductCategory" not-found="ignore"/>
</set>
</class>
<class name="ProductCategory">
<composite-id name="Id" class="ProductCategoryId">
<key-property name="Property1" column="property1" />
<key-property name="Property2" column="property2" />
</composite-id>
<property name="Name" />
<!--<many-to-one name="Product" class="Product" column="product_id"
insert="false"
update="false"
not-null="true" />-->
</class>
Greetings
Tero M
I am having problems with mappings. One class has single value surrogate
key and the other has natural key with two columns. One way of association
is working. Could you help me to get other direction working too?
public class Product
{
public Product()
{
HashSetProductCategory = new HashSet<ProductCategory>();
}
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual string Category { get; set; }
public virtual bool Discontinued { get; set; }
public virtual ISet<ProductCategory> HashSetProductCategory { get;
set; }
}
[Serializable]
public class ProductCategoryId
{
public ProductCategoryId()
{
}
public ProductCategoryId(string property1, string property2)
{
Property1 = property1;
Property2 = property2;
}
public virtual string Property1 { get; set; }
public virtual string Property2 { get; set; }
public override bool Equals(object obj)
{
if (obj == null)
return false;
var t = (ProductCategoryId)obj;
if (t == null)
return false;
if (Property1 == t.Property1 && Property2 == t.Property2)
return true;
return false;
}
public override int GetHashCode()
{
return (Property1 + "|" + Property2).GetHashCode();
}
}
public class ProductCategory
{
public ProductCategory()
{
}
public ProductCategory(ProductCategoryId id)
{
Id = id;
}
public ProductCategory(string property1, string property2)
{
Id = new ProductCategoryId(property1, property2);
}
public virtual ProductCategoryId Id {
get
{
return new ProductCategoryId(Property1, Property2);
}
set
{
Property1 = value.Property1;
Property2 = value.Property2;
}
}
public virtual string Property1 { get; set; }
public virtual string Property2 { get; set; }
public virtual string Name { get; set; }
public virtual Product Product
{
get
{
return _product;
}
set
{
_product = value;
}
}
private Product _product;
}
<class name="Product">
<id name="Id">
<generator class="guid" />
</id>
<property name="Name" />
<property name="Category" />
<property name="Discontinued" />
<set name="HashSetProductCategory" inverse="true" lazy="true"
cascade="all">
<key>
<column name="Id" />
</key>
<one-to-many class="ProductCategory" not-found="ignore"/>
</set>
</class>
<class name="ProductCategory">
<composite-id name="Id" class="ProductCategoryId">
<key-property name="Property1" column="property1" />
<key-property name="Property2" column="property2" />
</composite-id>
<property name="Name" />
<!--<many-to-one name="Product" class="Product" column="product_id"
insert="false"
update="false"
not-null="true" />-->
</class>
Greetings
Tero M
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.