当我编写以下类时,我得到以下编译错误:
无法解决财产问题
我怎样才能实现以下目标:
class Employee{
String Name
String Email
Employee Manager
static hasMany = [desginations:Designation]
static constraints = {
Name(unique:true)
Email(unique:true)
}
Run Code Online (Sandbox Code Playgroud)
谢谢,非常感谢.
我对嵌套域类有内部要求,我希望将父关系的更新传播给子项.代码示例可以说清楚:
class Milestone {
static belongsTo = [project:Project]
static hasMany = [goals:OrgGoals, children:Milestone]
String name
Date start
Date estimatedEnd
Date achievedEnd
...
}
Run Code Online (Sandbox Code Playgroud)
当父里程碑的estimatedEnd更新时,我希望孩子的估计会自动更新相同的金额.GORM的beforeUpdate()钩子似乎是一个合乎逻辑的地方:
为了让生活更轻松,我想使用一些简单的Date算法,所以我在Milestone类中添加了以下方法:
def beforeUpdate()
{
// check if an actual change has been made and the estimated end has been changed
if(this.isDirty() && this.getDirtyPropertyNames().contains("estimatedEnd"))
{
updateChildEstimates(this.estimatedEnd,this.getPersistentValue("estimatedEnd"))
}
}
private void updateChildEstimates(Date newEstimate, Date original)
{
def difference = newEstimate - original
if(difference > 0)
{
children.each{ it.estimatedEnd+= difference }
}
}
Run Code Online (Sandbox Code Playgroud)
没有编译错误.但是当我运行以下集成测试时:
void …Run Code Online (Sandbox Code Playgroud) 我有一个域类(缩小)为: -
class Expense {
Date dateOfExpense
int amount
}
Run Code Online (Sandbox Code Playgroud)
我试图获得按周/月/费用日期分组的金额总和.参考grails doc http://grails.org/doc/latest/guide/GORM.html中的 'sqlGroupProjection'方法,
我尝试使用以下代码: -
def results = c {
between("dateOfExpense", fromDate, toDate)
projections {
sqlGroupProjection 'dateOfExpense,sum(amount) as summed',
'MONTH(dateOfExpense)',['date','summed'],[DATE,NUMBER]
}
}
Run Code Online (Sandbox Code Playgroud)
抛出异常:
No such property: DATE for class: grails.orm.HibernateCriteriaBuilder. Stacktrace follows:
Message: No such property: DATE for class: grails.orm.HibernateCriteriaBuilder
Run Code Online (Sandbox Code Playgroud)
请建议使用sqlGroupProjection方法的方法
在我的grails域中,我有一个字段Date,即java.util.Date.
在我的控制器中,我使用SimpleDateFormate从params加载此日期.
确切地说,params.date类似'20/02/2013 02:30 am'.在控制器中我加载如下:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm a"); domainInstance.date = simpleDateFormat.parse(params.date)
执行此语句时,未检测到错误.但是,当保存域实例时会生成错误
[typeMismatch.Domain.date,typeMismatch.date,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable:codes
[无法将'java.lang.String'类型的属性值转换为属性'date'所需的类型'java.util.Date'; 嵌套异常是java.lang.IllegalArgumentException:无法解析日期:Unparseable date:"20/02/2013 02:30 am"]
你能告诉我哪里出了问题吗?我很确定SimpleDateFormat将String解析为Date.为什么它接受为String.
嗨,我试图获得一个列表的总计数与groovy totalCount但它抛出:
Exception evaluating property 'totalCount' for java.util.ArrayList,
Reason: groovy.lang.MissingPropertyException: No such property: totalCount
Run Code Online (Sandbox Code Playgroud)
无论如何,我可以用groovy totalCount而不是.size?
如何在grails中编写一个createCriteria,它只从表中拉出几列而不是所有列?
我有一张叫做广告的桌子.我想只检索列"标题","价格"和"照片".
def c = Classified.createCriteria()
def records = c.list {
eq('publish_date', '2014-06-06')
}
maxResults(8)
}
Run Code Online (Sandbox Code Playgroud)
上面的查询检索所有记录.如何限制只有几列?
有类产品:
class Product {
static hasMany = [attributeValues: ProductAttributeValue]
String productId
String manufacturer
BigDecimal price
static constraints = {
productId unique: true, blank: false
manufacturer blank: false
price min: BigDecimal.ZERO
}
}
Run Code Online (Sandbox Code Playgroud)
我想找到所有产品,哪个productId包含子串'filter'.我写了下一个代码:
Product.findAll {it.productId.contains(filter)}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.为什么?
情况:
我使用名为firstService的Grails资源和服务发送请求;
第一个请求的任务是
book1.save(flush: true);但是当我用book1.save(flush: true);它保存时不会立即保存,这就是为什么在60秒内第二个请求找不到book1实例;
第二个请求的任务只是更新第一个请求的数据.
我该如何解决这个问题?
我们公司使用的大多数域对象都有一些共同的属性.它们代表创建对象的用户,最后更新对象的用户以及用于执行此操作的程序.
在利益DRY荷兰国际集团我的领域类,我想找到一些方法来添加相同beforeInsert和更新前的逻辑具有这些列的所有领域类不与那些不干扰.
我是怎么想这样做的,就是使用一个带有自己的beforeInsert和beforeUpdate方法的Mixin.我知道你可以在域类上使用Mixins.
package my.com
import my.com.DomainMixin
@Mixin(DomainMixin)
class MyClass {
String foo
String creator
String updater
static constraints = {
creator nullable:false
updater nullable:false
}
}
package my.com
class DomainMixin {
def beforeInsert() {
this.creator = 'foo'
this.updater = 'foo'
}
def beforeUpdate() {
this.updater = 'bar'
}
}
Run Code Online (Sandbox Code Playgroud)
单元测试表明,当以这种方式实现时,beforeInsert方法实际上没有被触发.
旁注:我也知道可以使用metaClass 在BootStrap.groovy文件中添加方法,但我的好奇心已经变得更好了,我真的想看看mixin是否有效.请随意告诉我,这是更好的方法,我不应该混淆男人不应该的地方.
我按照我在网上找到许多地方的说明,了解Grails 2.2.4域对象属性如何在相应的MySQL 5.5列上创建默认值.
不幸的是,应该具有默认值的列没有应用于其MySQL列的默认值.
以下是我的域对象代码的相关摘录.有什么问题吗?:
class SelectOption {
int minSelectCount = 0
int maxSelectCount = 1
static constraints = {
minSelectCount nullable: false, min: 0, defaultValue: "0"
maxSelectCount nullable: false, min: 1, defaultValue: "1"
}
}
Run Code Online (Sandbox Code Playgroud)