Pergunta

Por que a seguinte consulta HQL falha?

string hql = @"delete MyLog log
               where
                    log.UtcTimestamp < :threshold and
                    log.Configuration.Application = :application";

session.CreateQuery(hql)
       .SetDateTime("threshold", threshold)
       .SetEnum("application", this.application)
       .ExecuteUpdate();

A mesma forma de consulta funciona quando usada em uma seleção:

string hql = @"from MyLog log
               where
                    log.UtcTimestamp < :threshold and
                    log.Configuration.Application = :application";
IList<MyLog> log = session.CreateQuery(hql)
    .SetDateTime("threshold", threshold)
    .SetEnum("application", this.application)
    .List<MyLog>();

O mapeamento para o MyLog contém:

References(x => x.Configuration)
     .Columns("CONFIGURATION_ID")
     .ReadOnly();      

O mapeamento para configuração contém:

Map(x => x.Application, "APPLICATION_ID");

O erro que recebo é:

Exclua do myLog, configuração COVERCON1_ onde utc_timestamp <: p0 e application_id =: p1; : p0 = 04/10/2010 17:15:52 ,: p1 = 7

Nibernate.Exceptions.GenericadoException: Não foi possível executar a atualização da consulta [SQL:

Exclua do myLog, Configuration COVERCON1_ Onde UTC_TIMESTAMP <? e Application_id =?

] ---> oracle.dataaccess.client.oracleException: ORA-00933: Comando SQL não terminou corretamente

Foi útil?

Solução

Experimente isso:

delete MyLog log
where log.id in
          (select l.id
           from MyLog l
           where l.UtcTimestamp < :threshold and
           and.Configuration.Application = :application)

Outras dicas

Do link enviado por Rafael acima:

http://docs.jboss.org/hibernate/stable/core/reference/en/html/batch.html#batch-direct

Nenhuma junção, implícita ou explícita, pode ser especificada em uma consulta HQL a granel. Sub-quilyes podem ser usadas na cláusula onde as próprias subconsivas podem conter junções

A sintaxe é DELETE FROM MyLog ....

Tenha em mente que o HQL Exclete não homenageia as cascatas definidas com os mapeamentos (n) hibernados.

Assim, você pode selecionar todas as entidades e excluí -las uma a uma.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top