Hibernate CreateSQL查询问题

时间:2010-05-06 13:42:20

标签: hibernate

我正在尝试使用内置在createsql函数中的hibernates,但似乎它不喜欢以下查询。

List =hibernateSession.createSQLQuery("SELECT number, location FROM table 
WHERE other_number IN 
(SELECT f.number FROM table2 AS f JOIN table3 AS g on f.number = g.number 
WHERE g.other_number = " + var + ") 
ORDER BY number").addEntity(Table.class).list();

我感觉它来自嵌套的select语句,但我不确定。内部选择在代码中的其他地方使用,它返回结果很好。

这是我对第一张表的映射:

<hibernate-mapping>

    <class name="org.efs.openreports.objects.Table" table="table">

        <id name="id" column="other_number" type="java.lang.Integer">
            <generator class="native"/>
        </id>

        <property name="number" column="number" not-null="true" unique="true"/>
        <property name="location" column="location" not-null="true" unique="true"/>

    </class>

</hibernate-mapping>

和.java

public class Table implements Serializable
{
  private Integer id;//panel_facility
  private Integer number;
  private String location;

  public Table()
  {
  }

  public void setId(Integer id)
  {
    this.id = id;
  }

  public Integer getId()
  {
    return id;
  }

  public void setNumber(Integer number)
  {
    this.number = number;
  }

  public Integer number()
  {
    return number;
  }

  public String location()
  {
    return location;  
  }

  public void setLocation(String location)
  {
      this.location = location;
  }

}

有什么建议吗?

编辑(添加映射)

1 个答案:

答案 0 :(得分:1)

您应该在外部选择中选择ID。 (并且不在查询中使用字符串连接)

使用类似的东西,hibernate会插入正确的列名:

List l = hibernateSession.createSQLQuery("SELECT {t.*} FROM table t
WHERE other_number IN 
(SELECT f.number FROM table2 AS f JOIN table3 AS g on f.number = g.number 
WHERE g.other_number = :var) 
ORDER BY number")
.addEntity("t", Table.class).setParameter("var", actualObject).list();