I have the domain classes as shown below
class College implements Serializable
{
String name
String description
}
class Student implements Serializable
{
String name
College college
}
And I have UpdateStudent action in StudentController, which accepts Student object and updates the data in MySQL database, the problem I'm facing is the foreign key "College" is getting updated too along with "Student", which is undesirable, I just want Student object to be updated ignoring the foreign keys.
As a workaround I'm using
student.college.refresh()
in the action, which fetches college object from database and ignores the college object in student, but with big codebase it is difficult to call refresh() on college object everywhere. How can I address this problem?
I'm using Grails 3.
Sample Student Object
{
"id":1,
"name":"Arjun",
"college":{
"id":1,
"name":"XYZ College",
"description": "Test description"
}
}
Here the contents of the college object should not be updated when I update Student object.
I've addressed this problem using transients in Domain, now the domain looks like
class College implements Serializable
{
String name
String description
boolean update
transients = ['update']
def beforeUpdate()
{
if(!this.update)
return false
}}
Now if I want to update the object voluntarily, i'll call college.update = true before college.save()