Hibernate version: 3.1.2
Salut, je souhaite faire une suppression d'entités (en masse) en utilisant la fonctionnalité de bulkUpdate. Mon problème est que je n'arrive pas exprimer ma requête pour parvenir à mes fins, à chaque fois j'ai une erreur de syntaxe !
Voici le mapping:
Code:
<class name="Job" table="jobs" >
....
<map name="stateDates" table="jobstates" lazy="false" sort="unsorted">
<key column="jobid" />
<index type="StateEnum" column="state"/>
<element column="statedate" type="timestamp" not-null="true"/>
</map>
<property name="state" type="StateEnum" access="field" column="activestate"/>
<join table="joblogs">
<key column="jobid"/>
<property name="logData" type="org.springframework.orm.hibernate3.support.ClobStringType" column="logs" lazy="true" />
</join> -->
<one-to-one name="logData" lazy="proxy" cascade="all" constrained="true"/>
...
</class>
<class name="LogData" table="joblogs" >
<id name="id" column="jobid"/>
<property name="data" type="org.springframework.orm.hibernate3.support.ClobStringType" column="logs" lazy="true" not-null="false"/>
</class>
La classe Job est mappée à 3 tables: jobs(id,activestate....), jobstates(jobid,state,statedate) et jologs(jobid,logdata).
En fait je souhaite réaliser l'opération suivante:
"Supprimer tous les Jobs dont l'état actif (activestate donne l'état courant du Job) est moins récent qu'une date donnée."
J'ai donc essayé le code suivant:
Date beforeDate = ...;
Session session = this.getSession();
int purgedJobsCount = session.createQuery("
delete from Job j where j.stateDates[j.state] < :beforeDate").setTimestamp("beforeDate",beforeDate).executeUpdate();
Voici le SQL généré qui au passage n'est pas correct pour MySQL :-(:
delete from jobs, jobstates statedates1_ where statedates1_.statedate<?
Et la stacktrace:
org.hibernate.exception.SQLGrammarException: could not execute update query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:65)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:84)
....
Caused by: java.sql.SQLException: 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 statedates1_.statedate<'2006-04-25'' at line 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2926)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2978)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2902)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:933)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1162)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1079)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1064)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:101)
at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:75)
... 32 more
Le code SQL généré est pas vraiment ce que j'attends.
J'ai essayé différentes syntaxes mais sans succès .
Bref j'ai besoin d'aide,
d'avance merci