4

There are two tables: bb_players and bb_player_skills. player skills table has one to one relation with bb_players, and also foreign key to bb_players.

Error happens at executing this code:

Query q = em.createNamedQuery(PlayerSkill.DELETE_SKILL_BY_PLAYER_ID);
q.setParameter("playerID", playerID);
q.executeUpdate();

The named query is:

 @NamedQuery(name = PlayerSkill.DELETE_SKILL_BY_PLAYER_ID, query = "DELETE FROM   PlayerSkill s " +
 " WHERE s.player.id = :playerID")

The error from postgresql logs is:

ERROR,42601,"syntax error at or near ""cross""",,,,,, 
 "delete from bb_player_skills cross join bb_players player1_ where id=$1",30,,""

Is my named query wrong and how should I rewrite it?

4
  • Why do you need a named query for a simple delete by PK? Commented Sep 23, 2015 at 23:44
  • @torvin what do you suggest, pure sql ? Commented Sep 24, 2015 at 0:43
  • 1
    I suggest pure Hibernate: use proper mapping for your Player class and use session.delete() to delete it. And Hibernate would create the query by itself without your help. Commented Sep 24, 2015 at 0:45
  • @torvin can you please provide simple example? Commented Sep 25, 2015 at 9:34

3 Answers 3

8

It appears that this may be an open Hibernate issue depending on your Hibernate version.

From: https://hibernate.atlassian.net/browse/HHH-7314

Using a JPA Delete query with conditions requiring a join through Hibernate entity-manager generates invalid SQL for PostgreSQL. PostgreSQL cannot use CROSS JOIN in the FROM clause of a DELETE query.

Sign up to request clarification or add additional context in comments.

Comments

3

In case anyone else faces the same problem,Here is one solution.
The named query can be rewritten to avoid join cross.

@NamedQuery(name = "PlayerSkill.DELETE_SKILL_BY_PLAYER_ID", query = "DELETE FROM   PlayerSkill s " +
 " WHERE s.player IN (SELECT player FROM Player player where player.id=:playerID)")

Comments

0

Looks like it is not a one to one relation. From bb_players it shoutd be a one to many relation, from bb_player_skills it should be a many to one relation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.