GORM 无法使用 addTo 创建新实体

GORM cannot create new entity using addTo

我有一个 class Sheet布局,其中包含以下内容:

private String size
private int xPosition
private int yPosition

private Sheet sheet
private Report report

static belongsTo=[sheet : Sheet]

和一个 class Sheet 具有以下内容:

@Id
private String id

private Date created = new Date()
private Date modified
private String title
private int sheetIndex
private String type

private Dashboard dashboard

static hasMany = [sheetLayouts : SheetLayout, slicersWidgets : SlicerWidget]

static belongsTo=[dashboard : Dashboard]

现在我试图以任何可能的方式(有或没有 Sheet)保存 SheetLayout,但它不起作用。 我尝试过的选项之一:

def testsl(){
        Sheet s = Sheet.findById("AAAAAaaaaaAAAaaaAA")
        SheetLayout sl = new SheetLayout()
        sl.setxPosition(0)
        sl.setyPosition(1)
        sl.setSize("satas")
        s.addToSheetLayouts(sl)
        s.save(flush:true)

    }

它只是不起作用:(

有什么想法吗?

GORM cannot create new entity using addTo

我不认为这是真的。以下作品:

Sheet.groovy

class Sheet {
    // consider using GORM auto timestamp properties for these Dates...
    Date created
    Date modified

    String title
    int sheetIndex
    String type

    static hasMany = [sheetLayouts : SheetLayout]
}

SheetLayout.groovy

class SheetLayout {
    String size
    int xPosition
    int yPosition

    Sheet sheet

    static belongsTo = [sheet : Sheet]
}

持久化实例的代码:

    def now = new Date()
    Sheet s = new Sheet(created: now, modified: now, title: 'Some Title', sheetIndex: 42, type: 'wilbur')
    SheetLayout sl = new SheetLayout()
    sl.setxPosition(0)
    sl.setyPosition(1)
    sl.setSize("satas")
    s.addToSheetLayouts(sl)
    s.save(flush:true, failOnError: true)

你也可以这样做:

    def now = new Date()
    Sheet s = new Sheet(created: now, modified: now, title: 'Some Title', sheetIndex: 42, type: 'wilbur')
    s.addToSheetLayouts(xPosition: 0, yPosition: 1, size: 'satas')
    s.save(flush:true, failOnError: true)

我建议您在尝试保存实例 and/or 后检查 errors 属性 使用 failOnError:true.

编辑:

我刚刚重新阅读了您的示例,看起来您的 SheetLayout class 有一个您没有初始化的 report 属性。除非你用 nullable: true 配置了它,否则这将是你问题的一部分。