组合主键的使用(1)

Grails中定义域类后,就可以通过脚手架自动生成各种CRUD操作代码。但是,如果是组合主键所生成的代码中诸多功能是不工作的,包括:

index中点击热点,自动调用show,显示当前记录;

edit功能,

delete功能。。

等等。

 

之所以不能工作,原因就是使用了复合主键——原来的ID不起作用了,或者说没有了。

 

所以,必须对域类、控制器、GSP文件都进行相应的修改,才行。

 

首先是域类的说明:

package cn.edu.cup.unit

import grails.gorm.DetachedCriteria
import groovy.transform.ToString
import org.apache.commons.lang.builder.HashCodeBuilder

@ToString(cache=true, includeNames=true, includePackage=false)
class QuantityStandardUnit implements Serializable {

    private static final long serialVersionUID = 1

    PhysicalQuantity physicalQuantity
    QuantityUnit quantityUnit

    QuantityStandardUnit(PhysicalQuantity p, QuantityUnit q) {
        this()
        physicalQuantity = p
        quantityUnit = q
    }

    @Override
    boolean equals(other) {
        if (!(other instanceof QuantityStandardUnit)) {
            return false
        }

        other.physicalQuantity?.id == 
        physicalQuantity?.id && 
        other.quantityUnit?.id == quantityUnit?.id
    }

    @Override
    int hashCode() {
        def builder = new HashCodeBuilder()
        if (physicalQuantity) builder.append(physicalQuantity.id)
        if (quantityUnit) builder.append(quantityUnit.id)
        builder.toHashCode()
    }

    static QuantityStandardUnit get(long physicalQuantityId, long quantityUnitId) {
        criteriaFor(physicalQuantityId, quantityUnitId).get()
    }

    static boolean exists(long physicalQuantityId, long quantityUnitId) {
        criteriaFor(physicalQuantityId, quantityUnitId).count()
    }

    private static DetachedCriteria criteriaFor(long physicalQuantityId, long quantityUnitId) {
        QuantityStandardUnit.where {
            physicalQuantity == PhysicalQuantity.load(physicalQuantityId) &&
            quantityUnit == QuantityUnit.load(quantityUnitId)
        }
    }

    static QuantityStandardUnit create(PhysicalQuantity physicalQuantity, QuantityUnit quantityUnit, boolean flush = false) {
        def instance = new QuantityStandardUnit(physicalQuantity, quantityUnit)
        instance.save(flush: flush, insert: true)
        instance
    }

    static boolean remove(PhysicalQuantity u, QuantityUnit r, boolean flush = false) {
        if (u == null || r == null) return false

        int rowCount = QuantityStandardUnit.where { physicalQuantity == u && quantityUnit == r }.deleteAll()

        if (flush) { QuantityStandardUnit.withSession { it.flush() } }

        rowCount
    }

    static void removeAll(PhysicalQuantity u, boolean flush = false) {
        if (u == null) return

        QuantityStandardUnit.where { physicalQuantity == u }.deleteAll()

        if (flush) { QuantityStandardUnit.withSession { it.flush() } }
    }

    static void removeAll(QuantityUnit r, boolean flush = false) {
        if (r == null) return

        QuantityStandardUnit.where { quantityUnit == r }.deleteAll()

        if (flush) { QuantityStandardUnit.withSession { it.flush() } }
    }

    static constraints = {
        quantityUnit validator: { QuantityUnit r, QuantityStandardUnit ur ->
            if (ur.physicalQuantity == null || ur.physicalQuantity.id == null) return
            boolean existing = false
            QuantityStandardUnit.withNewSession {
                existing = QuantityStandardUnit.exists(ur.physicalQuantity.id, r.id)
            }
            if (existing) {
                return 'quantityStandardUnit.exists'
            }
        }
    }

    static mapping = {
        id composite: ['physicalQuantity', 'quantityUnit']
        version false
    }
}

 

 

你可能感兴趣的:(组合主键的使用(1))