【问题标题】:Invalid column name error无效的列名错误
【发布时间】:2014-04-09 10:04:20
【问题描述】:

我正在尝试按瞬态属性排序,但由于 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)

普惠制代码:

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

但是当 Hibernate 试图读取它时,它会抛出 Invalid column name : ResultSet.getInt(clazz_)

【问题讨论】:

    标签: grails grails-orm


    【解决方案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 }
    

    【讨论】:

      【解决方案2】:

      您不能对瞬态字段进行排序。瞬态字段没有实际的数据库列!所以你的sql总是会报错!

      【讨论】:

      • 我在我们的项目中有一些使用瞬态属性的工作示例。但是在这种特殊情况下,查询在休眠层中失败,说无效的列名。
      【解决方案3】:

      我尝试使用 jdbcTemplate 并为我提供了所需的功能。

      代码sn-p:

      GSP 层:

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

      域层:

      刚刚定义了一个瞬态属性:

      静态瞬态 = ['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)
             }
      

      JDBC模板的配置:

      // Place your Spring DSL code here
      import org.springframework.jdbc.core.JdbcTemplate
      
      beans = {
      ..........
          jdbcTemplate(JdbcTemplate) {
              dataSource = ref('dataSource')
           }
      }
      

      希望对你有所帮助..

      干杯

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-03
        • 2020-02-09
        相关资源
        最近更新 更多