Discussions

EJB programming & troubleshooting: regarding sorting using EJB-QL

  1. regarding sorting using EJB-QL (2 messages)

    I want to get the sorted beans based on the parameter (attribute type) I provide to the finder method.

    suppose the function is findSortedBeans(String sortFeild)
    case:1) SELECT OBJECT (o) FROM EMPLOYEE AS o ORDER BY o.employeeid
    case:2) SELECT OBJECT (o) FROM EMPLOYEE AS o ORDER BY =?1

    are both the cases true???
  2. SELECT OBJECT (o) FROM EMPLOYEE AS o ORDER BY o.employeeid

    Output records will be sorted only by "o.employeeid" field

    >> SELECT OBJECT (o) FROM EMPLOYEE AS o ORDER BY =?1
    If perticular realization of EJB-QL parser do not deny this -- may be...

    But J2EE 1.4 in BETTA state now, everything can be changed...
    (Currently, to achieve sorted output some use or BMP with corresponding SQL, or CMP + VIEW)
  3. regarding sorting using EJB-QL[ Go to top ]

    As far as I recall, 'order by' clauses are not allowed in EJB 2.0.
    Most EJB-containers provide container-specific extensions for this purpose.

    The order by clause is added in the EJB 2.1 specification.

    "SELECT OBJECT (o) FROM EMPLOYEE AS o ORDER BY o.employeeid"
    is covered by EJB 2.1 but
    "SELECT OBJECT (o) FROM EMPLOYEE AS o ORDER BY =?1" is not.

    I can't imagine an app server supporting the latter construct.
    I would solve this last problem by using a session bean method like this :

    public Collection findEmployeesOrderedBy( String fieldName ) {
      if ( "fieldOne".equals( fieldName ) ) {
        employeeHome.findEmployeesSortedByFieldOne();
      } else if ( "fieldTwo".equals( fieldName ) ) {
        employeeHome.findEmployeesSortedByFieldTwo();
      } else if ( "fieldThree".equals( fieldName ) ) {
        employeeHome.findEmployeesSortedByFieldThee();
      } else {
        throw UnsupportedOperationException();
      }
    }

    Does anybody got a better solution ?