Grails,左外连接
我有两个域类用户和项目,如下所示
Users{
String firstName
String lastName
String emailAddress
static hasMany = [projects:Projects]
}
class Projects {
String projectName
String description
Users projectLead
Date completionDate
static belongsTo = Users
}
这里completionDate == null表示该项目尚未完成。
现在我想向每个用户发送有关其不完整项目的电子邮件提醒,如何编写查询来检索每个用户不完整的项目?
我正在考虑以下几行,但仍然无法继续。为了发送电子邮件,我需要用户的 emailid、所有不完整的项目及其名称
def users = Users.list()
for(user in users){
user.projects.find{it.completionDate==null}
}
在这种情况下是否可以使用 createCriteria?
I have two domain classes Users and Projects like following
Users{
String firstName
String lastName
String emailAddress
static hasMany = [projects:Projects]
}
class Projects {
String projectName
String description
Users projectLead
Date completionDate
static belongsTo = Users
}
Here completionDate == null means the project has not yet been completed.
Now I want to send an email reminder to each user about their incomplete projects, How can write a query to retrieve incomplete projects per user?
I was thinking on following lines but am still not able to go ahead. In order to send an email I will need users emailid, all incomplete projects and names of them
def users = Users.list()
for(user in users){
user.projects.find{it.completionDate==null}
}
Is it possible to use createCriteria in such a case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这应该可行:
这应该只返回具有不完整项目的用户,并且每个
User
的projects
属性将仅包含不完整的项目。如果您希望用户加载所有项目,我相信您需要使用别名测试...
给定 User 类:
和 Project 类:
此集成测试通过(希望断言能解释它)
(这是sql在此实例中执行条件查询):
I think this should work:
That should just return you the Users with incomplete projects, and the
projects
property for eachUser
will only contain the incomplete projects. If you want the Users to have all of their projects loaded, I believe you need to use an aliasTesting...
Given the User class:
And the Project class:
This integration test passes (hopefully the asserts explain it)
(this is the sql the criteria query executes in this instance):
我不确定这个问题是否需要左连接,除非您想包含具有空用户的项目。为什么不直接选择所有完成日期为空的项目并针对用户加入?
在 HQL 中,它看起来像这样:
您可以在条件查询中执行类似的操作:
I'm not sure this problem requires a left join, unless you want to include Projects with a null user. Why not just select all projects with null completion dates and join against user?
In HQL, it would look something like this:
You can do similar in a criteria query: