Version 7

    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 (HHH-2697), Hibernate supports escaping the colon char not to treat it as a parameter:

     

    Query q = em.createNativeQuery("SELECT @a \\:= 1, @b \\:= 2, @a + @b");
    

     

    That works.

     

    Note that The double backslash is to escape Java string; Hibernate sees it as single \ (in case you used it in some external resource or so).