Grails GORM:SELECT AS

Grails GORM : SELECT AS

我正在尝试使用 GORM 获取今天出生的所有用户,但我无法在 Grails 中编写此查询:

SELECT
DAY(dateOfBirth) AS 'day',
MONTH(dateOfBirth) AS 'month'
FROM Users
WHERE day = '...' AND month = '...';

... 将替换为今天的值。

最小 User 域 class

class User {

  Date dateOfBirth

  ...

}

最小UserServiceclass

@Transactional
class UserService {

  def getTodayBirthdays() {

    def bornTodayQuery = User.where{
      /* I'm thinking here I must
       * select the DAY and MONTH from the DB
       * and compare it with the today ones.
       */
    }       

    User usersBornToday = bornTodayQuery.findAll()      

    usersBornToday      

  }

}

关于如何使用 GORM 做别名(SELECT 字段 AS 别名)有什么想法吗?

我正在使用:

谢谢!

您可以在您的服务中使用 where 查询

@Transactional(readOnly = true)
def listBirthday(int _month, int _day) {
  // Calendar.JANUARY equals to zero!
  def m = _month + 1
  // Run a where query on the users
  User.where {
    month(dateOfBirth) == m && day(dateOfBirth) == _day
  }
}

@Transactional(readOnly = true)
def listBirthdayToday() {
  def cal = Calendar.getInstance()
  listBirthday(cal.get(cal.MONTH), cal.get(cal.DAY_OF_MONTH))
}

除了monthday还有一些其他的功能,here是文档(找"Other Functions")

Any ideas how can I do an alias (SELECT field AS alias) with GORM?

(SELECT field AS _alias)

在 grails-2.5.6 中使用下划线 (_) 作为别名的前缀对我有用

我没有在文档中找到它,而是使用跟踪和错误方法。