代码目的是要实现Course与Student的多对多。问题是多对多的两端插入正常,但中间表中的数据无法插入。小弟才疏学浅,还请各位指教。下面是一些代码:Course类 public class Course
{
private string id;
private string name;
private Iesi.Collections.ISet students = new Iesi.Collections.HashedSet(); public virtual string Id
{
get { return id; }
set { id = value; }
}
public virtual string Name
{
get { return name; }
set { name = value; }
}
public virtual Iesi.Collections.ISet Students
{
get { return students; }
set { students = value; }
}
}
Course.hbm.xml<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns = "urn:nhibernate-mapping-2.2" namespace = "NHibernateDemo" assembly = "NHibernateDemo">
<class name = "Course" table = "Course">
<id name = "Id">
<column name = "CourseId" sql-type = "char(32)" not-null = "true"/>
<generator class="uuid.hex"/>
</id>
<property name = "Name">
<column name = "Name" length = "16" not-null = "true"/>
</property>
<set name = "Students" cascade="all" inverse="true" lazy="false" table="Course_Student">
<key column="CourseId"/>
<many-to-many class="Student" column="StudentId"/>
</set>
</class>
</hibernate-mapping>
Student类 public class Student
{
private string id;
private string name;
private Iesi.Collections.ISet courses = new Iesi.Collections.HashedSet(); public virtual string Id
{
get { return id; }
set { id = value; }
}
public virtual string Name
{
get { return name; }
set { name = value; }
}
public virtual Iesi.Collections.ISet Courses
{
get { return courses; }
set { courses = value; }
}
}
Student.hbm.xml<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns = "urn:nhibernate-mapping-2.2" namespace = "NHibernateDemo" assembly = "NHibernateDemo">
<class name = "Student" table = "Student">
<id name = "Id">
<column name = "StudentId" sql-type = "char(32)" not-null = "true"/>
<generator class="uuid.hex"/>
</id>
<property name = "Name">
<column name = "Name" length = "16" not-null = "true"/>
</property>
<set name = "Courses" cascade="all" inverse="true" lazy="false" table="Course_Student">
<key column="StudentId"/>
<many-to-many class="Course" column="CourseId"/>
</set>
</class>
</hibernate-mapping>
写入数据库的代码 ISession session = NHibernateHelper.GetCurrentSession(); ITransaction tx = session.BeginTransaction(); Course courseOne = new Course();
courseOne.Name = "Math"; Course courseTwo = new Course();
courseTwo.Name = "Chinese"; Student studentOne = new Student();
studentOne.Name = "ZhangThree"; Student studentTwo = new Student();
studentTwo.Name = "LiFour"; studentOne.Courses.Add(courseOne);
studentOne.Courses.Add(courseTwo);
studentTwo.Courses.Add(courseTwo); courseOne.Students.Add(studentOne);
courseTwo.Students.Add(studentOne);
courseTwo.Students.Add(studentTwo); session.Save(courseOne);
session.Save(courseTwo);
session.Save(studentOne);
session.Save(studentTwo); tx.Commit(); NHibernateHelper.CloseSession();
{
private string id;
private string name;
private Iesi.Collections.ISet students = new Iesi.Collections.HashedSet(); public virtual string Id
{
get { return id; }
set { id = value; }
}
public virtual string Name
{
get { return name; }
set { name = value; }
}
public virtual Iesi.Collections.ISet Students
{
get { return students; }
set { students = value; }
}
}
Course.hbm.xml<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns = "urn:nhibernate-mapping-2.2" namespace = "NHibernateDemo" assembly = "NHibernateDemo">
<class name = "Course" table = "Course">
<id name = "Id">
<column name = "CourseId" sql-type = "char(32)" not-null = "true"/>
<generator class="uuid.hex"/>
</id>
<property name = "Name">
<column name = "Name" length = "16" not-null = "true"/>
</property>
<set name = "Students" cascade="all" inverse="true" lazy="false" table="Course_Student">
<key column="CourseId"/>
<many-to-many class="Student" column="StudentId"/>
</set>
</class>
</hibernate-mapping>
Student类 public class Student
{
private string id;
private string name;
private Iesi.Collections.ISet courses = new Iesi.Collections.HashedSet(); public virtual string Id
{
get { return id; }
set { id = value; }
}
public virtual string Name
{
get { return name; }
set { name = value; }
}
public virtual Iesi.Collections.ISet Courses
{
get { return courses; }
set { courses = value; }
}
}
Student.hbm.xml<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns = "urn:nhibernate-mapping-2.2" namespace = "NHibernateDemo" assembly = "NHibernateDemo">
<class name = "Student" table = "Student">
<id name = "Id">
<column name = "StudentId" sql-type = "char(32)" not-null = "true"/>
<generator class="uuid.hex"/>
</id>
<property name = "Name">
<column name = "Name" length = "16" not-null = "true"/>
</property>
<set name = "Courses" cascade="all" inverse="true" lazy="false" table="Course_Student">
<key column="StudentId"/>
<many-to-many class="Course" column="CourseId"/>
</set>
</class>
</hibernate-mapping>
写入数据库的代码 ISession session = NHibernateHelper.GetCurrentSession(); ITransaction tx = session.BeginTransaction(); Course courseOne = new Course();
courseOne.Name = "Math"; Course courseTwo = new Course();
courseTwo.Name = "Chinese"; Student studentOne = new Student();
studentOne.Name = "ZhangThree"; Student studentTwo = new Student();
studentTwo.Name = "LiFour"; studentOne.Courses.Add(courseOne);
studentOne.Courses.Add(courseTwo);
studentTwo.Courses.Add(courseTwo); courseOne.Students.Add(studentOne);
courseTwo.Students.Add(studentOne);
courseTwo.Students.Add(studentTwo); session.Save(courseOne);
session.Save(courseTwo);
session.Save(studentOne);
session.Save(studentTwo); tx.Commit(); NHibernateHelper.CloseSession();
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货