1

I have a strange situation I need to persist:

public class Person
{
  public string[] Nicknames { get; set; }
}

What mapping and table structure would be best to persist this string array?

Michael Hedgpeth
  • 7,732
  • 10
  • 47
  • 66

2 Answers2

3

You can map arrays directly, but you'll need a few additional columns:

<!-- can live w/o orderby -->
<array name="Nicknames" table="Nicknames" order-by="indexColumn ASC"> 
  <key column="keyColumn"/>
  <index column="indexColumn"/> <!-- position in array -->
  <element column="nickname" type="String"/>
</array>

If you don't have the other columns you need, I would probably persist it as a normal bag or list and expose it as an array from that in the entity.

AlexCuse
  • 18,008
  • 5
  • 42
  • 51
  • I'm not familiar with the primitive-array mapping, does it save you from needing the index column? – AlexCuse Jan 23 '11 at 07:53
  • From this link: http://www.intertech.com/Blog/post/Hibernates-Primitive-Array.aspx, it looks like the difference between your example and the primitive-array is that it does not need an order-by attribute. – Michael Hedgpeth Jan 24 '11 at 14:10
  • Ah, the order by is optional for the array mapping. It would be nice to lose the index column but it seems workable. – AlexCuse Jan 24 '11 at 15:25
2

You can use some logic of yours to achieve the same thing right.. probably adding your own delimiter between the nicknames and then split it by this delimiter once you read it into memory.

public class Person
{
  public string Nicknames { private get; set; }

  public string[] ArrayOfNicknames
  {
    get
    {
        return Nicknames.Split(<your_delimiter>);
    }
  }
}
Baz1nga
  • 15,485
  • 3
  • 35
  • 61