方法: public List querySql(String sql) {
  //hql风格
  //String sql = "select tableClassA.filed1 from tableClassA union select tableClassB.filed1 from tableClassB"
  //hql风格
  //String sql = "select new map(tableClassA.filed1) from tableClassA union select new map(tableClassB.filed1) from tableClassB"
//原生sql
 String sql = "select filed1 from tableA union select filed1 from tableB"
List list = this.getSession().createSQLQuery(sql).list();
return list; }错误提示:

org.hibernate.QueryException: addEntity() or addScalar() must be called on a sql query before executing the query. [select filed1 from tableA union select filed1 from tableB]
at org.hibernate.impl.SQLQueryImpl.verifyParameters(SQLQueryImpl.java:169)总提示我需要addEntity() or addScalar() 可是我并没有参数什么的啊.我要怎么修改啊?

解决方案 »

  1.   

    如需使用到复杂的sql语句..还是建议使用jdbc..还有使用sql就不在是面向对象而是面向表了..所以一些地方不在是写你地pojo而是表的名字和column了.
      

  2.   


    晕 
       //hql风格
          //String sql = "select tableClassA.filed1 from tableClassA "我这样写可以执行出来啊。.....................
    但是我这样写就执行不出来了啊。..........................怎么办啊?5555555555555555555555555555  //String sql = "select tableClassA.filed1 from tableClassA union select tableClassA.filed1 from tableClassA "
      

  3.   

    晕那你写1个我看看啊union的啊hql
      

  4.   

    把你表明或者类名,因为hibernate是面向对象的,不是面向表的……
      

  5.   

    String sql = "select filed1 from tableA union select filed1 from tableB"
    List list = this.getSession().createSQLQuery(sql).addScalar(field1,Hibernate.STRING).list();
            return list;