使用Hibernate命名查询

 

HQL查询支持将查询所用的HQL语句放入配置文件中,而不是代码中,在Hibernate映射文件的<hibernate-mapping>元素中使用<query>子元素来定义命名查询,这个<query>元素只需指定一个name属性,指定该命名查询的名字 ,如:


<sql-query name="education.listByMisId"> <![CDATA[ SELECT HISTORY.COLLEGE_ID AS id, HISTORY.ATTENDED_START_DATE AS startDate,--起止年月 HISTORY.ATTENDED_END_DATE AS endDate,--起止年月 COALESCE(HISTORY.COLLEGE,'')||' '||COALESCE(HISTORY.MAJOR,'') AS collegeMajor, --院校及系、专业 HISTORY.EDU_LEVEL as eduLevel,--学历 HISTORY.DEGREE AS degree,-- 学位 HISTORY.GRADUATE_DESC AS graduate,--毕(结、肆)业 HISTORY.FULL_TIME_FLAG AS fullTimeFlag--是否全日制 FROM EDBADM.T_ODS_LEARNING_HISTORY HISTORY WHERE 1 = 1 ]]> </sql-query>

String query = getSession().getNamedQuery("education.listByMisId").getQueryString();
QueryBuilder qb = new QueryBuilder(query);
qb.append("需要的一些sql语句");
String sql = qb.getQuery().toString();
最终得到自己想要的sql

 

你可能感兴趣的:(Hibernate)