public Long getMaxOrder(Long parentId) { Query query = this.getSession().createSQLQuery( "select max(ORDERID) from tbl_type where PARENTID =:parentId"); query.setLong("parentId", parentId); List<Object> list = query.list(); Long order = 0l; if (list != null && list.size() > 0) { if (list.get(0)!=null) { order = Long.parseLong(list.get(0).toString()); } } if (order != null && order > 0) { return order + 1; } else { return 1l; } }