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.  [ 1 post ] 
Author Message
 Post subject: Nested <map> collection
PostPosted: Wed Apr 09, 2008 9:02 pm 
Newbie

Joined: Wed Apr 09, 2008 8:17 pm
Posts: 2
I have a class Image that has an IDictionary<string, string> property named Details mapped using the <map>

element. I can retrieve the values, add new values.

However, now I'm required to add in support for multiple values per key, in essence going from IDictionary<string, string> to IDictionary<string, IList<string>> for the Details property. However, I'm at a loss as to how to translate this into the NHibernate mapping files, and much less how to query by it using NHibernate.


Hibernate version: 1.2.0
Code files
Code:
public class Image
{
    // Omitted other class details..
    private IDictionary<string, IList<string>> _details;
    public virtual IDictionary<string, IList<string>> Details
    {
        get { return this._details; }
        protected set { this._details = value; }
    }
}


Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="MyAssembly.Domain.Image, MyAssembly.Domain" table="Image">
    <id name="Id" column="Id" type="String" length="12" access="nosetter.camelcase-underscore" unsaved-value="null">
      <generator class="assigned" />
    </id>
    <property name="Name" column="Name" type="String" length="23" not-null="true"/>
    <property name="Number" column="Number" type="String" length="5" not-null="true"/>
    <property name="ImageDate" column="ImageDate" type="DateTime" not-null="true"/>
    <property name="LoadedDate" column="LoadedDate" type="DateTime" not-null="false" />
    <property name="Path" column="Path" type="String" length="500" not-null="true" />
    <many-to-one name="ImageType" column="ImageTypeId" class="MyAssembly.Domain.ImageType, MyAssembly.Domain" fetch="join"

not-null="true"/>
    <map name="Details" table="ImageDetail" fetch="join" lazy="false">
      <key column="ImageId"></key>
      <index column="DetailType" type="String" length="10"></index>
      <!-- Don't know what to put here -->
    </map>
  </class>
</hibernate-mapping>



The database tables are:
Code:
/****** Object:  Table [dbo].[Image]    Script Date: 04/09/2008 18:24:59 ******/
CREATE TABLE [dbo].[Image](
   [Id] [nchar](12) NOT NULL PRIMARY KEY,
   [Name] [nvarchar](23) NOT NULL,
   [Number] [nvarchar](5) NOT NULL,
   [ImageTypeId] [int] NOT NULL,
   [ImageDate] [datetime] NOT NULL,
   [Path] [nvarchar](500) NOT NULL,
   [LoadedDate] [datetime] NULL,
   [LoadedBy] [uniqueidentifier] NULL
) ON [PRIMARY]

/****** Object:  Table [dbo].[ImageDetail]    Script Date: 04/09/2008 18:42:02 ******/
CREATE TABLE [dbo].[ImageDetail](
   [ImageId] [nchar](12) NOT NULL,
   [DetailType] [nvarchar](10) NOT NULL,
   [DetailIndex] [int] NOT NULL,
   [Value] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_ImageDetail_1] PRIMARY KEY CLUSTERED
(
   [ImageId] ASC,
   [DetailType] ASC,
   [DetailIndex] ASC
) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[ImageDetail]  WITH CHECK ADD  CONSTRAINT [FK_ImageDetail_Image] FOREIGN KEY([ImageId])
REFERENCES [dbo].[Image] ([Id])
GO
ALTER TABLE [dbo].[ImageDetail] CHECK CONSTRAINT [FK_ImageDetail_Image]


So an example of what I'd like to do is say I have the following code:

Code:
ISession session = NHibernateHelper.GetCurrentSession();
// Add/Update details to an image
string id = "000061121C0 ";
Image img = session.Get<Image>(id);
string key = "Author";
img.Details[key].Add("Andrew");
img.Details[key].Add("Steven");
session.SaveOrUpdate(img);
session.Flush();

// Now I'd like to fetch all images with the value: "Andrew"
// under the "Author" key.
ICriteria crit = session.CreateCriteria(typeof(Image));
// TODO add filtering code here
IList<Image> imageList = crit.List<Image>();
// imageList should contain all images with "Andrew" as a value
// under the "Author" key.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.