Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stupid doubt about Grails ORM

 
Halley Thomas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was exploring Grails and I found this stupid stupid doubt!!!

Two classes




When I try to delete an object of Publisher, it is not letting me.
An exception is raised, org.hibernate.exception.ConstraintViolationException: could not delete: [libraryonline.Publisher#1].

Any explanations please?
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no reason, given the code you provided, that the publisher should not be deleted. I used your code and wrote the following test, all passing:



Maybe you have left something out that needs to be shared so we see more of what is going on. Maybe show us the code where you are deleting?
 
Halley Thomas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the defaul scaffolding of Grails. I haven't written any code for that!!
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Halley Thomas wrote:I am using the defaul scaffolding of Grails. I haven't written any code for that!!


Domains aren't scaffolded so your point makes no sense. Write a unit test and see what your results are. In fact, you could just copy and paste the one I wrote for you if the domains are the same.
 
chris webster
Bartender
Posts: 2407
32
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is that it is failing because your Publisher still has Books belonging to it, so it is causing a foreign key constraint error in your database. That's what constraints are for - to stop you doing bad things by mistake .

In general, you need to make sure that deleting the Publisher cascades to the Books if you want them to be deleted at the same time. Maybe the problem is that your Publisher class does not know it has Books (although it should know this from the belongsTo declaration), so it does not know it has to delete them as well. Not sure why this would be the case, but you could try adding this to your Publisher class so it knows explicitly that it has Books:


 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chris webster wrote:My guess is that it is failing because your Publisher still has Books belonging to it, so it is causing a foreign key constraint error in your database. That's what constraints are for - to stop you doing bad things by mistake .

You need to make sure that deleting the Publisher cascades to the Books if you want them to be deleted at the same time.


If his code is what he posted, Publisher does not belong to a Book. He has no belongsTo in his code. So that shouldn't be the issue.
 
chris webster
Bartender
Posts: 2407
32
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gregg Bolinger wrote:
If his code is what he posted, Publisher does not belong to a Book. He has no belongsTo in his code. So that shouldn't be the issue.

No, but the Publisher implicitly "hasMany" books, because he does say explicitly that a Book "belongsTo" a Publisher, so right now it looks like Grails is leaving them there in the database when it tries to delete the Publisher.

But that's just my guess...
 
Halley Thomas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup. It worked Chris. Thanks.
I guess you have to map explicitly.
Is it just GORM?
I need to try the same in JPA and see the outcome..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic