rogeliog rogeliog - 3 months ago 21x
SQL Question

Hibernate update not working

I am new with hibernate and I was trying to update a mapped object with the following code, but it does not update

factory = config.buildSessionFactory();
session = factory.getCurrentSession();
Transaction t = session.beginTransaction();
String hql = "UPDATE "+tableName+" SET "+columnName+" = '"+columnValue+"' WHERE id ="+id+";";
Query query=session.createSQLQuery(hql);

Am I missing something? It do not crash nor update the record.

NOTE: I am using Hibernate3 and Mysql


You're missing query.executeUpdate();

Also, if you're updating a mapped object I would recommend you to make the changes to the java object, and let Hibernate do the update for you. Or at least use a hql query, not a native one.

Make sure that your persistence.xml file has show_sql set to true and watch the log to see if the update is executed.

<property name="hibernate.show_sql" value="true"/>