Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8843977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:22:15+00:00 2026-06-14T11:22:15+00:00

The project I am working on has run into some undesirable Mapper behaviour. It

  • 0

The project I am working on has run into some undesirable Mapper behaviour. It seems you cannot add objects to many-to-many associations unless the objects are already saved in the database (more specifically, you cannot retrieve some not-saved objects from many-to-many associations).

As an example we have Employees and Departments in an M:N relationship. We instantiate 2 departments: Accounting and Security. Accounting gets employees before they are saved, Security gets employees after they are saved.

object Employee extends Employee with LongKeyedMetaMapper[Employee]
class Employee extends LongKeyedMapper[Employee] with IdPK {
  def getSingleton = Employee
  object name extends MappedString(this, 20)
  override def toString = name.get
}

object Department extends Department with LongKeyedMetaMapper[Department]
class Department extends LongKeyedMapper[Department] with ManyToMany with IdPK {
  def getSingleton = Department
  object employees extends MappedManyToMany(
    EmployeeDepartment,
    EmployeeDepartment.department,
    EmployeeDepartment.employee,
    Employee)
}

/* Many-to-Many association record */
object EmployeeDepartment extends EmployeeDepartment with LongKeyedMetaMapper[EmployeeDepartment]
class EmployeeDepartment extends LongKeyedMapper[EmployeeDepartment] with IdPK {
  def getSingleton = EmployeeDepartment

  object employee extends MappedLongForeignKey(this, Employee)
  object department extends MappedLongForeignKey(this, Department)
}

object Company extends App {
  DB.defineConnectionManager(DefaultConnectionIdentifier, myDBVendor)

  val accounting = Department.create
  val security = Department.create

  accounting.employees ++= Seq(
    Employee.create.name("Fred"),
    Employee.create.name("Steve"))

  security.employees ++= Seq(
    Employee.create.name("Dave"),
    Employee.create.name("Sonia")) map
    {_.saveMe()}

  accounting.employees.toList map println; println
  security.employees.toList map println
}

Output

Fred
Fred

Dave
Sonia

!!!

The employees of accounting have all turned into Fred! The problem scales similarly: adding 10 not-yet-saved entities to accounting.employees yields 10 references to Fred on access.

This appears to either be a bug, or a serious limitation to the usefulness of Mapper. Is there a workaround for this behaviour that does not involve either:

  • using some data structure external to the model to keep track of entities and associations until you are ready to save, or
  • saving entities on create, issuing deletes if the user “changes their mind” about saving
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:22:16+00:00Added an answer on June 14, 2026 at 11:22 am

    Found out the concrete reason for this apparent misbehaviour. Adding elements to a many-to-many association invokes the following algorithm:

    Find "other" element in "this" join list.
    if not found
      create join and add to join list
    else
      add to join list
    

    The rub is in the find criteria. Specified in ManyToMany.scala (Lift 2.4), method isJoinForChild compares the is of the association foreign key (MappedManyToMany constructor parameter 3) with the primary key (also is) of the “other” element.

    As every non-saved entity with IdPK has id=-1, the first element in the “this” join list satisfies the find criteria and is then added in duplicate.

    This answers my original question: it is possible to fix this behaviour by modifying isJoinForChild. Rather than simply comparing keys, the method would do something akin to:

    if other.is > -1
      regular comparison
    else
      instance comparison
    

    … or something to that effect. Since isJoinForChild is protected this can be accomplished non-invasively by overriding isJoinForChild using a trait.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a project that already has begun being developed. ON some
I was working on a project and ran into some issues which i dont
I'm working on a software project for Emacs that has some tests that can
I'm currently working on my first major project in clojure and have run into
A project I was working on has finished, so I've been moved on to
The project I am working on has a rich text box that can have
The project has been working fine in 2003 but when opening it in 2008
I've a project that I'm working on which has not been started by me.
Can anyone help me find an up-to-date, working ATL project which has a main
Im working on a project that has an implementation of JOSSO in place. We

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.