2
votes

I have a table structure that looks similar to this, simplified for brevity

CREATE TABLE Parent (
  ParentId NUMBER NOT NULL,
  CONSTRAINT ParentId_PK PRIMARY KEY ParentId
);

CREATE TABLE Attribute (
  ParentId NUMBER NOT NULL,
  Key NVARCHAR2(200) NOT NULL,
  Value NVARCHAR2(4000) NOT NULL,
  CONSTRAINT Attribute_PK PRIMARY KEY (ParentId, Key),
  CONSTRAINT ParentId_FK FOREIGN KEY (ParentId)
      REFERENCES Parent (ParentId)
);

I would like to map the child key and value to a name value pair and store the list as a dictionary in the mapping to the parent. Where the code would look like this:

public class Parent
{
    public long Id { get; set; }
    public Dictionary<string, string> Attributes { get; set; }
}

It seems that this should be possible, but I can't seem to figure out the proper mapping to place in the HBM file.

1

1 Answers

3
votes

@Rippo - your comment pointed me to the solution, but I am posting the code here to show the solution per the guidelines.

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="com.so.answers" namespace="com.so.answers">
  <class name="Parent" table="Parent" lazy="false">
    <id name="Id" column="ParentId" />
    <map name="Attributes" table="Attribute">
      <key column="ParentId"/>
      <index column="Key" type="System.String"/>
      <element column="Value" type="System.String"/>
    </map>
  </class>
</hibernate-mapping>