views:

22

answers:

1

Hi everybody, I am using Castle ActiveRecord to mapping database.

I have a simple database like this: alt text And mapping code:

Group:

[ActiveRecord("[Group]")]
    public class Group : ActiveRecordBase
    {
        private long m_ID;
        private string m_GroupName;
        private string m_Description;
        private IList<Contact> m_Contacts = new List<Contact>();

        [PrimaryKey(Column = "`ID`", Generator = PrimaryKeyType.Identity)]
        public long ID
        {
            get { return m_ID; }
            set { m_ID = value; }
        }

        [Property(Column = "`GroupName`", NotNull = true)]
        public string GroupName
        {
            get { return m_GroupName; }
            set { m_GroupName = value; }
        }

        [Property(Column = "`Description`", NotNull = false)]
        public string Description
        {
            get { return m_Description; }
            set { m_Description = value; }
        }

        [HasAndBelongsToMany(typeof(Contact),
            Table = "`Contact_Group`",
            ColumnRef = "`ContactID`",
            ColumnKey = "`GroupID`")]
        public IList<Contact> Contacts
        {
            get { return m_Contacts; }
            set { this.m_Contacts = value; }
        }

        public static Group[] FindAll()
        {
            try
            {
                return (Group[])ActiveRecordBase.FindAll(typeof(Group));
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Loi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }

Contact:

[ActiveRecord("[Contact]")]
    public class Contact : ActiveRecordBase
    {
        private int m_ID;
        private string m_FirstName;
        private string m_LastName;
        private string m_Number;
        private string m_ExtraNumber;
        private string m_Sex;
        private DateTime m_DayOfBirth;
        private string m_Email;
        private string m_Address;
        private string m_Company;
        private string m_Nationality;
        private string m_Career;
        private string m_Picture;
        private string m_ExtraInfo;

        private IList<Group> m_Groups = new List<Group>();

        [PrimaryKey(Column = "`ID`",  Generator=PrimaryKeyType.Identity)]
        public int ID
        {
            get { return this.m_ID; }
            set { this.m_ID = value; }
        }

        [Property(Column = "`Number`", NotNull = true)]
        public string Number
        {
            get { return m_Number; }
            set { m_Number = value; }
        }

        [Property(Column = "`FirstName`", NotNull = true)]
        public string FirstName
        {
            get { return m_FirstName; }
            set { m_FirstName = value; }
        }

        [Property(Column = "`LastName`", NotNull = true)]
        public string LastName
        {
            get { return m_LastName; }
            set { m_LastName = value; }
        }

        [Property(Column = "`ExtraNumber`", NotNull = false)]
        public string ExtraNumber
        {
            get { return m_ExtraNumber; }
            set { m_ExtraNumber = value; }
        }

        [Property(Column = "Sex", NotNull = false)]
        public string Sex
        {
            get { return m_Sex; }
            set { m_Sex = value; }
        }

        [Property(Column = "`DayOfBirth`", NotNull = false)]
        public DateTime DayOfBirth
        {
            get { return m_DayOfBirth; }
            set { m_DayOfBirth = value; }
        }

        [Property(Column = "`Email`", NotNull = false)]
        public string Email
        {
            get { return m_Email; }
            set { m_Email = value; }
        }

        [Property(Column = "`Nationality`", NotNull = false)]
        public string Nationality
        {
            get { return m_Nationality; }
            set { m_Nationality = value; }
        }

        [Property(Column = "`Address`", NotNull = false)]
        public string Address
        {
            get { return m_Address; }
            set { m_Address = value; }
        }

        [Property(Column = "`Company`", NotNull = false)]
        public string Company
        {
            get { return m_Company; }
            set { m_Company = value; }
        }

        [Property(Column = "`Career`", NotNull = false)]
        public string Career
        {
            get { return m_Career; }
            set { m_Career = value; }
        }

        [Property(Column = "`Picture`", NotNull = false)]
        public string Picture
        {
            get { return m_Picture; }
            set { m_Picture = value; }
        }

        [Property(Column = "`ExtraInfo`", NotNull = false)]
        public string ExtraInfo
        {
            get { return m_ExtraInfo; }
            set { m_ExtraInfo = value; }
        }

        [HasAndBelongsToMany(typeof(Group),
            Table = "`Contact_Group`",
            ColumnRef = "`GroupID`",
            ColumnKey = "`ContactID`")]
        public IList<Group> Groups
        {
            get { return m_Groups; }
            set { this.m_Groups = value; }
        }

        public Contact()
        {

        }

        public Contact(string firstname, string lastname,  string number)
        {
            this.m_FirstName = firstname;
            this.m_LastName = lastname;
            this.m_Number = number;
        }

        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return true;
            }

            Contact key = obj as Contact;

            if (key == null)
            {
                return false;
            }
            if (this.m_Number == key.m_Number)
            {
                return true;
            }
            return false;
        }

        public override int GetHashCode()
        {
            return Convert.ToInt32(m_Number);
        }

        public static Contact[] FindAll()
        {
            try
            {
                return (Contact[])  ActiveRecordBase.FindAll(typeof(Contact));
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Loi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }

        public static Contact FindByPrimaryKey(string id)
        {
            try
            {
                return (Contact)ActiveRecordBase.FindByPrimaryKey(typeof(Contact), id, false);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Loi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }

        public static Contact[] FindByKeyWord(string keyword)
        {
            try
            {               
                SimpleQuery<Contact> q = new SimpleQuery<Contact>("from Contact c where c.FirstName like '%' || :key || '%' or c.LastName like '%' || :key || '%'");
                q.SetParameter("key", keyword);                
                return q.Execute();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Loi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }             

        public static bool Delete(string id)
        {
            Contact temp = (Contact)ActiveRecordBase.FindByPrimaryKey(typeof(Contact), id);
            try { ActiveRecordBase.Delete(temp); return true; }
            catch { return false; }
        }
    }

Contact_Group:

[Serializable]
    public class Contact_Group_Key
    {
        private string m_ContactID;
        private long m_GroupID;

        [KeyProperty(Column="`ContactID`")]
        public string ContactID
        {
            get { return m_ContactID; }
            set { m_ContactID = value; }
        }

        [KeyProperty(Column="`GroupID`")]
        public long GroupID
        {
            get { return m_GroupID; }
            set { m_GroupID = value; }
        }              

        public override int GetHashCode()
        { return this.m_GroupID.GetHashCode() ^ this.m_ContactID.GetHashCode(); }

        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return true;
            }

            Contact_Group_Key key = obj as Contact_Group_Key;

            if (key == null)
            {
                return false;
            }
            if (m_GroupID != key.m_GroupID || !m_ContactID.Equals(key.m_ContactID))
            {
                return false;
            }
            return true;
        }
    }

    [ActiveRecord("[Contact_Group]")]
    public class Contact_Group : ActiveRecordBase
    {        
        private Contact_Group_Key m_Key;
        private Contact m_Contact;
        private Group m_Group;       

        [CompositeKey]
        public Contact_Group_Key Key
        {
            get { return m_Key; }
            set { m_Key = value; }
        }

        [BelongsTo("`GroupID`")]
        public Group Group
        {
            set { this.m_Group = value; }
            get { return this.m_Group; }
        }

        [BelongsTo("`ContactID`")]
        public Contact Contact
        {
            set { this.m_Contact = value; }
            get { return this.m_Contact; }
        }
    }

Now I want to insert a new contact into Contact table as well as add a set of Contact_Group to Contact_Group table. This is my code:

public void Save()
        {
            try
            {
                if (this.view.CheckFields())
                {
                    // Add new contact 
                    if (this.contact == null)
                    {
                        Contact c = new Contact();
                        c.FirstName = this.view.GetFirstName();
                        c.LastName = this.view.GetLastName();
                        c.Number = this.view.GetNumber();
                        c.Sex = this.view.GetSex();
                        c.DayOfBirth = this.view.GetDayOfBirth();
                        c.Nationality = this.view.GetNationality();
                        c.ExtraNumber = this.view.GetExtraNumber();
                        c.Email = this.view.GetEmail();
                        c.Address = this.view.GetAddress();
                        c.Company = this.view.GetCompany();
                        c.Career = this.view.GetCareer();
                        c.ExtraInfo = this.view.GetExtraInfo();

                        // Check if the contact's number is valid
                        if (IsValidNumber(c.Number))
                        {                            
                            Group[] gs = this.view.GetGroupList();
                            foreach (Group g in groups)
                            {
                                c.Groups.Add(g);                           
                            }                         
                            c.CreateAndFlush();
                        }
                    }
                    else // Modify a contact
                    {

                    }
                }
            }
            catch (OleDbException e)
            {
                MessageBox.Show(e.Message, "Loi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

But after perform Save() it just insert a new Contact without the Contact_Group. Do I have any error in mapping or in Save() method? Plz help me. Thanks in advanced. P/S: I am newbie in NHibernate.

A: 

I tried this code, and it worked =.=

using (new SessionScope())
{
    Group[] gs = this.view.GetGroupList();
    foreach (Group g in groups)
    {
    contact.Groups.Add(g);
    }
    contact.Save();
}
Trần Quốc Bình