-->
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.  [ 3 posts ] 
Author Message
 Post subject: IDictionary Mapping
PostPosted: Wed Oct 03, 2007 5:13 pm 
Newbie

Joined: Wed May 02, 2007 9:32 am
Posts: 17
Hi
I was wondering if someone could lend a quick hand with this. I have an IDictionary<string, int> that I am having problems persisting (the other parts save ok). I have the following class and mapping files defined. Is there anything special I need to do for an IDictionary?

Thanks for any help.
Joe

Code:

    public class ParkedBasket
    {
        private int _ParkedBasketId;
        private Guid _UserId;
        private string _BasketName;
        private DateTime _SaveDate;
        private IDictionary<string, int> _LineItems = new Dictionary<string, int>();

        public int ParkedBasketId
        {
            get { return _ParkedBasketId; }
            set { _ParkedBasketId = value; }
        }

        public Guid UserId
        {
            get { return _UserId; }
            set { _UserId = value; }
        }

        public string BasketName
        {
            get { return _BasketName; }
            set { _BasketName = value; }
        }

        public DateTime SaveDate
        {
            get { return _SaveDate; }
            set { _SaveDate = value; }
        }

        public IDictionary<string, int> LineItems
        {
            get { return _LineItems; }
            set { _LineItems = value; }
        }

        public ParkedBasket() { }
 
    }



And the following XML mapping file

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Orders" assembly="Orders" >
  <class name="ParkedBasket" table="ParkedBasket" lazy="false">
    <id name="ParkedBasketId" column="ParkedBasketId" unsaved-value="0">
      <generator class="native" />
    </id>

    <property name="UserId" />
    <property name="BasketName" />
    <property name="SaveDate"  />

    <map name="LineItems" generic="true" table="_ParkedBasketItems" cascade="all"  collection-type="IDictionary" >
      <key column="ParkedBasketId" foreign-key="ParkedBasketId"  />
      <index column="CatalogNumber" type="string" />
      <element type="int" column="Quantity" />
    </map>

  </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 04, 2007 1:54 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
1. You don't need the 'generic' attribute at all (FYI)
2. You don't need to specify the IDictionary collection type...NHibernate knows
3. try changing your .NET types in your mapping to their formal names like this:
Code:
    <map name="LineItems" table="_ParkedBasketItems" cascade="all"  >
      <key column="ParkedBasketId" foreign-key="ParkedBasketId"  />
      <index column="CatalogNumber" type="System.String" />
      <element type="System.Int32" column="Quantity" />
    </map>


Cheers
MIKE

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 04, 2007 8:20 am 
Newbie

Joined: Wed May 02, 2007 9:32 am
Posts: 17
Thanks, Mike

Along with your suggestions, I also had make a call to CommitChanges.



Code:
        [TestMethod]
        public void TestCanParkBasket()
        {
            Core.DataInterfaces.IGenericDao<ParkedBasket, int> dal = new NHibernateDaoFactory(TestGlobals.SessionFactoryConfigPath).GetGenericDao<ParkedBasket, int>();

            ParkedBasket basket = new ParkedBasket();
            basket.BasketName = DateTime.Now.Ticks.ToString();
            basket.SaveDate = DateTime.Now;
            basket.UserId = Guid.NewGuid();
            basket.LineItems.Add("LineItem1", 1);
            basket.LineItems.Add("LineItem2", 2);
            basket.LineItems.Add("LineItem3", 3);
            basket.LineItems.Add("LineItem4", 4);
            basket.LineItems.Add("LineItem5", 5);

            dal.Save(basket);
            dal.CommitChanges();
           
        }


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

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.