Hibernate version:
Hibernate 3.1.2
Mapping documents:
Not provided, mostly a syntax question, however they
are Hibernate 2.0 mapping files.
Hey guys, quick use question. I've ran into a problem attempting to use a join table (many-2-many) in createSQLQuery when building relationships.
To illustrate the problem I'm going to use the box -> ball analogy; that is box is mapped to ball via a many-2-many table (ball_for_box).
An attempt to load a box and its collection of balls using the many-2-many table and createSQLQuery could look as follows:
Code:
SQLQuery query = sess.createSQLQuery("select {box.*}, {ball.*}, {joinTable.*} from Box box left join Ball_For_Box {joinTable} on joinTable.box_id = box.box_id left join Ball ball on joinTable.ball_id = ball.ball_id
query.addEntity("box", Box.class);
query.addEntity("ball", Ball.class);
query.addJoin("joinTable, "box.balls");
query.list();
The above code
works in Hibernate 3.0.4, however appears to fail in the newest version:
Code:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at org.hibernate.loader.custom.SQLQueryParser.substituteBrackets(SQLQueryParser.java:133)
at org.hibernate.loader.custom.SQLQueryParser.process(SQLQueryParser.java:85)
at org.hibernate.loader.custom.SQLCustomQuery.<init>(SQLCustomQuery.java:157)
at org.hibernate.engine.query.NativeSQLQueryPlan.<init>(NativeSQLQueryPlan.java:21)
at org.hibernate.engine.query.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:113)
at org.hibernate.impl.AbstractSessionImpl.getNativeSQLQueryPlan(AbstractSessionImpl.java:140)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:147)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:164)
...
I saw no code demonstrating the use of a join table (In this case "Ball_For_Box") when building relationships in createSQLQuery anywhere. Any help is appreciated!
Note, the following code gives the same exception:
Code:
SQLQuery query = sess.createSQLQuery("select {box.*}, {ball.*}, joinTable.* from Box box left join Ball_For_Box joinTable on joinTable.box_id = box.box_id left join Ball ball on joinTable.ball_id = ball.ball_id
query.addEntity("box", Box.class);
query.addJoin("ball", "box.balls");
query.list();
Thanks in advance