-->
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.  [ 2 posts ] 
Author Message
 Post subject: Simple Parent Child example needed
PostPosted: Thu Feb 15, 2007 12:58 pm 
Newbie

Joined: Thu Feb 15, 2007 12:48 pm
Posts: 11
Hi,
I am a newbie to both Hibernate and .NET. However, in the past few days I have spent so much time looking through forums to learn and I am getting there. With all the tutorials, I am still not able to get a simple parent child relationship work. I have attached all the necessary files and here are the C$ files:

Template.cs:
Code:
public class Template
    {
        private int id;
        private string name;
       // private DateTime modified;
        private IList templateValues;

        public Template()
        {
        }
        public virtual int ID
        {
            set { id = value; }
            get { return id; }
        }

        public virtual string TemplateName
        {
            set { name = value; }
            get { return name; }
        }

        /**
        public virtual DateTime Modified
        {
            set { modified = value; }
            get { return modified; }
        }
        */

        public virtual IList TemplateValues
        {
            set { templateValues = value; }
            get { return templateValues; }
        }

        public void AddTemplateValue(TemplateValue value)
        {
            if (TemplateValues.Contains(value))
                return;
            value.Parent = this;
            TemplateValues.Add(value);
        }

        public void RemoveTemplateValue(TemplateValue value)
        {
            if (templateValues.Contains(value))
            {
                templateValues.Remove(value);
            }
            Db.Delete(value);
        }
    }


TemplateValue.cs
Code:
public class TemplateValue
    {
        private int id;
        private string type;
        private string value;
        private Template parent;

        public TemplateValue()
        {
        }

        public TemplateValue(string type, string value)
        {
            this.type = type;
            this.value = value;
        }

        public int ID
        {
            set { id = value; }
            get { return id; }
        }

        public string Type
        {
            set { type = value; }
            get { return type; }
        }

        public string Value
        {
            set { this.value = value; }
            get { return this.value; }
        }

        public override bool Equals(Object obj)
        {
            if (obj == null) return false;

            if (this.GetType() != obj.GetType()) return false;

            // safe because of the GetType check
            TemplateValue templateVal = (TemplateValue)obj;

            if ((Type.Equals(templateVal.Type)) && (Value.Equals(templateVal.Value)) )return true;

            return false;
        }

        public Template Parent
        {
            set { parent = value; }
            get { return parent; }
        }

        public override int GetHashCode()
        {
            string stringRep = this.ToString();
            return stringRep.GetHashCode();
        }

   
    }



Okay after having done all these mappings and code, I tried to test the database tables:
I was able to just add a new Parent row but however was not able to add child to existing parent table

Template template = (Template) Session.Load(typeof(Template), 5);
template.AddTemplateValue(new TemplateValue('location', 'chicago');
Session.Save(template).

The code above doesnt work and no exception happens.

Any help or suggestions please??

Thanks for your help in advance.

Javid
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
NHibernate 1.0.4.0
Mapping documents:
Template.hbm.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="HaynesPrototype" assembly="HaynesPrototype">
<class name="HaynesPrototype.Template" table="SCENARIO_TEMPLATES">
<id name="ID" column="ID" type="integer" length="50">
<generator class="identity" />
</id>
<property name="TemplateName" column="TEMPLATE_NAME" type="string" length="40" />
<bag name="TemplateValues" access="property" inverse="true" lazy="true" cascade="all-delete-orphan">
<key column="TEMPLATE_ID" />
<one-to-many class="TemplateValue" />
</bag>

</class>
</hibernate-mapping>

TemplateValue.hbm.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="HaynesPrototype" assembly="HaynesPrototype">
<class name="HaynesPrototype.TemplateValue" table="SCENARIO_TEMPLATE_VALUES">
<composite-id>
<key-many-to-one name="Parent" class="Template">
<column name="TEMPLATE_ID" not-null="true"/>
</key-many-to-one>
<key-property name="Type" column="TYPE" type="string" />
<key-property name="Value" column="VALUE" type="string" />
</composite-id>
<many-to-one name="Parent" column="TEMPLATE_ID" class="Template" cascade="none" insert="false" update="false" not-null="true"/>
</class>

</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:
MS SQL Server 2005
The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 16, 2007 1:13 am 
Beginner
Beginner

Joined: Mon Jan 08, 2007 11:59 pm
Posts: 31
In addition to adding the TemplateValue to the collection in the Template, you will also have to set the parent in the TemplateValue to the Template.

In short you have to set both the ends of the relation for this to work.

Job Samuel


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