Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HQL Deletion fail

 
P Marksson
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I have this HQL Get Query which works just fine:

In an other method, I have the following HQL which cases an "unexpected token: inner":

As you see, the only difference between these two queries is that the second one begins with an delete keyword.

Full code:


Why do the second one fail?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi P Marksson,

First of all, a warm welcome to CodeRanch!

P Marksson wrote:Why do the second one fail?

Probably because you can't use INNER JOIN in a DELETE statement. And that's confirmed in the Delete Queries section of the Java Persistence WikiBook. So you'll probably have to rewrite your query using a subquery instead.

Hope it helps!
Kind regards,
Roel
 
P Marksson
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bumb! Thanks for the help! So I am trying to implement this using sub queries but can't get it to work. Have been reading tutorials and can't really see what's wrong.

I have the table URL which have a fk to table Group. The entities look like this:



This is the delete method:


This query throws an exception with no details.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P Marksson wrote:This query throws an exception with no details.

Which exception do you get?

Could you try the simplest version of that query using nested properties?

If that doesn't work try something similar based on the group id

Hope it works!
Kind regards,
Roel

PS. It might be worth mentioning that Group might be interpreted as a HQL keyword as it is part of the GROUP BY clause. If it's an issue, try enclosing Group within square brackets ([Group]), single or double quotes.
 
P Marksson
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Escaping Group with bracket, single or double quote actually resulted in org.hibernate.QueryException, where the escaped character being denoted as "unexpected".



The first snipped you suggested have I already tried. Just to make sure, I tried again with no luck. The exception was
Trying the last suggestion, I got exactly the same error.

A bad solution to this would be to get the group by name, and then scan the OneToMany-URL-List presented in the Group class, search for for the given URL and remove it. Really ugly.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P Marksson wrote:The first snipped you suggested have I already tried. Just to make sure, I tried again with no luck. The exception was
Trying the last suggestion, I got exactly the same error.

That's a very descriptive exception you get! That's not related to the query itself, but due to the lack of a transaction. You need a transaction to execute an update/delete query. So there seems to be something wrong with your configuration...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic