我尝试按瞬态属性排序,但因SQL错误而失败:列名错误无效。
请在我的代码下面找到:
在声明的域类中:
static transients = ['sortCandidateLastName']
查询我要执行的内容:
当我尝试在Oracle中运行以下查询时:它运行正常
( select * from ( select row_.* ,rownum rownum_ from ( select * from booking b where b.marked_deleted='N' order by (select c.cand_id from candidate c where b.cand_id = c.cand_id) asc ) row_ where rownum <= 15 ) where rownum > 0)
GSP代码:
<g:sortableColumn property="sortCandidateLastName" title="Sort By Candidate Last Name" />
但是当Hibernate尝试读取它时,它会抛出无效的列名:ResultSet.getInt(clazz_)
答案 0 :(得分:1)
瞬态属性不会持久存在,因此无法编写按瞬态属性排序的查询。如果从查询中检索对象列表并希望按瞬态属性对它们进行排序,则必须在Groovy代码中执行此操作,例如。
// an example domain class with a transient property
class Book {
private static Long SEQUENCE_GENERATOR = 0
String isbn
String title
Long sequence = ++SEQUENCE_GENERATOR
static transients = ['sequence']
}
// get a list of books from the DB and sort by the transient property
def books = Book.list()
books.sort { it.sequence }
答案 1 :(得分:0)
你无法对瞬态场进行排序。瞬态字段没有实际的数据库列!所以你的SQL总是会抛出一个错误!
答案 2 :(得分:0)
我尝试使用jdbcTemplate并为我提供了所需的功能。
<g:sortable property="sortCandidateLastName">
<g:message code="booking.alphabetical.label" default="Alphabetical(A-Z)" />
</g:sortable>
刚刚定义了一个瞬态属性:
static transients = ['sortCandidateLastName']
添加瞬态字段的原因:因此它不会抛出任何异常,例如缺少属性。
if(params.sort == "sortCandidateLastName" )
{
bookingCandList= bookingService.orderByCandidateLastName(params.max, params.order,params.offset.toInteger())//Booking.getSortCandidateLastName(params.max, params.order,params.offset.toInteger()) //
}
def jdbcTemplate
public List orderByCandidateLastName(Integer max, String sortOrder,Integer offset) {
println "Inside the getcandidateLastName ${max} :: offset ${offset}"
def sortedList
int minRow = offset
int maxRow = offset+max
String queryStr = " select * from " +
" ( "+
" select row_.* " +
" ,rownum rownum_ " +
" from " +
" ( " +
" select * from booking b where b.item_id= 426 and b.marked_deleted='N' order by " +
" (select c.cand_id from candidate c where b.cand_id = c.cand_id) ${sortOrder} "+
" ) row_ "+
"where rownum <= ${maxRow} " +
") " +
"where rownum > ${minRow}"
return jdbcTemplate.queryForList(queryStr)
}
// Place your Spring DSL code here
import org.springframework.jdbc.core.JdbcTemplate
beans = {
..........
jdbcTemplate(JdbcTemplate) {
dataSource = ref('dataSource')
}
}
希望它有助于......
干杯