0

我有以下 Sybase 查询,

select *
  from dbo.translation_style_sheet t1
 where t1.create_date = (select max(t2.create_date) 
                           from dbo.translation_style_sheet t2
                          where t1.file_name = t2.file_name);

我正在尝试将其转换为休眠条件查询,但无法弄清楚。我假设我需要使用 DetachedCriteria 来处理这个问题,但不确定如何使用它。

这就是我迄今为止所拥有的。

DetachedCriteria maxCreateDate = DetachedCriteria.forClass(TranslationStyleSheet.class, "translationStyleSheet2")
    .setProjection( Property.forName("createDate").max() )
    .add( Property.forName("translationStyleSheet2.fileName").eqProperty("translationStyleSheet.fileName") );

List<TranslationStyleSheet> translationStyleSheets = this.session.createCriteria(TranslationStyleSheet.class, "translationStyleSheet")
            .add( Property.forName("createDate").eq(maxCreateDate))
            .list();

我收到以下异常。

org.hibernate.exception.GenericJDBCException
could not execute query

SQL
    select this_.translation_style_sheet_id as translat1_20_0_, this_.create_date as create2_20_0_, this_.description as descript3_20_0_, this_.file_content as file4_20_0_, this_.file_extension as file5_20_0_, this_.file_name as file6_20_0_, this_.file_size as file7_20_0_, this_.style_sheet_content as style8_20_0_, this_.style_sheet_type as style9_20_0_ from translation_style_sheet this_ where this_.create_date = (select max(translationStyleSheet2_.create_date) as y0_ from translation_style_sheet translationStyleSheet2_ where translationStyleSheet2_.file_name=this_.file_name)
SQLState
    ZZZZZ

有人知道我做错了什么吗?

更新

错误似乎发生在 max(translationStyleSheet2_.create_date) as y0_ as。当我在 sql 语句中删除 as y0_ 时,我可以运行查询,但是我不确定如何在休眠条件中修复它。

4

1 回答 1

1

因此,我没有成功地将其用作条件查询,但我确实成功地将其用作 HQL 查询。

HQL 解决方案

this.session.createQuery("from TranslationStyleSheet this "
                + "where this.createDate = (select max(translationStyleSheet2.createDate) "
                + "from TranslationStyleSheet translationStyleSheet2 "
                + "where translationStyleSheet2.fileName=this.fileName)")
                .list();

我仍然有兴趣让查询作为条件查询工作。

于 2012-10-29T18:00:51.757 回答