Handling hibernate exceptions when records have been deleted

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Handling hibernate exceptions when records have been deleted

MT01
In my project I have two domain classes, call them Person and Project. Each Project can have one or two "people", so the Project class looks like

class Project {

    Person person1
    Person person2

    static constraints = { person2 nullable: true }

}

If a Person record is deleted, related Project person2 fields are not set to null, so the next time projects that were linked to the deleted person are accessed, I get

Class org.hibernate.ObjectNotFoundException
Message
No row with the given identifier exists: [com.mydomain.mypackage.Person#12345]

Is there a way to make GORM set fields on related records to null automatically when a record is deleted, or do I need to do this manually somewhere?

Right now when this happens I must manually edit the underlying database tables and null the field. 

Thanks,

MT

Reply | Threaded
Open this post in threaded view
|

Re: Handling hibernate exceptions when records have been deleted

Eamonn O'Connell-2
If you use the belongsTo mechanism it should do what you want. 
Alternatives are defining cascades, or manually setting to null on the object - something like:

Project.findAllByPerson2(personToDelete).each{ p -> 
   p.person2 = null
}

Regards,
Eamonn


On 13 December 2012 12:36, MT <[hidden email]> wrote:
In my project I have two domain classes, call them Person and Project. Each Project can have one or two "people", so the Project class looks like

class Project {

    Person person1
    Person person2

    static constraints = { person2 nullable: true }

}

If a Person record is deleted, related Project person2 fields are not set to null, so the next time projects that were linked to the deleted person are accessed, I get

Class org.hibernate.ObjectNotFoundException
Message
No row with the given identifier exists: [com.mydomain.mypackage.Person#12345]

Is there a way to make GORM set fields on related records to null automatically when a record is deleted, or do I need to do this manually somewhere?

Right now when this happens I must manually edit the underlying database tables and null the field. 

Thanks,

MT


Reply | Threaded
Open this post in threaded view
|

Re: Handling hibernate exceptions when records have been deleted

longwa
In reply to this post by MT01
I would change Project to hasMany Person. Then as Eamonn says, you can setup each Person as belongsTo Project. That will move the foreign key from Project to Person.

If you want to maintain the two person limit, you can add a minSize or maxSize constraint on the "persons" collection.

-Aaron


On Thu, Dec 13, 2012 at 6:36 AM, MT <[hidden email]> wrote:
In my project I have two domain classes, call them Person and Project. Each Project can have one or two "people", so the Project class looks like

class Project {

    Person person1
    Person person2

    static constraints = { person2 nullable: true }

}

If a Person record is deleted, related Project person2 fields are not set to null, so the next time projects that were linked to the deleted person are accessed, I get

Class org.hibernate.ObjectNotFoundException
Message
No row with the given identifier exists: [com.mydomain.mypackage.Person#12345]

Is there a way to make GORM set fields on related records to null automatically when a record is deleted, or do I need to do this manually somewhere?

Right now when this happens I must manually edit the underlying database tables and null the field. 

Thanks,

MT