-->
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: One to many; many to one
PostPosted: Fri Jun 02, 2006 3:04 am 
Beginner
Beginner

Joined: Tue May 23, 2006 12:53 am
Posts: 34
I've been doing this for about an hour now but still can't figure out what's wrong... I have an Article having many Comments. So, this is my code.
Code:
/*
* Article.cs 0.1
*
* Copyright 2006 Ian Escarro. All rights reserved.
* PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

using System;
using System.Drawing;
using System.Collections;

namespace Agta.ManilaBulletin.Data
{
   /// <summary>
   ///
   /// </summary>
   public class Article : Observable
   {
      private int id;
      private DateTime date;
      private string keyword;
      private bool isTopStory;
//      private string body;
      private Teaser teaser;
      private Body body;
      private Image image;
      private string caption;
      private bool isInactive;
      private Section section;
      private ArrayList comments = new ArrayList();

      public Article(int id, DateTime date, string keyword,
         bool isTopStory, Body body, Image image,
         string caption, bool isInactive, Section section)
      {
         this.Id = id;
         this.Date = date;
         this.Keyword = keyword;
         this.IsTopStory = isTopStory;
         this.Body = body;
         this.Image = image;
         this.Caption = caption;
         this.IsInactive = isInactive;
         this.Section = section;
      }

      public Article(int id) : this (id, DateTime.Now, "", false, new Body(), new Bitmap(1, 1), "", false, new Section()) {}

      /// <summary>
      /// Creates a new instance of Article.
      /// </summary>
      public Article() : this (0) {}

      public int Id
      {
         get { return id; }
         set { id = value; this.NotifyObservers(); }
      }

      public DateTime Date
      {
         get { return date; }
         set { date = value; this.NotifyObservers(); }
      }

      public string Keyword
      {
         get { return keyword; }
         set { keyword = value; this.NotifyObservers(); }
      }

      public bool IsTopStory
      {
         get { return isTopStory; }
         set { isTopStory = value; this.NotifyObservers(); }
      }

//      public string Body
//      {
//         get { return body; }
//         set { body = value; this.NotifyObservers(); }
//      }

      public Body Body
      {
         get { return body; }
         set
         {
            body = value;
            this.teaser = body;
            this.NotifyObservers();
         }
      }

      public Image Image
      {
         get { return image; }
         set { image = value; this.NotifyObservers(); }
      }

      public string Caption
      {
         get { return caption; }
         set { caption = value; this.NotifyObservers(); }
      }

      public bool IsInactive
      {
         get { return isInactive; }
         set { isInactive = value; this.NotifyObservers(); }
      }

      public Section Section
      {
         get { return section; }
         set { section = value; this.NotifyObservers(); }
      }

      public ArrayList Comments
      {
         get { return comments; }
         set { comments = value; this.NotifyObservers(); }
      }

      public void AddComment(Comment comment)
      {
         if (comment == null)
            throw new NullReferenceException();
         if (!comments.Contains(comment))
         {
            comment.Article = this;
            comments.Add(comment);
         }
      }

      public void RemoveComment(Comment comment)
      {
         comments.Remove(comment);
      }

      public int CountComments()
      {
         return comments.Count;
      }
   }
}
and my Comment
Code:
/*
* Comment.cs 0.1
*
* Copyright 2006 Ian Escarro. All rights reserved.
* PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

using System;

namespace Agta.ManilaBulletin.Data
{
   /// <summary>
   ///
   /// </summary>
   public class Comment
   {
      private Article article;
      private int number;
      private string sender;
      private string email;
      private string subject;
      private string message;
      private bool isHidden;

      public Comment(Article article, int number, string sender,
         string email, string subject, string message,
         bool isHidden)
      {
         this.Article = article;
         this.Number = number;
         this.Sender = sender;
         this.Email = email;
         this.Subject = subject;
         this.Message = message;
         this.IsHidden = isHidden;
      }

      public Comment(Article article, string sender, string email,
         string subject, string message) : this (article, 0,
         sender, email, subject, message, true) {}

      public Comment(string sender, string email, string subject,
         string message) : this (null, sender, email,
         subject, message) {}
      
      /// <summary>
      /// Creates a new instance of Comment.
      /// </summary>
      public Comment() : this ("", "", "", "") {}

      public Article Article
      {
         get { return article; }
         set { article = value; }
      }

      public int Number
      {
         get { return number; }
         set { number = value; }
      }

      public string Sender
      {
         get { return sender; }
         set { sender = value; }
      }

      public string Email
      {
         get { return email; }
         set { email = value; }
      }

      public string Subject
      {
         get { return subject; }
         set { subject = value; }
      }

      public string Message
      {
         get { return message; }
         set { message = value; }
      }

      public bool IsHidden
      {
         get { return isHidden; }
         set { isHidden = value; }
      }
   }
}
Now for the mapping
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="Agta.ManilaBulletin.Data" assembly="webartiData">
   <class name="Article" table="Articles">
      <id name="Id" column="ArticleId" type="Int32">
         <generator class="native"/>
      </id>
      <property name="Date" column="Date" type="DateTime"/>
      <property name="Keyword" column="Keyword" type="String"/>
      <property name="IsTopStory" column="IsTopStory" type="Boolean"/>
      <!-- <property name="Body.Text" column="Body" type="String"/>-->
      <!--<property name="Image" column="Image" type="Boolean"/>-->
      <property name="Caption" column="Caption" type="String"/>
      <property name="IsInactive" column="IsInactive" type="Boolean"/>
      <!--<property name="Section" column="SectionId" type="Int32"/>-->
      <set name="Comments" inverse="true">
         <key column="ArticleId"/>
         <one-to-many class="Comment"/>
      </set>
      <!--<subclass name="Body">
         <property name="Text" column="Body" type="String"/>
      </subclass>-->
   </class>
</hibernate-mapping>
and
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="Agta.ManilaBulletin.Data" assembly="webartiData">
   <class name="Comment" table="ArticleComments">
      <id name="Number" column="CommentNumber" type="Int32">
         <generator class="identity"/>
      </id>
      <property name="Sender" column="Sender" type="String"/>
      <property name="Email" column="Email" type="String"/>      
      <property name="Subject" column="Subject" type="String"/>      
      <property name="Message" column="Message" type="String"/>      
      <property name="IsHidden" column="IsHidden" type="Boolean"/>
      <many-to-one name="Article" column="ArticleId" not-null="true"/>
   </class>
</hibernate-mapping>
Help please? Thanks a bunch.

When tested btw, it says
Code:
TestCase 'Agta.ManilaBulletin.Test.ObservableFactoryTest.TestCreate'
failed: NHibernate.ADOException : Could not save object
  ----> System.InvalidCastException : Specified cast is not valid.
   at NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything)
   at NHibernate.Impl.SessionImpl.Save(Object obj)
   D:\ian\projects\csharp\webarti\webartiData\Data\ObservableFactory.cs(47,0): at Agta.ManilaBulletin.Data.ObservableFactory.Create()
   d:\ian\projects\csharp\webarti\webartidata\test\observablefactorytest.cs(61,0): at Agta.ManilaBulletin.Test.ObservableFactoryTest.TestCreate()
   --ADOException
   at NHibernate.Type.SetType.Wrap(ISessionImplementor session, Object collection)
   at NHibernate.Impl.WrapVisitor.ProcessArrayOrNewCollection(Object collection, PersistentCollectionType collectionType)
   at NHibernate.Impl.WrapVisitor.ProcessCollection(Object collection, PersistentCollectionType collectionType)
   at NHibernate.Impl.AbstractVisitor.ProcessValue(Object value, IType type)
   at NHibernate.Impl.WrapVisitor.ProcessValues(Object[] values, IType[] types)
   at NHibernate.Impl.SessionImpl.DoSave(Object theObj, Key key, IClassPersister persister, Boolean replicate, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)
   at NHibernate.Impl.SessionImpl.DoSave(Object obj, Object id, IClassPersister persister, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)
   at NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything)
I can't figure out of me what's wrong with this stuff.... :(


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 11:21 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
if you're creating an association using <set> you need to specifiy Iesi.Collections.ISet as the type in your class file and include a reference to Iesi.Collections in your project.

here is a helpful tip:

http://www.hibernate.org/359.html#A8


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.