Kornelito Benito
Kornelito Benito

Reputation: 1097

Hibernate delete: SQLGrammarException Unexpected token: CROSS

I've got 3 Entities:

Entity A, Entity B, Entity C.

public class Entity_A {

    public List<Entity_B> entBList;
    Some other objects...

     @OneToMany(mappedBy="entityA", cascade = {CascadeType.ALL}, fetch = FetchType.EAGER, orphanRemoval=true)
     @Fetch(value = FetchMode.SUBSELECT)
     public List<GebruikerKind> getEntBList() {
        return entBList;
     }

} 

public class Entity_B {
    public Entity_A entityA;
    public Entity_C entityC;
    Some other objects...


    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "COLUMNJOINNAME")
    public Entity_A getEntityA() {
        return entityA;
    }

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "OTHERCOLUMNJOINNAME)
    public Entity_C getEntityC() {
        return entityC;
    }
}

public class Entity_C {
    public List<Entity_B> entBList;
    Some other objects...


     @OneToMany(mappedBy="entityC", cascade = {CascadeType.ALL}, fetch = FetchType.EAGER, orphanRemoval=true)
     @Fetch(value = FetchMode.SUBSELECT)
     public List<GebruikerKind> getEntBList() {
        return entBList;
     }
} 

Notice: All setters are available, and there are plenty of other objects available too :). As far as I know, my configuration is good.. I'm trying to delete a Entity_B:

Query query = getCurrentSession().createQuery(
            "delete from " + Entity_B.class.getName() + " eb " +
            " where eb.entityA.someField = :someField and eb.entityC.someOtherField = :someOtherField"); 


    query.setString("someField", someId);
    query.setString("someOtherField", someOtherId);
    return query.executeUpdate();

Troubles in paradise:

Hibernate throws me this SQLGrammarException: Unexpected token: Cross. Anyone knows what i can do?

Upvotes: 3

Views: 1294

Answers (1)

codedabbler
codedabbler

Reputation: 1261

One solution is to rewrite the query.

You can avoid the cross join between eb.entityA and eb.entityC by writing a SQL query to fetch the ids that you need to delete. Then call HQL delete on each of those ids.

Upvotes: 2

Related Questions