Hibernate supports SQLQuery (sql statements related to specific database platforms) and Query (hql) query mode, as well as Criteria object query mode.
SQLQuery
1. Query results are mapped to map,: param parameterization
public List<Map<String, Object>> findMapBySql(String sql, Map<String, Object> params) {
SQLQuery sqlQuery = this.getCurrentSession().createSQLQuery(sql);
sqlQuery=getSqlQueryByMap(sqlQuery,params);
return sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
}
public SQLQuery getSqlQueryByMap(SQLQuery sqlQuery,Map<String,Object> params){
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
Object obj = params.get(key);
if (obj instanceof Collection<?>)
sqlQuery.setParameterList(key, (Collection<?>) obj);
else if (obj instanceof Object[])
sqlQuery.setParameterList(key, (Object[]) obj);
else
sqlQuery.setParameter(key, obj);
}
}
return sqlQuery;
}
The data returned is a list of map types, and the parameters are passed through map. The parameter types are: param;
Call examples:
String sql="select id,user_name,create_date_time from tbl_user where code=:code"
Map<String,Object> params=new HashMap<>();
params.put("code","test");
List<Map<String,Object>> users=this.findMapBySql(sql,params);
If you need to translate into a collection of entities, you need to deal with it yourself.
2. Query results are mapped to entities,: param parameterization
public <T> List<T> find(String sql, Map<String,Object> params, Class<T> clazz) {
SQLQuery query = this.getCurrentSession().createSQLQuery(sql);
query=getSqlQueryByMap(query,params);
query.setResultTransformer(Transformers.aliasToBean(clazz));
return query.list();
}
The data returned is a list of entities. The parameters are passed through map. The parameter type is param. It should be noted that the database field must correspond to the mapping entity field one by one, and there is a getter/setter method, otherwise the error will be reported.
Call examples:
String sql="select id,user_name,create_date_time from tbl_user where code=:code"
Map<String,Object> params=new HashMap<>();
params.put("code","test");
List<User> users=this.find(sql,params,User.class);
User entities need to have user_name,create_date_time attribute definitions, rather than userName, create DateTime, which can of course be solved by setting aliases;
There is also a way to get a list of entities
public <T> List<T> findBySql(String sql, Class<T> clazz) {
SQLQuery sqlQuery = this.getCurrentSession().createSQLQuery(sql);
sqlQuery.addEntity(clazz);
return sqlQuery.list();
}
The following way is to get the list of entities. For example, SQL - > user_name will map to entity - > userName. Note that @Transient entities will not be assigned, and the corresponding query results will not be injected into entity.
3. Query results are mapped to maps or entities. Biography
public List findMapBySql(String sql, Object[] params, Type[] types, Class clazz) {
SQLQuery query = this.getCurrentSession().createSQLQuery(sql);
if (clazz != null) {
query.setResultTransformer(Transformers.aliasToBean(clazz));
} else {
query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
}
query.setParameters(params, types);
return query.list();
}
The returned data is a list. According to the clazz parameter, the returned map type list or entity list can be determined. The parameter is used? Placeholders, such as in, need to be replaced by more than one?
Call examples:
String sql="select id,user_name,create_date_time from tbl_user where code like ? and id in (?,?,?)"
Object[] params=new Object[]{"%test%",1,3,5};
Type[] types=new Type[]{Hibernate.String,Hibernate,Integer,Hibernate.Integer,Hibernate.Integer};
List<Map<String,Object>> users=this.findMapBySql(sql,params,types,null);
Query
By querying data through HQL statements, the query results are the same as those of SQLQuery type:
- Support setResultTransformer to map query results to entity or map lists
- Support through setParameters (? Reference)
- setParameter/setParameterList (: param param)
- AddiEntity mapping to entities is not supported (because hql is bound to entities in this article)
In addition, Hiberante query supports Criteria and Example.