Question

I am trying to sort by transient property but fails with SQL Error: Invalid column name error .

Pls find below my code :

In domain class declared :

static transients = ['sortCandidateLastName']

Query which I am trying to execute:

When I am trying to run the below query in Oracle :It runs fine

( 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 code:

<g:sortableColumn property="sortCandidateLastName" title="Sort By Candidate Last Name" />

But when Hibernate is trying to read it ,it throws Invalid column name : ResultSet.getInt(clazz_)

Était-ce utile?

La solution

Transient properties are not persisted so it's impossible to write a query which sorts by a transient property. If you retrieve a list of objects from a query and want to sort them by a transient property, you'll have to do it in Groovy code, e.g.

// 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 }

Autres conseils

You cant sort on transient field. There is no actual database column for a transient field! So your sql would always throw an error!

I tried using jdbcTemplate and gives me the required functionality.

Code snippet:

GSP Layer:

<g:sortable property="sortCandidateLastName">
                    <g:message code="booking.alphabetical.label" default="Alphabetical(A-Z)" />
</g:sortable>

Domain layer:

just defined a transient property :

static transients = ['sortCandidateLastName']

Reason for adding the transient field : SO that it doesn't throw me any exception like missing property.

Controller layer :

if(params.sort == "sortCandidateLastName" )
        {
            bookingCandList= bookingService.orderByCandidateLastName(params.max, params.order,params.offset.toInteger())//Booking.getSortCandidateLastName(params.max, params.order,params.offset.toInteger()) //


        }

Service layer :

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)
       }

Configuration of JDBC template :

// Place your Spring DSL code here
import org.springframework.jdbc.core.JdbcTemplate

beans = {
..........
    jdbcTemplate(JdbcTemplate) {
        dataSource = ref('dataSource')
     }
}

Hope it helps to ..

Cheers

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top