-
Jun 1st, 2011, 05:09 AM
#1
Grails delete bidirectional association
Hi all,
I want to create bidirectional one to one association,
so I have created following domain classes and controllers:
class Face {
String val11
String val12
static hasOne = [nose: Nose]
static constraints = {
nose(nullable:true)
}
}
class Nose {
Face face
String val21
String val22
static constraints = {
face(nullable:true)
}
}
class FaceController {
static transactional = false
def create = {
def face = new Face()
def nose = Nose.get(params.nose)
if(nose!=null){
face.properties = params
face.nose = nose
face.save(flush: true)
nose.face = face
nose.save(flush:true)
} else{
face.properties = params
face.save(flush: true)
}
String strJson = (face as JSON)
render strJson
}
def remove = {
Face face = Face.findById(params.id)
face.delete(flush: true)
render params.id
}
}
class NoseController {
static transactional = false
def create = {
def nose = new Nose()
nose.properties = params
nose.save(flush: true)
String strJson = (nose as JSON)
render strJson
}
def remove = {
Nose nose = Nose.findById(params.id)
nose.delete(flush: true)
render params.id
}
}
I can successfully create nose and after it face objects, and can get nose value through face (Face.get(1).nose) and face value through nose (Nose.get(1).face).
My problem is when I want to delete objects(for example face), I got the following exception:
org.springframework.dao.DataIntegrityViolationExce ption: Foreign key constraint violation trying to delete onetoone.Nose with id 1
at org.grails.hbase.gorm.DeletePersistentMethod.invok e(DeletePersistentMethod.groovy:66)
---------------------------------------------------------------------------
I have tried before deleting face explicity set nose to null:
//in FaceController
def remove = {
Face face = Face.findById(params.id)
Nose nose = Nose.findById(face.nose.id)
face.nose = null
nose.face = null
nose.save(flush:true)
face.save(flush:true)
face.delete(flush: true)
render params.id
}
After this I can delete face, but in any case in my cosole it throw the following exception:
ERROR gorm.SavePersistentMethod - Cannot get property 'class' on null object
java.lang.NullPointerException: Cannot get property 'class' on null object
at org.codehaus.groovy.runtime.NullObject.getProperty (NullObject.java:56)
at org.codehaus.groovy.runtime.InvokerHelper.getPrope rty(InvokerHelper.java:156)
-----------------------------------------------------------------------------
ERROR associations.ReferenceTable - Reference table entry not found, row=[79, 78, 69, 84, 79, 79, 78, 69, 95, 79, 78, 69, 84, 79, 79, 78, 69, 95, 78, 79, 83, 69, 95, 0, 0, 0, 0, 0, 0, 0, 1]
I use grails-1.3.2 and gorm-hbase-0.2.4.
Please say what am I doing wrong?
If anybody know solution please help..
-
Jun 1st, 2011, 08:33 AM
#2
First of all, this could be a problem with gorm-hbase. If problems persist, try with the grails-hibernate plugin instead - if the problems go away, gorm-hbase is likely to be the problem.
Second, I recommend you add a 'static belongsTo = [face: Face]' line to Nose. That should ensure that the Nose is deleted when you delete the Face, assuming this is the behaviour you want. Is it?
Finally, if you have an domain instance ID, use Domain.get(id) rather than Domain.findById(id). It's more efficient and makes better use of the Hibernate second-level cache. Of course, if you're using HBase, this probably isn't a big deal, but it's still preferable to use Domain.get().
-
Jun 8th, 2011, 07:54 AM
#3
I have used hibernate and it worked.
pledbrook thanks for reply and advices.
Last edited by bella; Jun 9th, 2011 at 02:23 AM.
-
Jun 9th, 2011, 02:36 AM
#4
Is there any way to delete for gorm-hbase?
-
Jun 10th, 2011, 08:26 AM
#5
I genuinely don't know, sorry. I would ask on the Grails User mailing lists or on the Grails plugins forum (or both).
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules