Ondra Žižka Ondra Žižka - 24 days ago 5
Java Question

JPA/Hibernate: How to use INSERT JPQL?

This JPQL

em.createQuery("INSERT INTO Count (id, count) SELECT 1, ?").setParameter(1, id).executeUpdate();


throws

org.hibernate.hql.internal.ast.QuerySyntaxException: expecting OPEN, found ')' near line 1, column 29 [INSERT INTO Count (id, count) SELECT 1, ?]


How should the query look like?

Does it matter that I use JPA API but HQL actually? JPQL doesn't support
INSERT
.

Using Hibernate 4.1.6 in JBoss AS 7.1.2.

Reference: http://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html/ch11.html#d5e2637

Answer

Since count is a reserved word, adding square brackets around it like [count] will work.