-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate creating invalid SQL (MySQL)
PostPosted: Thu Apr 15, 2010 6:26 pm 
Newbie

Joined: Thu Apr 15, 2010 5:55 pm
Posts: 2
Versions:
- Hibernate 3.2.6.ga
- MySQL 5.1.39

The problem seems to be in manyTomany association. I have two classes created by hbm2java and using annotations, no xml configuration here. Both classes are in persistence.xml. When trying to do the query, i get:

Code:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as col_5_0_, realty0_.rental as col_6_0_, realty0_.sale as col_7_0_ from foo


The whole query created by hibernate is:

Code:
Hibernate: select realty0_.id as col_0_0_, realty0_.street as col_1_0_, realty0.postcode as col_2_0_, town1_.name as col_3_0_, realty0_.area as col_4_0_, . as col_5_0_, realty0_.rental as col_6_0_, realty0_.sale as col_7_0_ from foo.realty realty0_, foo.town town1_ inner join foo.realty_realty_type realty type2_ on realty0_.id=realtytype2_.realty_id inner join foo.realty_type realtytype3_ on realtytype2_.realty_type_id=realtytype3_.id where realty0_.town_id=town1_.id and realty0_.valid=? order by realty0_.area ASC


Classes are Realty and RealtyType. Mapping in Realty to realtyTypes is:
Code:
@ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
    @JoinTable(name="realty_realty_type", catalog="foo", joinColumns = {
        @JoinColumn(name="realty_id", nullable=false, updatable=false) }, inverseJoinColumns = {
        @JoinColumn(name="realty_type_id", nullable=false, updatable=false) })
    public Set<RealtyType> getRealtyTypes() {
        return this.realtyTypes;
    }


Mapping in RealtyType to realties is:
Code:
@ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
    @JoinTable(name="realty_realty_type", catalog="foo", joinColumns = {
        @JoinColumn(name="realty_type_id", nullable=false, updatable=false) }, inverseJoinColumns = {
        @JoinColumn(name="realty_id", nullable=false, updatable=false) })
    public Set<Realty> getRealties() {
        return this.realties;
    }


HQL query is:
Code:
SELECT new foo.RealtyWrapper(rty.id, rty.street, rty.postcode, rty.town.name, rty.area, rty.realtyTypes, rty.rental, rty.sale) FROM Realty rty WHERE rty.valid = :valid0 ORDER BY rty.area ASC


So hibernate is forgetting table.column definiton from col_5_0_ definition. Db engine is InnoDB and application is using org.hibernate.dialect.MySQL5InnoDBDialect. What could be the problem?


Top
 Profile  
 
 Post subject: Re: Hibernate creating invalid SQL (MySQL)
PostPosted: Fri Apr 16, 2010 6:02 pm 
Newbie

Joined: Thu Apr 15, 2010 5:55 pm
Posts: 2
Ok, the problem is somehow solved, but still don't get the right results. Now my HQL query is:
Code:
SELECT new foo.RealtyWrapper(rty.id, rty.street, rty.postcode, rty.town.name, rty.area, realtyTypes, rty.rental, rty.sale) FROM Realty AS rty LEFT JOIN rty.realtyTypes AS realtyTypes

The problem is that query is returning only one RealtyType per Realty and I would like it to return Set<RealtyType> (all the realtyTypes of Realty). So the current query is working with constructor:
Code:
public RealtyWrapper(Integer id, String street, String postcode, String townName, Double area, RealtyType realtyType, boolean rental, boolean sale)

but how to make a HQL query to return right objects for:
Code:
public RealtyWrapper(Integer id, String street, String postcode, String townName, Double area, Set<RealtyType> realtyTypes, boolean rental, boolean sale)

I know how to make that possible with more complex HQL or native SQL queries, but is it possible for Hibernate / HQL to do the magic for me with a one simple query?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.