I would like to know if there is a way to do some complex SQL queries using Mapper in Liftweb.
In fact what I would like to do is to perform a Join query from databases Employes and Departments using the fact that they are linked by a 1-to-many relationship.
Another example is also welcome.
Thanks in advance.
Here are some more details: Suppose I have 2 tables :
Employee : birthday, department ID, salary
Department : department ID, budget, address
Now I would like to obtain a list of the object Employee (created with Mapper) which have a salary > 10$ and a department budget < 100$.
Of course my original code is much more complicated than that but my objective is to be able to have a List of mapped objects (ie Employee) corresponding to criterions in its own table or on a linked table.
I’ve looked this up. It looks as though the joins are done in the object layer.
Extrapolated from http://exploring.liftweb.net/master/index-8.html to your case:
If you want to do many-to-many mappings you’ll need to provide your own
“join” class with foreign keys to both of your mapped entities.
Next, we define our join entity, as shown below.
It’s a LongKeyedMapper just like the rest of the entities,
but it only contains foreign key fields to the other entities.
To use the join entity, you’ll need to create a new instance and set the
appropriate foreign keys to point to the associated instances. As you can see,
we’ve defined a convenience method on our Expense meta object to do just that.
To make the many-to-many accessible as a field on our entities, we can use the
HasManyThrough trait, as shown below