hibernate中使用SQLQuery处理一些hibernate不方便处理的有关问题(表外连接)

hibernate中使用SQLQuery处理一些hibernate不方便处理的问题(表外连接)

一般表外链接hibernate是不建议并且支持不好的,所以尽量使用SQLQuery处理即可。方式如下:

 

	public List<Long> getNotNotifyOrders(){
		return (List<Long>)getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuffer sql = new StringBuffer("select o.`ID` from `order` o left join `notify` n on o.`ID`=n.`ORDER_ID` where n.`ID` is null);
				SQLQuery q = session.createSQLQuery(sql.toString()).addScalar("ID",Hibernate.LONG);
//如果配置有hibernate缓存,那么这里不设false的话会有缓存问题,必须注意
				q.setCacheable(false);
				return q.list();
			}
		});
	}