Home Grails: Prevent Contents of the foreign key getting Updated
Reply: 0

Grails: Prevent Contents of the foreign key getting Updated

user1197
1#
user1197 Published in April 26, 2018, 1:47 pm

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.740014 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO