这是我域的代码。
class Btr {
Date dateBreak
int timeBreak
String typeBreak
User usuario
static constraints = {
}
static mapping = {
}
}
class User {
String name
String user
String password
String confirmPassword
String state
String extent
String movileNumber
String email
String address
Rol rol
static constraints = {
}
static mapping = {
}
}
这是我的控制器的代码。
def df = new SimpleDateFormat("yyyy-MM-dd HH:mm")
def startDate = params.startDate
def stopDate = params.stopDate
resultado = Btr .executeQuery("select dateBreak, timeBreak, typeBreak,
user, usuario.rol from Btr inner join User on user = usuario.rol where
dateBreak between :startDate" and :stopDate", [startDate:
df.parse(startDate), stopDate: df.parse(stopDate)])
render (view: "data", model: [result: resultado])
这是我的看法。
<g:each in="${result}" var="results" status="i">
<tr><td>{results.dateBreak}</td><td>{results.timeBreak}</td><td>
{results.typeBreak} </td><td>${results.usuario.rol}</td></tr>
</g:each>
然后我在提交表单时收到此错误。在GSP中,当我打印数据时,
Exception evaluating property 'dateBreak' for java.util.Arrays$ArrayList, Reason: groovy.lang.MissingPropertyException: No such property: dateBreak for class: java.sql.Timestamp
有人可以告诉我如何使用 executeQuery 连接 grails 中的表,并且很高兴学习使用它,使用标准
resultado = Btr .executeQuery("select dateBreak, timeBreak, typeBreak,
user, usuario.rol from Btr inner join User on user = usuario.rol where
dateBreak between :startDate" and :stopDate", [startDate:
df.parse(startDate), stopDate: df.parse(stopDate)])
应该是
resultado = Btr .executeQuery("""select new map (btr.dateBreak as dateBreak, btr.timeBreak as timeBreak, btr.typeBreak as typeBreak,
u as user, user.usuario.rol as rol) from Btr btr join btr.user u where
btr.dateBreak between :startDate and :stopDate""", [startDate:
df.parse(startDate), stopDate: df.parse(stopDate)])
您拥有的是原始SQL而不是HQL,这是一个轻微的变化,并使用实际的域对象来连接
对于典型的一对一关系,使用左联接可能为空的多个联接
如果一对一关系可以为空,也使用左联接
除此之外,您可以将实际查询作为原始 sql 查询,如下所示
def sql=new Sql(dataSource)
return sql.rows(query,whereParams)