[hibernate-issues] [Hibernate-JIRA] Created: (EJB-356) ManyToMany queries are generated incorrect

Heiko Wenzel (JIRA) noreply at atlassian.com
Mon May 5 05:02:33 EDT 2008


ManyToMany queries are generated incorrect
------------------------------------------

                 Key: EJB-356
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/EJB-356
             Project: Hibernate Entity Manager
          Issue Type: Bug
          Components: EntityManager
         Environment: all database platforms, last Hibernate version
            Reporter: Heiko Wenzel
            Priority: Critical


See this example:
A ManyToMany reference between 2 entities generates a wrong query: 
1.) 2 entities Entity1 and Entity2 like:
----------------------------------------------------------
@Entity
public class Entity1 {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @ManyToMany
    private List<Entity2> entity2;

    public List<Entity2> getEntity2() {
        return entity2;
    }

    public void setEntity2(List<Entity2> entity2) {
        this.entity2 = entity2;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}
---------------------------------------------------
@Entity
public class Entity2 {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @ManyToMany(mappedBy = "entity2")
    private List<Entity1> entity1;

    public List<Entity1> getEntity1() {
        return entity1;
    }

    public void setEntity1(List<Entity1> entity1) {
        this.entity1 = entity1;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

------------------------------------------
2:) The query to select is: 

        Query query = em.createQuery("select a from Entity1 a where a.entity2 = :e2");
        query.setParameter("e2", new Entity2());
        List list = query.getResultList();
------------------------------------------
3.) Hibernate generates the following incomplete query to execute on the SQL server:

Hibernate: select entity1x0_.id as id57_ from Entity1 entity1x0_, Entity1_Entity2 entity2x1_, Entity2 entity2x2_ where entity1x0_.id=entity2x1_.entity1_id and entity2x1_.entity2_id=entity2x2_.id and .=?

Please take a look at the last token in this query  "and .=?". 
This token is incomplete and the underlying database cannot execute this.
The error message is: 
...
Caused by: org.firebirdsql.jdbc.FBSQLException: GDS Exception. 335544569. Dynamic SQL Error
SQL error code = -104
Token unknown - line 1, column 189
.
------------------------------------
------------------------------------
Please note, the referring parameter is a entity, not the ID of it. 
If it is a ManyToOne relation, the query is generated correct.

TopLink or OpenJPA generates also a usable query in this case,
but not Hibernate.

Can you give a solution?

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the hibernate-issues mailing list