开发者

NHibernate: many-to-many mapping exception

I have a c# .net 3.5 application using NHibernate and MySQL.

My SQL tables are laid out in a many-to-many pattern linking configuration strings to tasks

CREATE TABLE `configstring` (
  `ConfigStringID` int(11) NOT NULL AUTO_INCREMENT,
  `ConfigString` varchar(45) NOT NULL,
  PRIMARY KEY (`ConfigStringID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

CREATE TABLE `task_configstring` (
  `ConfigStringID` int(11) NOT NULL,
  `TaskID` int(11) NOT NULL,
  PRIMARY KEY (`ConfigStringID`,`TaskID`),
  KEY `TaskID` (`TaskID`),
  KEY `ConfigStringID` (`ConfigStringID`),
  CONSTRAINT `ConfigStringID` FOREIGN KEY (`ConfigStringID`) REFERENCES `configstring` (`ConfigStringID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `TaskID` FOREIGN KEY (`TaskID`) REFERENCES `task` (`TaskID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1

CRE开发者_如何学GoATE TABLE `task` (
  `TaskID` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`TaskID`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=latin1

I have a Task class that contains a list of configuration strings like this:

public class Task
{
    public virtual IList<string> ConfigurationStrings { get; set; }
}

The NHibernate XML mapping for this class to the database looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="QueueTest"
                   namespace="Phoenix.Action">

    <class name="Task" table="queue_task">
        <id name="Id" column="TaskID" type="int" >
            <generator class="native"/>
        </id>

        <bag name="ConfigurationStrings" table="queue_task_configstring">
            <key column="TaskID"/>

            <many-to-many column="ConfigStringID"/>

        </bag>
    </class>

</hibernate-mapping>

But, when I call Configuration().BuildSessionFactory(), I get this exception: An association from the table task_configstring does not specify the referenced entity

What's the correct way to do a many-to-many mapping to a System.String object?


Do you have a class for the ConfigString ?

You're missing the class name in the many-to-many mapping.

Create a new ConfigString entity:

class ConfigStringEntity
{
   public virtual int ConfigStringID { get; set; }

   public virtual string ConfigString { get; set; }
}

<many-to-many class="ConfigStringEntity" column="ConfigStringID"/>

Your list will then be a list of ConfigStringEntity.

public class Task
{
    public virtual IList<ConfigStringEntity> ConfigurationStrings { get; set; }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜