Guest User

Untitled

a guest
Aug 11th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. Why is GORM performing cascading delete, even if I omit the belongsTo keyword?
  2. class Holiday {
  3. String justification
  4. User user
  5. //static belongsTo = User
  6. static constraints = {
  7. }
  8. }
  9.  
  10. class User {
  11. String login
  12. String password
  13. static hasMany = [ holidays : Holiday ]
  14. static constraints = {
  15. }
  16. }
  17.  
  18. void testWithoutBelongsTo() {
  19. def user1 = new User(login:"anto", password:"secret")
  20. user1.save()
  21. def holiday1 = new Holiday(justification:"went to trip")
  22. holiday1.save()
  23. user1.addToHolidays(holiday1)
  24. assertEquals 1, User.get(user1.id).holidays.size()
  25. user1.delete()
  26. assertFalse User.exists(user1.id)
  27. assertFalse Holiday.exists(holiday1.id)
  28. }
  29.  
  30. class Holiday {
  31. String justification
  32. User user
  33. //static belongsTo = User
  34. static constraints = {
  35. user(nullable: true)
  36. }
  37. }
  38.  
  39. void testWithoutBelongsTo()
  40. {
  41. def user1 = new User(login:"anto", password:"secret")
  42. user1.save(failOnError: true)
  43. def holiday1 = new Holiday(justification:"went to trip",
  44. user: user1) // Set user properly
  45. holiday1.save(failOnError: true)
  46. user1.addToHolidays(holiday1)
  47. assert 1, User.get(user1.id).holidays.size()
  48. holiday1.user = null // Unset user as otherwise your DB
  49. // won't be happy (foreign key missing)
  50. user1.delete()
  51. assert ! User.exists(user1.id)
  52. assert Holiday.exists(holiday1.id)
  53. }
Add Comment
Please, Sign In to add comment