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: Remove item from array?
PostPosted: Tue Jul 17, 2007 8:52 am 
Newbie

Joined: Tue Feb 20, 2007 12:04 pm
Posts: 16
This seems like such a simple thing to accomplish, but I'm having a difficult time with the array index when I attempt to delete any single item within a mapped array.

Here's my mapping file:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
  <class name="com.brightharbour.schema.UserType, BH.Service" table="Users" lazy="false">
    <id name="UserId" column="UserId" type="String">
      <generator class="assigned" />
    </id>
    <property name="Password" type="String"/>
    <property name="Name" type="String"/>
    <property name="Email" type="String"/>
    <array name="SongPlaylists" table="SongPlaylists" cascade="all-delete-orphan">
      <key column="UserId"/>
      <index column="SongPlaylistsIndex" />
      <one-to-many class="com.brightharbour.schema.SongPlaylistInfoType, BH.Service"/>
    </array>
  </class>
</hibernate-mapping>


Now, when I attempt to delete any single item in the SongPlaylists array (session.Delete), my index gets messed up. I realize that using a list would be much easier, but I'm stuck with an array for reasons that aren't negotiable.

Any thoughts/ideas are appreciated -- feel like a dummy that I can't figure out how to get this working just right.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 19, 2007 7:57 am 
Newbie

Joined: Tue Feb 20, 2007 12:04 pm
Posts: 16
Okay, I have something that works, but I'm really wondering if there is a better way.

In essence, I just delete the desired item and shift everything down above that index.


Code:
bool found=false;
if (user.Items != null)
                    {
                        for (int m = 0; m < user.Items.Length; m++)
                        {
                            if (found) // found our record, so shift everything else down
                            {
                                user.Items[m - 1] = user.Items[m];
                                session.Save(user.Items[m - 1]);
                            }

                            if (user.Items[m].PlaylistId == oRequest.PlaylistId)
                            {
                                found = true;
                                x = m;
                                // found it, so this is the one that we need to remove
                                playlistId = user.Items[m].PlaylistId;
                                session.Delete(user.Items[m]);
                            }
                        }
                    }


Not pretty, but it works.


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.