'What is the best way to map one-to-one relationhip in NHibernate using NHibernate.Mapping.Attributes
I've been trying to map a one-to-one relationship using attributes in NHibernate and I am not getting anywhere. I would love some assistance in mapping it, also is there a different method of modeling this particular relationship here below.
I modeled the thing is such a way that its a one-to-one relationship where the Primary Key of the Parent is a Foreign Key in the child and also a PK.
I tried mapping this in NHibernate but I am not winning. Below is the Product Class which reflects the Product Model
[Class(Table = "product", Lazy = true)]
public class Product
{
[Required(ErrorMessage = "Product barcode is required!")]
[Id(0, Name = "Barcode", Column = "`barcode`")]
public virtual String Barcode { get; set; }
[Required(ErrorMessage = "Product's name is required!")]
[Property(Name = "ProductName", Column = "`name`")]
public virtual String ProductName { get; set; }
[ManyToOne(Name = "ProductBrand", Column = "`brand`", Cascade = "all", ClassType = typeof(Brand))]
public virtual Brand ProductBrand { get; set; }
[Required(ErrorMessage = "Product price is required!")]
[Property(Name = "ProductPrice", Column = "`price`")]
public virtual double ProductPrice { get; set; }
[Required(ErrorMessage = "Product's stock quantity is required!")]
[Property(Name = "StockQuantity", Column = "`stock`")]
public virtual Int32 StockQuantity { get; set; }
[Required(ErrorMessage = "Product tax is required!")]
[Property(Name = "ProductTax", Column = "`tax`")]
public virtual double ProductTax { get; set; }
[Required(ErrorMessage = "Product's category is required!")]
[OneToOne(Name = "ProductCategory", PropertyRef = "Product", ClassType = typeof(Category), Cascade = "all")]
public virtual Category ProductCategory { get; set; }
[Required(ErrorMessage = "Product's discount is required!")]
[Property(Name = "IsDiscounted", Column = "`discount_available`")]
public virtual bool IsDiscounted { get; set; }
[Property(Name = "DiscountAmount", Column = "`discount_amount`")]
public virtual double DiscountAmount { get; set; }
public Product()
{ }
public Product(string barcode, string productName, Brand productBrand, double productPrice, int stockQuantity, double productTax, Category productCategory, bool isDiscounted, double discountAmount)
{
Barcode = barcode;
ProductName = productName;
ProductBrand = productBrand;
ProductPrice = productPrice;
StockQuantity = stockQuantity;
ProductTax = productTax;
ProductCategory = productCategory;
IsDiscounted = isDiscounted;
DiscountAmount = discountAmount;
}
public Product(Product product)
{
Barcode = product.Barcode;
ProductName = product.ProductName;
ProductBrand = product.ProductBrand;
ProductPrice = product.ProductPrice;
StockQuantity = product.StockQuantity;
ProductTax = product.ProductTax;
ProductCategory = product.ProductCategory;
IsDiscounted = product.IsDiscounted;
DiscountAmount = product.DiscountAmount;
}
public Product(ProductViewModel product)
{
Barcode = product.Barcode ?? "";
ProductName = product.ProductName ?? "";
ProductBrand = new Brand(product.ProductBrand);
ProductPrice = product.ProductPrice;
StockQuantity = product.StockQuantity;
ProductTax = product.ProductTax;
ProductCategory = new Category(product.ProductCategory);
IsDiscounted = product.IsDiscounted;
DiscountAmount = product.DiscountAmount;
//ProductAddionaltInfo = new ProductAdditionalInformation(product.ProductAddionaltInfo);
}
}
And this class below reflects the ProductAdditionalInformation Model
[Class(Table = "productadditionalinformation", Lazy = true)]
public class ProductAdditionalInformation
{
//[Id(0, Name = "Product", Column = "`product`")]
[OneToOne(Name = "Product", Lazy = Laziness.Proxy, PropertyRef = "barcode", ForeignKey = "none", ClassType = typeof(Product))]
public virtual Product Product { get; set; }
[Property(Name = "ProductDesciption", Column = "product_desciption")]
public virtual String ProductDesciption { get; set; }
[Property(Name = "ProductWeight", Column = "product_weight")]
public virtual Decimal ProductWeight { get; set; }
[Property(Name = "ProductWidth", Column = "product_width")]
public virtual Decimal ProductWidth { get; set; }
[Property(Name = "ProductLength", Column = "product_length")]
public virtual Decimal ProductLength { get; set; }
[Property(Name = "ProductColour", Column = "product_colour")]
public virtual String ProductColour { get; set; }
[Property(Name = "ProductStyle", Column = "product_style")]
public virtual String ProductStyle { get; set; }
[Property(Name = "ConnectivityTechnology", Column = "connectivity_technology")]
public virtual String ConnectivityTechnology { get; set; }
[Property(Name = "Voltage", Column = "voltage")]
public virtual Decimal Voltage { get; set; }
[Property(Name = "Current", Column = "current")]
public virtual Decimal Current { get; set; }
[Property(Name = "PowerConsumption", Column = "power_consumption")]
public virtual Int32 PowerConsumption { get; set; }
[Property(Name = "CountryMade", Column = "country_made")]
public virtual String CountryMade { get; set; }
[Property(Name = "ModelNumber", Column = "model_number")]
public virtual String ModelNumber { get; set; }
[Property(Name = "Material", Column = "material")]
public virtual String Material { get; set; }
ProductAdditionalInformation()
{
}
public ProductAdditionalInformation(string productDesciption, decimal productWeight, decimal productWidth, decimal productLength, string productColour, string productStyle, string connectivityTechnology, decimal voltage, decimal current, int powerConsumption, string countryMade, string modelNumber, string material)
{
ProductDesciption = productDesciption;
ProductWeight = productWeight;
ProductWidth = productWidth;
ProductLength = productLength;
ProductColour = productColour;
ProductStyle = productStyle;
ConnectivityTechnology = connectivityTechnology;
Voltage = voltage;
Current = current;
PowerConsumption = powerConsumption;
CountryMade = countryMade;
ModelNumber = modelNumber;
Material = material;
}
public ProductAdditionalInformation(ProductAdditionalInformation productAdditionalInformation)
{
ProductDesciption = productAdditionalInformation.ProductDesciption;
ProductWeight = productAdditionalInformation.ProductWeight;
ProductWidth = productAdditionalInformation.ProductWidth;
ProductLength = productAdditionalInformation.ProductLength;
ProductColour = productAdditionalInformation.ProductColour;
ProductStyle = productAdditionalInformation.ProductStyle;
ConnectivityTechnology = productAdditionalInformation.ConnectivityTechnology;
Voltage = productAdditionalInformation.Voltage;
Current = productAdditionalInformation.Current;
PowerConsumption = productAdditionalInformation.PowerConsumption;
CountryMade = productAdditionalInformation.CountryMade;
ModelNumber = productAdditionalInformation.ModelNumber;
Material = productAdditionalInformation.Material;
}
public ProductAdditionalInformation(ProductAdditionalInformationViewModel product)
{
ProductDesciption = product.ProductDesciption ?? "";
ProductWeight = product.ProductWeight;
ProductWidth = product.ProductWidth;
ProductLength = product.ProductLength;
ProductColour = product.ProductColour ?? "";
ProductStyle = product.ProductStyle ?? "";
ConnectivityTechnology = product.ConnectivityTechnology ?? "";
Voltage = product.Voltage;
Current = product.Current;
PowerConsumption = product.PowerConsumption;
CountryMade = product.CountryMade ?? "";
ModelNumber = product.ModelNumber ?? "";
Material = product.Material ?? "";
}
}
At first I was getting an error which stated that one of the classes didn't have an ID of which it was correct, As you can see, I mapped it as a one-to-one which I am not sure if it's correct to begin with and how exactly do I make that The ID of the class since its a one-to-one mapping property as it is modeled like that???
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

