• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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..
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic