Exception Grails GORM关联未映射异常

Exception Grails GORM关联未映射异常,exception,grails,associations,gorm,Exception,Grails,Associations,Gorm,我正在创建一个Grails 2.4.3应用程序,并具有以下域类: class StockItem { String name BigDecimal wholesalePrice BigDecimal retailPrice BigDecimal profit static belongsTo = [ledger: Ledger] static constraints = { name minSize:3, maxSize:255

我正在创建一个Grails 2.4.3应用程序,并具有以下域类:

class StockItem {
    String name
    BigDecimal wholesalePrice
    BigDecimal retailPrice
    BigDecimal profit

    static belongsTo = [ledger: Ledger]

    static constraints = {
        name minSize:3, maxSize:255
        wholesalePrice min:0.0, scale:2
        retailPrice min:0.0, scale:2
        retailPrice validator: { retailPrice, StockItem obj ->
            if (retailPrice < obj.wholesalePrice) {
                ['retailLessThanWholesale']
            }
        }
    }

    static mapping = { 
        profit(formula: 'RETAIL_PRICE - WHOLESALE_PRICE') 
    }
}

class Ledger {
    String name

    static hasMany = [clients: Client, invoices: Invoice, availableItems: StockItem, payments: Payment]

    static constraints = {
        name unique:true
    }
}
}

这就是失败:

Failure:  Test profit calculation(com.waldoware.invoicer.StockItemSpec)
|  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is 
org.hibernate.MappingException: An association from the table stock_item refers to an unmapped class: com.waldoware.invoicer.Ledger

这个应用程序似乎运行正常,我不明白为什么这个测试失败了。任何想法都很感激

由于您的
StockItem
Ledger
域类之间存在
belongsTo
关联,您需要在测试中将
Ledger
添加到
@domain
注释中:

@Domain([StockItem, Ledger])
这应该在单元测试运行时初始化时正确配置所需的域类。根据您的其他测试用例,您可能还需要在注释中包括
Client
Payment

@Domain([StockItem, Ledger])