Search code examples
mysqlhibernatejoinnativequery

Hibernate native query with multiple joins on same table returning wrong results


I am using a native sql query where I have a players table to which I join three times, first to get the batsman name, then to get bowler name and then to get the fielder name. Now the first join works, but the next two also return the same name i.e the batsman name.

Here is the sql query

 select 
    del.over_no , 
    del.delivery_no , 
    batsman.sname , 
    outType.name , 
    outBy.sname , 
    fielder.sname , 
    bep.runs, 
    bep.deliveries, 
    bep.fours, 
    bep.sixes

    from delivery del 
    INNER JOIN batsman_performance bep ON del.innings_id=bep.innings_id 
    INNER JOIN ref_player batsman ON del.batsman_id = batsman.id
    INNER JOIN ref_player outBy ON del.bowler_id = outBy.id
    LEFT OUTER JOIN ref_player fielder ON del.fielder_id1= fielder.id
    INNER JOIN ref_out_type outType ON del.out_type_id=outType.id
    and del.out_type_id IS NOT NULL 
    and del.innings_id=:innings_id 
    and bep.player_id = del.batsman_id
    order by over_no, delivery_no;

I am not using aliases for the selected columns because when i did, hibernate threw an exception for whichever column I use an alias

Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query] with root cause java.sql.SQLException: Column 'over_no' not found.

This query is working when I run it on my mysql client and returns the correct dataset but when I run it in my code, the result set somehow overrides the two subsequent joins on ref_player table, leaving me with the batsman name in all three columns, i.e same name in batsman.sname, outBy.sname and fielder.sname columns.

I am stuck here for the last two days, Please any help would be great.


Solution

  • its a pending issue on the Hibernate bug tracking.

    See this ticket

    also in hibernate forums. This clearly shows this is bug a hibernates end.

    See Hibernate Forum Discussion