简体   繁体   English

合并后DomainClass.getAll()无法正常工作

[英]DomainClass.getAll() not working correctly after merge

I was trying to update a row on the database and noticed that once the row was updated, getAll stopped working correctly. 我试图更新数据库上的一行,并注意到一旦更新了该行,getAll就会停止正常工作。 All the records that were updated, started missing from getAll's result. 所有已更新的记录开始从getAll的结果中丢失。

for instance, imagine you have a list of products [product1, product2, product3]. 例如,假设您有一个产品列表[product1,product2,product3]。 if I do something like product2.someProperty = 2 followed by a product2.merge(), product two will start missing from the list. 如果我做类似product2.someProperty = 2的操作,然后再执行product2.merge(),则列表中将缺少第二个产品。 So the result of Products.getAll() will be [product1, product3]. 因此,Products.getAll()的结果将为[product1,product3]。

(Sorry if this is a noob question) (很抱歉,如果这是一个菜鸟问题)

If I perform a Product.get(idOfUpdatedProduct), it will work. 如果我执行Product.get(idOfUpdatedProduct),它将正常工作。 I've checked the database and the merge is updating the data correctly. 我已经检查了数据库,并且合并正确地更新了数据。 The problem seems to be on the GetAll() method. 问题似乎出在GetAll()方法上。 findAll() presents the same behavior. findAll()表现出相同的行为。

my code: 我的代码:

---action
def update() {
        Product product = productService.detail(params.id?.toLong())
        bindData(product, params)
        productService.updateProduct(product)
        redirect action: "show", params: [id: params.id]
}

---service
void updateProduct(product) {

        product.validate()
        StringBuilder str = new StringBuilder()
        product.errors.allErrors.each { str << "${it}" }

        if (!str.toString().isEmpty()) throw new Exception("Error: ${str.toString()}")

        product.merge(flush: true, failOnError: true)

    }

I found the problem. 我发现了问题。

the result was actually being returned, but for some reason, it was placed in the middle of the list. 结果实际上是被返回的,但是由于某种原因,它被放置在列表的中间。

I solved it by sorting the list. 我通过对列表进行排序解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM