gpt4 book ai didi

hibernate - 如何基于id更新一行(grails域类)?

转载 作者:行者123 更新时间:2023-12-02 16:00:28 25 4
gpt4 key购买 nike

我需要更新ID已知的数据库中的一行。但是要更新的字段数量很多,并且是一个复杂的对象。 (即)包含嵌套对象。因此,我正在尝试下面的代码行,但不是更新而是发生插入

long myId // Primary key/id of row to be updated
DomainObject obj = gson.fromJson(jsonString, DomainObject.class)
obj.id=myId
obj.save(flush:true)

请帮忙为什么要插入而不是更新。如何基于id更新一行(grails域类)?

最佳答案

你可以试试看

        def gson = gsonBuilder.create()
// because the incoming JSON contains an id this will read the DomainObject
// from the database and update it!
def domainObjectInstance = gson.fromJson(request.reader, DomainObject)

domainObjectInstance.save();

关于hibernate - 如何基于id更新一行(grails域类)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31696593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com