These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Problem with one-to-one mapping
PostPosted: Mon Sep 01, 2008 5:40 pm 
Newbie

Joined: Tue Apr 01, 2008 7:42 am
Posts: 2
I have a big but maybe simple problem with my web page. I don't seem to get one-to-one mapping to work. The ProductItem member in the Product object
is always null after I'm loading it from my database. I'm using Billy McCafferty's design described in this article:

http://www.codeproject.com/KB/architect ... tices.aspx.

I have probably made a very simple mistake but I can't seem to find out what I have been doing wrong. All the needed data seems to be saved in the database but when I load

object from the database it fails. If you need more info or code, I'll post it.

I would be very grateful if someone could help me with this.

Best regards

/Ingo

////////////////////////////////////////////////////////////////////////////////////
//
// Mapping
//
////////////////////////////////////////////////////////////////////////////////////
//
// Product.hbm.xml
//
////////////////////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="MyProject.Core.Product, MyProject.Core" table="Products">
<id name="ID" column="ProductID">
<generator class="identity" />
</id>


<one-to-one name="ProductItem"
class="MyProject.Core.ProductItem, MyProject.Core" />

</class>
</hibernate-mapping>

////////////////////////////////////////////////////////////////////////////////////
//
// ProductItem.hbm.xml
//
////////////////////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="MyProject.Core.ProductItem, MyProject.Core" table="ProductItems" lazy="false">
<id name="ID" column="ProductItemID">
<generator class="identity" />
</id>

<property name="Quantity" column="Quantity" />

<many-to-one name="Product"
class="MyProject.Core.Product, MyProject.Core"
column="ProductID"
cascade="all" />

</class>
</hibernate-mapping>


////////////////////////////////////////////////////////////////////////////////////
//
// Database tables
//
////////////////////////////////////////////////////////////////////////////////////
//
// Products table

//
////////////////////////////////////////////////////////////////////////////////////
CREATE TABLE [dbo].[Products](
[ProductID] [int] IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED

////////////////////////////////////////////////////////////////////////////////////
//
// ProductItems table
//
////////////////////////////////////////////////////////////////////////////////////
CREATE TABLE [dbo].[ProductItems](
[ProductItemID] [int] IDENTITY(1,1) NOT NULL,
[ProductID] [int] NOT NULL,
[Quantity] [int] NOT NULL,
CONSTRAINT [PK_ProductItems] PRIMARY KEY CLUSTERED

////////////////////////////////////////////////////////////////////////////////////
//
// Code
//
////////////////////////////////////////////////////////////////////////////////////
//
// Product.cs

//
////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using MyProject.Core.DataInterfaces;
using ProjectBase.Utils;

namespace MyProject.Core
{
public class Product : DomainObject<int>
{
#region Constructors

/// <summary>
/// Needed by ORM for reflective creation.
/// </summary>
protected Product() { }

#endregion

#region Properties

/// <summary>
/// Gets or sets the the product items associated to this product
/// </summary>
public virtual ProductItem Item
{
get { return _Item; }
set
{
_Item = value;
}
}

#endregion

#region Members

private ProductItem _Item;

#endregion
}
}

////////////////////////////////////////////////////////////////////////////////////
//
// ProjectItem.cs
//
////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using MyProject.Core.DataInterfaces;
using ProjectBase.Utils;

namespace MyProject.Core
{
public class ProductItem : DomainObject<int>
{
#region Constructors

/// <summary>
/// Needed by ORM for reflective creation.
/// </summary>
protected ProductItem() { }

#endregion

#region Properties

/// <summary>
/// Gets or sets the quantity of the products in this item
/// </summary>
public virtual int Quantity
{
get { return _Quantity; }
set
{
_Quantity = value;
}
}

/// <summary>
/// Gets or sets the ID for the product associated with this items entry
/// </summary>
public virtual Product Product
{
get { return _Product; }
set
{
_Product = value;
}
}

#endregion

#region Members

private int _Quantity;
private Product _Product;

#endregion
}
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.