我有域类,如下所示
class College implements Serializable
{
String name
String description
}
class Student implements Serializable
{
String name
College college
}
我在StudentController中有UpdateStudent动作,它接受Student对象并更新MySQL数据库中的数据,我面临的问题是外键" College"与#34; Student"一起得到更新,这是不可取的,我只是希望更新Student对象而忽略外键。
作为解决方法,我使用
student.college.refresh()
在动作中,从数据库中取出大学对象并忽略学生中的大学对象,但是在代码库很大的情况下,很难在大学对象上调用refresh()。我该如何解决这个问题?
我使用的是Grails 3。
示例学生对象
{
"id":1,
"name":"Arjun",
"college":{
"id":1,
"name":"XYZ College",
"description": "Test description"
}
}
当我更新Student对象时,不应该更新college对象的内容。
答案 0 :(得分:1)
我不清楚您的问题,但您可以尝试以下方式:
保存Student
对象时添加deepValidate:false
student.save(deepValidate:false)
或尝试以下
您可以向Student
域类添加约束:
static constraints = {
college blank: true, nullable: true
}
默认情况下,所有域类属性都不可为空(即它们具有隐式nullable: false
约束。)
希望这会对你有所帮助
答案 1 :(得分:0)
我已经在Domain中使用瞬态解决了这个问题,现在域看起来像
$(document).bind('keydown', function (e) {
if(e.keyCode == 116 || (e.keyCode == 116 && e.ctrlKey)){
e.preventDefault(); // Prevent default bevaior only if F5 or Ctrl+F5 [thanks to the first two commenters (y) :)]
//This is from your original post
abp.message.confirm("Are You Sure To Refresh This Page?",
function (isConfirm) {
if (isConfirm) {
window.location = "NewForm?id=" + $("#Id").val();
}
});
}
});
});
现在,如果我想自动更新对象,我将在College.save()之前调用college.update = true