[jboss-dev-forums] [JBoss AS 7 Development] - How to use MySQL assignment operator (:=) and variables in Hibernate NativeQuery

Ondrej Zizka do-not-reply at jboss.com
Sun Jul 29 09:44:53 EDT 2012


Ondrej Zizka [https://community.jboss.org/people/ozizka] created the document:

"How to use MySQL assignment operator (:=) and variables  in Hibernate NativeQuery"

To view the document, visit: https://community.jboss.org/docs/DOC-19002

--------------------------------------------------------------
Formerly, when using the assignment operator in Native Query, Hibernate threw an exception:

Caused by: org.hibernate.QueryException: Space is not allowed after parameter prefix ':' [ INSERT INTO poh_users_stats_daily (id_user, day, watch_patterns, watch_subjects, watch_merged) SELECT u.id_user, CURRENT_DATE() AS day,   @a :=  (SELECT COUNT(*) FROM poh_users_vzory AS v  WHERE v.id_user = u.id_user) AS hlid_vz,   @b :=  (SELECT COUNT(*) FROM poh_users_sled_osoby AS ho WHERE ho.id_user = u.id_user) AS hlid_os,   @a + @b AS soucet FROM isir_sled_users AS u LEFT OUTER JOIN poh_users_stats_daily AS s ON u.id_user = s.id_user AND `day` = CURRENT_DATE() WHERE s.id_user IS NULL]
        at org.hibernate.engine.query.spi.ParameterParser.parse(ParameterParser.java:95) [hibernate-core-4.1.5.SP1.jar:4.1.5.SP1]
        at org.hibernate.engine.query.spi.ParamLocationRecognizer.parseLocations(ParamLocationRecognizer.java:75) [hibernate-core-4.1.5.SP1.jar:4.1.5.SP1]
        at org.hibernate.engine.query.spi.QueryPlanCache.buildParameterMetadata(QueryPlanCache.java:138) [hibernate-core-4.1.5.SP1.jar:4.1.5.SP1]
        at org.hibernate.engine.query.spi.QueryPlanCache.getSQLParameterMetadata(QueryPlanCache.java:131) [hibernate-core-4.1.5.SP1.jar:4.1.5.SP1]
        at org.hibernate.internal.AbstractSessionImpl.createSQLQuery(AbstractSessionImpl.java:209) [hibernate-core-4.1.5.SP1.jar:4.1.5.SP1]
        at org.hibernate.internal.SessionImpl.createSQLQuery(SessionImpl.java:1742) [hibernate-core-4.1.5.SP1.jar:4.1.5.SP1]
        at org.hibernate.ejb.AbstractEntityManagerImpl.createNativeQuery(AbstractEntityManagerImpl.java:730) [hibernate-entitymanager-4.1.5.SP1.jar:4.1.5.SP1] 


Actually, this will still happen but as of 4.1.3, Hibernate supports excaping the colon char not to treat it as a parameter:

SELECT @a \\:= 1, @b \\:= 2, @a + @b


That works.
--------------------------------------------------------------

Comment by going to Community
[https://community.jboss.org/docs/DOC-19002]

Create a new document in JBoss AS 7 Development at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2225]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-dev-forums/attachments/20120729/f47eac79/attachment.html 


More information about the jboss-dev-forums mailing list