Guest User

Untitled

a guest
Feb 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. class Organization {
  2.  
  3. String name
  4.  
  5. static hasMany = [
  6. users: Person,
  7. teams: Team
  8. ]
  9.  
  10. static mapping = {
  11. users cascade: 'all-delete-orphan'
  12. }
  13. }
  14.  
  15. class Person {
  16.  
  17. String name
  18. Organization organization
  19.  
  20. static belongsTo = [Organization, Team]
  21. static hasMany = [teams: Team]
  22.  
  23. static constraints = {
  24. }
  25.  
  26. def beforeDelete() {
  27.  
  28. Team.withNewSession {
  29. removeFromAllTeams()
  30. }
  31.  
  32. true
  33. }
  34.  
  35. def removeFromAllTeams() {
  36. Team.where {
  37. members {
  38. id == this.id
  39. }
  40. }.each { Team team ->
  41. if (team.members.contains(this)) {
  42. team.members.remove(this)
  43. team.save()
  44. }
  45. }
  46. }
  47. }
  48.  
  49.  
  50. class Team {
  51.  
  52. String name
  53.  
  54. static hasMany = [members: Person]
  55. static belongsTo = [organization: Organization]
  56.  
  57. static constraints = {
  58. }
  59. }
  60.  
  61. SQL [n/a]; Referential integrity constraint violation: "FKGHKKY8WMH379RPMFH92T807RY: PUBLIC.TEAM_MEMBERS FOREIGN KEY(PERSON_ID) REFERENCES PUBLIC.PERSON(ID) (1)"; SQL statement:
  62. delete from person where id=? and version=? [23503-194]; nested exception is org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "FKGHKKY8WMH379RPMFH92T807RY: PUBLIC.TEAM_MEMBERS FOREIGN KEY(PERSON_ID) REFERENCES PUBLIC.PERSON(ID) (1)"; SQL statement:
  63. delete from person where id=? and version=? [23503-194]
  64.  
  65. @Unroll
  66. void "user is removed from team before deletion, runManually = #runManually"() {
  67. given: 'an existing org'
  68. Organization.withNewSession {
  69. def organization = new Organization(name: 'Cyberdyne Systems').save(failOnError: true)
  70.  
  71. and: 'a person is added'
  72. organization.addToUsers(name: 'John Connor').save(failOnError: true)
  73. organization.save(failOnError: true, flush: true)
  74.  
  75. and: 'a new team is added to the org'
  76. organization.addToTeams(name: 'IT').save(failOnError: true, flush: true)
  77.  
  78. and: 'the person is added to the team'
  79. organization.teams.first().addToMembers(organization.users.first())
  80. organization.save(failOnError: true, flush: true)
  81. }
  82.  
  83. and: 'the person is deleted'
  84. def userToDelete = Person.first()
  85. if (runManually) {
  86. userToDelete.removeFromAllTeams()
  87. }
  88. userToDelete.delete(flush: true)
  89.  
  90. expect: 'the team has no users'
  91. Team.first().members.isEmpty()
  92.  
  93. and: 'there are no more users'
  94. Person.count == 0
  95.  
  96. where:
  97. runManually << [true, false]
  98. }
Add Comment
Please, Sign In to add comment