【问题标题】:grails multiple table criteriagrails 多个表条件
【发布时间】:2015-04-27 20:00:27
【问题描述】:

我的应用程序适用于除显示不同董事会成员的调用之外的所有内容。我可以使用 SQL 查询在数据库中获得正确的输出,但在使用 createCriteria 的 Grails 中尝试它时遇到问题。

必须使用 Oracle 11g 作为我的数据库。 圣杯 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]
}

【问题讨论】:

    标签: grails grails-orm criteria


    【解决方案1】:

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

    我最终得到了正确的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多