gpt4 book ai didi

grails - 如何修改现有域并将新属性更新到Bootstrap?

转载 作者:行者123 更新时间:2023-12-02 15:59:14 25 4
gpt4 key购买 nike

好吧,我将简化问题,以便您可以更好地理解它。
我有一个正在生产的软件,该软件的域名为Person,带有一些属性。
我在Contact域中添加了一个名为Person的新属性,启动我的应用程序从Contact获取一些信息时,需要更新Person.AnotherContactObject

所以在我的Bootstrap.groovy中,我试图这样做:

List<Person> persons = Person.findAll()
for(Person person : persons){
person.contact = new Contact()
person.save(flush:true)
}

我收到此错误:
context.GrailsContextLoader Error initializing the application: object references an unsaved transient instance - save the transient instance before flushing: com.test.Contact; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.test.Contact
org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: com.test.Contact; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.test.Contact
at BootStrap$_closure1.doCall(BootStrap.groovy:154)
at grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:308)
at grails.util.Environment.executeForEnvironment(Environment.java:301)
at grails.util.Environment.executeForCurrentEnvironment(Environment.java:277)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.test.Contact
... 8 more

好吧,我也试图解决这种问题:

创建新的 Contact对象,将其保存,然后将其设置为 person.contact并保存 person对象,但仍然存在相同的错误。

最佳答案

我假设Contact也是一个域类。您需要先保存Contact,然后才能保存Person

List<Person> persons = Person.findAll().each { person ->
person.contact = new Contact().save(flush:true)
person.save(flush:true)
}

当然,不要忘了在保存 Contact实例之前设置所有不可为空的属性。

关于grails - 如何修改现有域并将新属性更新到Bootstrap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32637524/

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