15

I'm wondering if it is possible to execute several semicolon-separated SQL update queries with one SQLQuery#executeUpdate() call in Hibernate (3.2).

I have string containing multiple updates like this:

String statements = "UPDATE Foo SET bar=1*30.00 WHERE baz=1; 
                     UPDATE Foo SET bar=2*45.50 WHERE baz=2;...";

I'm trying to do

 Query query = session.createSQLQuery(statements);
 query.executeUpdate();

But keep getting the following error

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 
You have an error in your SQL syntax; check the manual that corresponds to 
your MySQL server version for the right syntax to use near 
'UPDATE Foo SET bar=Y WHERE ' at line 3

If I execute the contents of the statements by hand I get no errors so I'm assuming the multiple semicolon-separated queries are causing trouble somewhere in Hibernate or JDBC.

Is it better to just split the statementsstring and do createSQLQuery(statement).executeUpdate() individually for each line?

1
  • The example code was a bit off, in reality I'm calculating some amounts to bar, they should be OK without quotes, right? Commented Jul 18, 2011 at 14:35

3 Answers 3

6

An alternative is to use a CASE statement. For example:

UPDATE Foo
    SET bar = 
    CASE baz
       WHEN 1 THEN 'X'
       WHEN 2 THEN 'Y'
    END CASE
WHERE baz in (1,2)
Sign up to request clarification or add additional context in comments.

Comments

2

What you can do is use batch statements, basically before you execute your query call the addBatch(string query); method, then when all your queries are in the batch just call the executeBatch() method. That will perform them all for you in the order you added the mto the batch.

also: the error in your syntax is you need put in quotes 'Y' or 'X'

6 Comments

addBatch() and executeBatch are from java.sql.Statement, but the poster uses Hibernate.
If I understand correctly, he referred to JDBC as a probable cause of the problem (for example, the thrown exception belongs to the JDBC connector), not as if he would accept a solution in JDBC. Janne could clear it out if JDBC is acceptable.
Nicolae is right, this isn't really the answer I am looking for. I still want to use Hibernate's SQLQuery
@Janne, im sorry I guess I didn't read the question as well as I thought, but still if you are using JDBC this is a good solution so I will not remove it because, others could learn from it. Plus it's a viable solution.
@Janne I was looking at the HIBERNATE api, and there is something very similar to what I had suggested(batch) docs.jboss.org/hibernate/core/3.2/api/org/hibernate/jdbc/…
|
1

well you have to at least embrace X and Y with single quotes, I'd say.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.