如何编写hibernate模板查询从sql查询? [英] How to write hibernate template query from the sql query?
问题描述
实际上,我有一个SQL查询使用 mySql
数据库运行非常好。但是,我的问题是我想在 hibernatetemplate
查询中编写该查询。
我的SQL QUERY IS
SELECT * FROM task,dependency WHERE DEPENDENCY_From IN(10,11,12,13)
,我希望它由hibernate模板执行。
这到一个
HibernateTemplate
查询?解决方案code> HibernateTemplate 查询下面是hibernate查询的示例,但在您的情况下,我们需要映射文件来查看实体如何关联。希望以下查询将会帮助你。
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;
列表< City> cityList = getHibernateTemplate()。find(query);
Actually I had one SQL query that runs very fine with
mySql
database. but, my problem is that I want to write that query inhibernatetemplate
query.My SQL QUERY IS
SELECT * FROM task, dependency WHERE DEPENDENCY_From IN(10,11,12,13)
and I want it to executed by the hibernate template.
How do I to convert this to a
HibernateTemplate
Query?解决方案If you want to use
HibernateTemplate
query then below is example of hibernate query but in your case, we need mapping files to see how the entities are associated. Hope below query will help you.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; }
or
String query="from City"; List<City> cityList = getHibernateTemplate().find(query);
这篇关于如何编写hibernate模板查询从sql查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!