Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Why is GORM performing cascading delete, even if I omit the belongsTo keyword?
- class Holiday {
- String justification
- User user
- //static belongsTo = User
- static constraints = {
- }
- }
- class User {
- String login
- String password
- static hasMany = [ holidays : Holiday ]
- static constraints = {
- }
- }
- void testWithoutBelongsTo() {
- def user1 = new User(login:"anto", password:"secret")
- user1.save()
- def holiday1 = new Holiday(justification:"went to trip")
- holiday1.save()
- user1.addToHolidays(holiday1)
- assertEquals 1, User.get(user1.id).holidays.size()
- user1.delete()
- assertFalse User.exists(user1.id)
- assertFalse Holiday.exists(holiday1.id)
- }
- class Holiday {
- String justification
- User user
- //static belongsTo = User
- static constraints = {
- user(nullable: true)
- }
- }
- void testWithoutBelongsTo()
- {
- def user1 = new User(login:"anto", password:"secret")
- user1.save(failOnError: true)
- def holiday1 = new Holiday(justification:"went to trip",
- user: user1) // Set user properly
- holiday1.save(failOnError: true)
- user1.addToHolidays(holiday1)
- assert 1, User.get(user1.id).holidays.size()
- holiday1.user = null // Unset user as otherwise your DB
- // won't be happy (foreign key missing)
- user1.delete()
- assert ! User.exists(user1.id)
- assert Holiday.exists(holiday1.id)
- }
Add Comment
Please, Sign In to add comment