2015-04-27 6 views
1

Мое приложение работает на все, кроме вызова для отображения членов разных плат. Я могу получить правильный вывод в db с SQL-запросом, но у вас проблемы с его использованием в Grails, используя createCriteria.grails multiple table criteria

Необходимо использовать Oracle 11g как моя БД. Grails 2.3.3 как DB, так и Grails являются локальными.

Вот мои домены

class Trustee { 

    String salutation 
    String firstName 
    String middleName 
    String lastName 

    static hasMany = [board:Boards, membership:TrusteeMembership] 

    static constraints = { 
     salutation nullable: true 
     firstName nullable: true 
     middleName nullable: true 
     lastName nullable: true 
    } 

    //map to the existing DB table 
    static mapping = { 
     table 'BOT_TRUSTEE' 
     id column:'TRUSTEE_ID' 
     salutation column: 'SALUTATION' 
     firstName column: 'FIRST_NAME' 
     middleName column: 'MIDDLE_INITIAL' 
     lastName column: 'LAST_NAME' 

     version false 
    } 
} 

class Boards { 

    String boardName 

    static belongsTo = [trustee:Trustee, hospital:Hospitals] 

    static constraints = { 
     boardName nullable:true 
    } 

    static mapping = { 
     table name:"BOT_BOARD" 
     id column:'BOARD_ID' 
     trustee column:'TRUSTEE_ID' 
     hospital column:'HOSPITAL_ID' 
     boardName column:'BOARD' 
     version false 
    } 
} 

class Hospitals { 

    String hospitalName 

    static hasMany = [committees:Committees, board:Boards] 

    static constraints = { 
     hospitalName nullable:true 
    } 

    static mapping = { 
     table 'BOT_HOSPITAL' 
     id column:'HOSPITAL_ID' 
     hospitalName column:'HOSPITAL' 
     version false 
    } 
} 

class Committees { 

    String committeeName 
    String description 

    static belongsTo = [hospital: Hospitals] 
    static hasMany = [membership:TrusteeMembership] 

    static constraints = { 
     committeeName nullable:true 
     description nullable:true 
    } 

    static mapping = { 
     table 'BOT_COMMITTEE' 
     id column:'COMMITTEE_ID' 
     hospital column:'HOSPITAL_ID' 
     committeeName column:'COMMITTEE' 
     description column:'DESCRIPTION' 
     version false 
    } 
} 

class TrusteeMembership implements Serializable{ 

    String position 
    String type 

    static belongsTo = [trustee:Trustee, committees:Committees]// 

    static constraints = { 
     position nullable:true 
     type nullable:true 
    } 

    static mapping = { 
     table 'BOT_TRUSTEE_COMMITTEES' 
     version false 
     id composite: ['trustee','committees'] 
     trustee column:'TRUSTEE_ID' 
     committees column: 'COMMITTEE_ID' 

     position column:'POSITION' 
     type column:'TYPE' 
    } 

Вот мой контроллер

def members(){ 
    def letter = params.letter 
    def commId = params.committee 

    params.max = Math.min(params.max ? params.int('max'): 15, 100) 

    def indexSearch = Trustee.createCriteria().list(params){ 

     //search by First letter of lastName 
     if(letter != null){ 
      ilike("lastName", "${letter}%") 
     } 

     //search by lastName 
     if(params.lastName){ 
      ilike("lastName", "%${params.lastName}%") 
     } 

     //search by firstName 
     if(params.firstName){ 
      ilike("firstName", "%${params.firstName}%") 
     } 

     //search by boardName 
     if(params.boardId){ 
      //display only members within a board id 
      board{ 
       eq("id", "%${params.boardId}%") 
      } 
     } 

     order("lastName", "asc") 
    } 

    respond Hospitals.list(params), model:[hospitalsInstanceCount: Hospitals.count(), 
    trusteeInstanceList : indexSearch] 
} 

ответ

0

//search by boardName \t \t \t 
 
\t \t \t if(params.boardId){ 
 
\t \t \t \t //display only members with the boardName \t \t \t \t 
 
\t \t \t \t board{ 
 
\t \t \t \t \t eq("id", params.long('boardId')) 
 
\t \t \t \t } 
 
\t \t \t } \t

Я закончил с этим для правильного результата.