【问题标题】:How to write hibernate template query from the sql query?如何从 sql 查询中编写休眠模板查询?
【发布时间】:2012-01-26 15:06:06
【问题描述】:

实际上,我有一个 SQL 查询在 mySql 数据库中运行得非常好。但是,我的问题是我想在 hibernatetemplate 查询中编写该查询。

我的 SQL 查询是

SELECT * FROM task, dependency WHERE DEPENDENCY_From IN(10,11,12,13)

我希望它由休眠模板执行。

如何将其转换为 HibernateTemplate 查询?

【问题讨论】:

标签: java mysql database hibernate


【解决方案1】:

HQL 查询

Object[] params  = {cid,cid};

List list=getHibernateTemplate().find("select S.stateName,C.countryName from State S,Country C where C.countryId=? and S.countryId=?", params);

SQL 查询

select S.stateName,C.countryName from State S,Country C where S.countryId=C.countryId;

【讨论】:

    【解决方案2】:

    如果您想使用HibernateTemplate 查询,那么下面是休眠查询的示例,但在您的情况下,我们需要映射文件来查看实体是如何关联的。希望以下查询对您有所帮助。

    public List<Object[]> getCities(Integer stateId)  {
            List<Object[]> cityList = new ArrayList<Object[]>();
            String query = "select city.cityId,city.cityName from City city where city.state.stateId=?";
            Object[] queryParam = {stateId};
            cityList = getHibernateTemplate().find(query, queryParam);
            return cityList;
        }
    

    String query="from City";
    List<City> cityList = getHibernateTemplate().find(query);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-13
      • 2010-09-24
      • 1970-01-01
      • 2014-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多