This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Groovy and the fly likes Stupid doubt about Grails ORM Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Languages » Groovy
Bookmark "Stupid doubt about Grails ORM" Watch "Stupid doubt about Grails ORM" New topic
Author

Stupid doubt about Grails ORM

Halley Thomas
Greenhorn

Joined: Nov 17, 2008
Posts: 9
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

Joined: Jul 11, 2001
Posts: 15299
    
    6

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?


GenRocket - Experts at Building Test Data
Halley Thomas
Greenhorn

Joined: Nov 17, 2008
Posts: 9
I am using the defaul scaffolding of Grails. I haven't written any code for that!!
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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

Joined: Mar 01, 2009
Posts: 1609
    
  13

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:



No more Blub for me, thank you, Vicar.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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

Joined: Mar 01, 2009
Posts: 1609
    
  13

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

Joined: Nov 17, 2008
Posts: 9
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..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stupid doubt about Grails ORM
 
Similar Threads
Doubt in reflection
doubt
Doubt
doubt??
doubt