grails多个表标准

时间:2015-04-27 20:00:28

标签: grails gorm criteria

我的应用程序适用于除调用不同主板的成员之外的所有内容。我可以使用SQL查询在db中获取正确的输出,但是在使用createCriteria在Grails中尝试它时会出现问题。

必须使用 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]
}

1 个答案:

答案 0 :(得分:0)



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




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