-->
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.  [ 4 posts ] 
Author Message
 Post subject: Save association automatically
PostPosted: Mon Apr 02, 2007 5:19 pm 
Newbie

Joined: Mon Apr 02, 2007 5:09 pm
Posts: 5
Hello there,

I have two objects associated.

First one, Category:

Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace byow.Objects
{
    public class Category
    {
        private int _categoryId = int.MinValue;
        private int _number = int.MinValue;
        private IList<CategoryLocale> _arrLocale = new List<CategoryLocale>();

        public virtual int CategoryId
        {
            get { return _categoryId; }
            set { _categoryId = value; }
        }

        public virtual int Number
        {
            get { return _number; }
            set { _number = value; }
        }

        public virtual IList<CategoryLocale> Locales
        {
            get { return _arrLocale; }
            set { _arrLocale = value; }
        }
    }
}


And then CategoryLocale:

Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace byow.Objects
{
    public class CategoryLocale
    {
        private int _categoryLocaleId = int.MinValue;
        private string _name = string.Empty;
        private Category _category = null;
        private LocLanguage _locLanguage = null;

        public virtual int CategoryLocaleId
        {
            get { return _categoryLocaleId; }
            set { _categoryLocaleId = value; }
        }

        public virtual string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public virtual Category Category
        {
            get { return _category; }
            set { _category = value; }
        }

        public virtual LocLanguage LocLanguage
        {
            get { return _locLanguage; }
            set { _locLanguage = value; }
        }
    }
}


Now the mapping files:

Category:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="byow.Objects" assembly="byow.Objects">
  <class name="Category" table="Categories">

    <id name="CategoryId" type="Int32" column="categoryId">
      <generator class="identity"/>
    </id>

    <property name="Number" column="number" type="System.Int32" />

    <bag name="Locales" table="CategoriesLocale">
      <key column="categoryId" />
      <one-to-many class="CategoryLocale" />
    </bag>

  </class>
</hibernate-mapping>


CategoryLocale:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="byow.Objects" assembly="byow.Objects">
  <class name="CategoryLocale" table="CategoriesLocale">

    <id name="CategoryLocaleId" type="Int32" column="categoryLocaleId">
      <generator class="identity"/>
    </id>

    <property name="Name" column="name" type="String" />   
    <many-to-one name="Category" column="categoryId" />
    <many-to-one name="LocLanguage" column="locLanguageId" />

  </class>
</hibernate-mapping>


If I do:

Code:
// create Locale
CategoryLocale cl = new CategoryLocale();
cl.Name = "brown";

// create and assign language to Locale
cl.LocLanguage = new LocLanguage();
cl.LocLanguage.LocLanguageId = 1;
cl.LocLanguage.CultureName = "en-CA";

// create Category
Category cat = new Category();
cat.Number = 222;

// add Locale to Category
cat.Locales.Add(cl);
cl.Category = cat;

// save Category
this.NHSession.SaveOrUpdate(cat);


It persists only the Category object in the database. The CategoryLocale object (which was created and added to the IList collection) is not saved in its table.

I need to explicitly call:

this.NHSession.SaveOrUpdate(cl);

Is that correct? Is there a better way to save everything (Category and its children CategoryLocale) with 1 step?

Thanks


Last edited by leodippolito on Tue Apr 03, 2007 3:57 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 03, 2007 3:49 am 
Newbie

Joined: Mon Apr 02, 2007 5:09 pm
Posts: 5
By the way,

This is the database schema:

Image


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 03, 2007 4:32 am 
Regular
Regular

Joined: Tue Aug 08, 2006 4:28 am
Posts: 96
Location: Hong Kong
The "cascade" attribute of <bag> must be set if you want NHibernate to automatically update associated objects.

BTW, "inverse" attribute may also be needed in <bag>. Please check.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 03, 2007 4:34 am 
Newbie

Joined: Mon Apr 02, 2007 5:09 pm
Posts: 5
Thanks, canton!

I'll take a look at these properties.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.