Squeryl does not have this form of ORM functionnality, as a design choice,
ORM functionnality (in Squeryl), don't go beyond mapping rows to objects and
basic CRUD and schema definition. Everything else if achieved via the DSL,
there is a lot of litterature about why ORMs suck, a big part of it is
the excess
of "magic", or lack of control of the generated SQL. The one good thing that
ORM has (besides allowing to work with objects rather than result sets/cursor)
is to allow code that is more DRY, or helps to apply the "Single
Source Of Truth"
principle. A DSL can provide these benefit almost as well, and with much less
magic (loss of control of the generated SQL).
For this specific case, I will assume that User and AuditInfo live in
two different table,
you will write a little bit more code than you would if you had
composition, but you will
never get a "suprise" join by fetching a User. In many cases it is
stiill possible to
centralize the join logic, you can define this only once :
def fetchUserWithAuditInfo(userName: String):
from(users, auditInfo)((u, ai) =>
where(u.userName === userName and u,id === ai.userId)
)
and since the return type is a Query[(User,AuditIndo)], it can be a
sub query of a more complex one.
If what you had in mind is a User and AuditInfo that live in the same
table, then, why not have a trait
AuditInfo extended by the User ? Then code that needs to deal with
just an AuditInfo can take a user
as argument. If you want a bit more syntactic sugar, have this method in user :
def auditInfo: AuditInfo = this
Does any of this make sense ?
2012/11/2 Joe Zulli <
j...@savings.com>:
> Hi everyone,
>
> I'm new to Squeryl, and I'm trying to figure out how to do composition
> classes. It seems that there is no way to do it but I might be wrong.
> Specifically, I want to use composition to store some auditing
> characteristics for my domain objects. Here's roughly what I'm doing:
>
> case class AuditInfo(var createUser: Long = -1, var createTime: Timestamp =
> new Timestamp(System.currentTimeMillis), var updateTime: Timestamp = new
> Timestamp(System.currentTimeMillis))
>
> trait Auditing {
>
> val auditInfo: AuditInfo
>
> }
>
> So I have some auditing code, then my domain classes look like this:
>
> case class User(val userName: String, val email: String, override val
> auditInfo: AuditInfo = new AuditInfo()) extends Auditing
>
> In this example, I want the createUser, createTime and updateTime fields to
> all map to respective columns in the USER table right along side userName
> and email. Is this possible to do in Squeryl? If so, can anyone point me in
> the right direction?
>
> If not, I am happy to take a shot at implementing it myself and submit back
> as a patch. If Max or anyone wants to give me some pointers to get me
> started (where in the code to look, etc...), it would be greatly
> appreciated.
>
> Thanks in advance!
>
> Joe