To workaround this situation I created sql server dialect modification:
Code:
import org.hibernate.QueryException;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.dialect.function.SQLFunctionTemplate;
import org.hibernate.engine.Mapping;
import org.hibernate.type.Type;
public class AdvancedSQLServerDialect extends SQLServerDialect {
public AdvancedSQLServerDialect() {
registerFunction("firstrow", new SQLFunctionTemplate(null, " top 1 ?1 ") {
@Override
public Type getReturnType(Type columnType, Mapping mapping) throws QueryException {
return columnType;
}
});
}
}
Well, I don't feel this is how it should be, but it faster than before, more readable and can be expressed in hql rather than create sql just to be able to use subselect properly. Like this:
Code:
select
item.name,
(select firstrow(r.remarkText) from Remark r inner join r.item i where i = item order by r.creationDate desc, r.id desc)
from Item item
I don't understand why HQL doesn't support it directly by the syntax. Does anybody have the comments on how it is implemented and/or ideas what can be the better solution?[/code]