Many-to-many relationships are also very common in real-world applications. In this recipe, we will learn how to use Mapper to create such relationships. To do this, we will create a model for a school where we can have one student attending many subjects and one subject having many students.
You can use any of the projects we have used in the previous sections of this chapter. It is up to you whether to change an existing project or duplicate it.
To create a many-to-many relationship, carry out the following steps:
Create a class called
Student
in themodel
package with the following code:package code.model import net.liftweb.mapper._ class Student extends LongKeyedMapper[Student] with ManyToMany with IdPK { def getSingleton = Student object name extends MappedString(this, 50) object courses extends MappedManyToMany(CourseStudents, CourseStudents.student, CourseStudents.course, Course) } object Student extends Student...