Skip to content

Instantly share code, notes, and snippets.

@pjagielski
Created October 5, 2012 14:10
Show Gist options
  • Save pjagielski/3839991 to your computer and use it in GitHub Desktop.
Save pjagielski/3839991 to your computer and use it in GitHub Desktop.
Projections Grails
Link.withCriteria {
votes {
projections {
groupProperty("link")
sum("score")
}
}
}
Link.withCriteria {
createAlias('votes', 'v')
projections {
groupProperty('v.link')
sum('v.score', 'score')
}
order 'score', 'desc'
}
class LinkStatistics {
static belongsTo = [link: Link]
Integer score
Date dateCreated
static mapping = {
table("v_link_statistics")
link(fetch: FetchMode.EAGER)
version(false)
}
}
LinkStatistics.list(sort: 'score', order: 'desc')
LinkStatistics.withCriteria {
createAlias('link','l')
order('score', 'desc')
order('dateCreated', 'desc')
order('id', 'desc')
}
CREATE VIEW v_link_statistics AS
SELECT l.id, l.id as link_id, sum(nvl(v.score,0)), l.date_created
FROM link l LEFT JOIN vote v ON v.link_id = v.id
GROUP by l.id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment