-->
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: Persist more than one collection in a class
PostPosted: Wed Oct 18, 2006 8:13 am 
Newbie

Joined: Tue Oct 17, 2006 1:59 pm
Posts: 2
Hello everybody,

i've got the following problem, in my class i have two IList collections. If i persist the class, with the help of the below listed mapping file, the data is stored like in the following table:
Code:
ID     lfd_nr        mylist            mylist1
===============================================
17       0           firststring       <NULL>
17       1           secondstring      <NULL>       
17       0           <NULL>            ShouldBeInRowLikeFirstString
17       0           <NULL>            ShouldBeInRowLikeSecondString       


How do i have to adapt the mapping file, so that the data is stored like follows?
Code:
ID       lfd_nr        mylist           mylist1
===============================================
17       0             firststring      ShouldBeInRowLikeFirstString
17       1             secondstring     ShouldBeInRowLikeSecondString       


Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
    namespace="testnamespace.Classes" assembly="WindowsApplication2">

  <class name="Class1" table="TestTable">

    <id name="Id" column="ID" type="Int32">
      <generator class="native"/>
    </id>

    <property name="Description"        column="TestString"        type="String"/>
   
    <list name ="MyList" lazy="true" table="TestTable1">
      <key column="ID"/>
      <index column="lfd_nr"/>
      <element type ="string" column="mylist" not-null="false"/>
    </list>

    <list name ="MyList1" lazy="true" table="TestTable1">
      <key column="ID"/>
      <index column="lfd_nr"/>
      <element type ="string" column="mylist1" not-null="false"/>
    </list>

  </class>

</hibernate-mapping>


Class to persist

Code:
using System.Collections;

namespace testnamespace.Classes
{
    public class Class1
    {

        private string description;
        private IList myList;
        private IList myList1;
        private int id;

        public Class1()
        {
            this.description = "TestDesciption";
            this.myList = new ArrayList();
            this.myList1 = new ArrayList();
           
        }

        public string Description
        {
            get { return description; }
            set { description = value; }
        }
       
        public IList MyList
        {
            get { return myList; }
            set { myList = value; }
        }

        public IList MyList1
        {
            get { return myList1; }
            set { myList1 = value; }
        }

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


thx in advance
a NHibernate Newbie


Top
 Profile  
 
 Post subject: Re: Persist more than one collection in a class
PostPosted: Wed Oct 18, 2006 3:35 pm 
Beginner
Beginner

Joined: Wed Aug 03, 2005 8:06 am
Posts: 40
Location: Netherlands
Hi,

I don't think this can be done with NHibernate. If at all... I doubt it. What SQL would be required to store e.g. mylist1? If it has the same number of elements as the previously stored mylist1 a number of update statements should be executed. However, if mylist1 is longer additional inserts are necessary, what an overhead to store a simple list! Besides, this would make mylist contain a number of null elements, which I think is not desirable.

So I would just store the lists in two separate tables, unless there is some compelling reason to do it your way (or I'm overlooking something...).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 19, 2006 1:55 am 
Newbie

Joined: Tue Oct 17, 2006 1:59 pm
Posts: 2
Hi,

thx for your answer, but i've got one more question assumed I have lets say 10 IList Collections in my class, I would need ten different tables, to store one Object? Isn' t that a real overhead?

thx
NHibernateNewbie


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 19, 2006 3:11 am 
Beginner
Beginner

Joined: Wed Aug 03, 2005 8:06 am
Posts: 40
Location: Netherlands
NHibernateNewbie wrote:
Hi,

thx for your answer, but i've got one more question assumed I have lets say 10 IList Collections in my class, I would need ten different tables, to store one Object? Isn' t that a real overhead?

thx
NHibernateNewbie


Of course you can always find a way to store your lists in one table, a column per list or one column for all lists and an additional list identifier. The first is hard to implement with standard NHibernate mappings, the second would be easier, e.g. by subclassing your list elements and using a table-per-class hierarchy strategy, or using a composite-id. Maybe you should add a level in your model, a list class, having a values collection. Your Class1 should have a list of lists then.


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.