• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Stupid doubt about Grails ORM

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using the defaul scaffolding of Grails. I haven't written any code for that!!
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic