Hi all,
I´m using Hibernate 3.2.5
In my code I tried to delete all the Messagens sent by some user.
getSession().createQuery("delete Mensagem c where c.usuarioRecebeu.login = ?").setParameter(0, usuario).executeUpdate();
"usuarioRecebeu" is the User association, and represents the user that received that message.
The hibernate showSql=true printed:
Code:
Hibernate: delete from mensagem, usuario usuario1_ where login=?
It´s strange because it did not do the join between Message and User.
So in the logs, I got the following exception:
Code:
org.hibernate.exception.SQLGrammarException: could not execute update query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:84)
at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:396)
at org.hibernate.engine.query.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:259)
at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1141)
at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:94)
at net.wasys.wafv.chat.server.dao.MensagemDAO.deletaMensagemsRecebidasByUsuario(MensagemDAO.java:26)
at net.wasys.mlife.actions.DeletaMensagensAction.execute(DeletaMensagensAction.java:27)
at net.wasys.mlife.actions.framework.DecoratorTransactionAction.execute(DecoratorTransactionAction.java:33)
at net.wasys.wafv.chat.server.moblife.ActionHelper.execute(ActionHelper.java:31)
at net.wasys.wafv.chat.server.moblife.MobiLifeSocketServerThread.run(MobiLifeSocketServerThread.java:56)
Caused by: com.mysql.jdbc.exceptions.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 'where login='ricardo'' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2941)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3249)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1268)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1541)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1455)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1440)
I tried the same HQL to select the data and it works very well:
Code:
List list = getSession().createQuery("from Mensagem c where c.usuarioRecebeu.login = ?").setParameter(0, usuario).list();
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Mensagem msg = (Mensagem) iterator.next();
System.out.println(msg.getMsg());
}
The hibernate showSQL=true now prints the SQL and the join:
Quote:
Hibernate: select mensagem0_.id as id7_, mensagem0_.usuario_id as usuario2_7_, mensagem0_.usuario_contato_id as usuario3_7_, mensagem0_.msg as msg7_, mensagem0_.service as service7_, mensagem0_.data as data7_, mensagem0_.status as status7_, mensagem0_.rsId as rsId7_, mensagem0_.rsId_contato as rsId9_7_, mensagem0_.chat as chat7_ from mensagem mensagem0_, usuario usuario1_ where mensagem0_.usuario_contato_id=usuario1_.id and usuario1_.login=?
My Mensagem.hbm.xml mapping:
Code:
<class
name="net.wasys.wafv.chat.server.model.Mensagem"
proxy="net.wasys.wafv.chat.server.model.Mensagem"
table="mensagem"
>
<id
name="id"
type="java.lang.Long"
column="id"
>
<generator class="native" />
</id>
<many-to-one
name="usuarioEnviou"
class="net.wasys.wafv.chat.server.model.Usuario"
column="usuario_id"
not-null="true"
cascade="none"
/>
<many-to-one
name="usuarioRecebeu"
class="net.wasys.wafv.chat.server.model.Usuario"
column="usuario_contato_id"
not-null="true"
cascade="none"
/>
....
Since the same HQL works to retrieve the information, why I can´t delete the data with the same HQL, but using the executeUpdate ?
thank you,
Ricardo