使用DISTINCT时,MSSQL使用Hibernate语法无效?

时间:2016-01-28 08:17:52

标签: java sql-server hibernate

所以我有这段Java代码:

final Query query = session.createSQLQuery("SELECT DISTINCT(expense_document.id) FROM expense_document JOIN generic_object ON expense_document.id = generic_object.id JOIN expense_document_item ON expense_document_item.document_id = expense_document.id JOIN generic_object ON expense_document_item.id = generic_object.id WHERE expense_document.client_id = :client_id").setParameter("client_id", client.getId()).setMaxResults(1000);

当执行此代码时,我得到:

org.hibernate.exception.SQLGrammarException: could not execute query
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'from'.

我无法找到MS SQL对此查询不喜欢的内容。当我连接到MySQL时,这一行不会引起任何问题。

2 个答案:

答案 0 :(得分:1)

尝试将参数的括号移除到select distinct something from somewhere 关键字。

像这样使用:

//arrows
svg.append("defs").selectAll("marker")
    .data(["suit", "licensing", "resolved"])
  .enter().append("marker")
    .attr("id", function(d) { return d; })
    .attr("viewBox", "0 -5 10 10")
    .attr("refX", 25)
    .attr("refY", 0)
    .attr("markerWidth", 8)
    .attr("markerHeight", 8)
    .attr("orient", "auto")
  .append("path")
    .attr("d", "M0,-5L10,0L0,5 L10,0 L0, -5")
    .style("stroke", "#4679BD")
    .style("opacity", "0.6");

//onTick
force.on("tick", function () {
    link.attr("x1", function (d) {
        return d.source.x;
    })
        .attr("y1", function (d) {
        return d.source.y;
    })
        .attr("x2", function (d) {
        return d.target.x;
    })
        .attr("y2", function (d) {
        return d.target.y;
    });
    d3.selectAll("circle").attr("cx", function (d) {
        //return d.x;
        return d.x = Math.max(radius, Math.min(width - 10, d.x));
    })
        .attr("cy", function (d) {
        return d.y = Math.max(radius, Math.min(height - 10, d.y));
        //return d.y;
    });
    d3.selectAll("text").attr("x", function (d) {
        return d.x;
    })
        .attr("y", function (d) {
        return d.y;
    });
    node.each(collide(5.0));    //collision detection
});

答案 1 :(得分:1)

您的查询看起来很好..您使用MSSQL的最新驱动程序吗?

有关支持的SQL Server驱动程序,请参阅how to configure hibernate config file for sql server

你应该可以这样做:

final Query query = session.createSQLQuery("SELECT DISTINCT(expense_document.id) FROM expense_document JOIN generic_object ON expense_document.id = generic_object.id JOIN expense_document_item ON expense_document_item.document_id = expense_document.id JOIN generic_object ON expense_document_item.id = generic_object.id WHERE expense_document.client_id = :client_id").setParameter("client_id", client.getId()).setMaxResults(1000);

final Query query = session.createSQLQuery("SELECT DISTINCT expense_document.id FROM expense_document JOIN generic_object ON expense_document.id = generic_object.id JOIN expense_document_item ON expense_document_item.document_id = expense_document.id JOIN generic_object ON expense_document_item.id = generic_object.id WHERE expense_document.client_id = :client_id").setParameter("client_id", client.getId()).setMaxResults(1000);

但是,如果您正在使用hibernate,为什么不使用HQL,JPA查询语言或Criteria?这应该确保您不必根据供应商更改SQL语法,因为它们在不同风格上略有不同。我觉得如果你在不同的数据库之间切换,使用JPA和实体管理器可能是前进的方法,因为 createSqlQuery 会将字符串作为本机SQL发送给供应商。

  

Hibernate EntityManager实现编程接口和   生命周期规则,由JPA 2.0规范定义   https://docs.jboss.org/hibernate/entitymanager/3.6/reference/en/html_single/

如果您正在使用类似hibernate之类的东西,那么当您想要设置提示时,createSqlQuery非常适合发送特定于供应商的查询。