如何在同一类型上同时使用hasOne和hasMany关系?

时间:2016-11-02 07:27:12

标签: grails gorm

地图域有一个中心和一个坐标列表。中心和路径都具有相同的类型,即Coordinate。

class Map {

    Coordinate center

    List path

    static hasOne = [center: Coordinate]

    static hasMany = [path: Coordinate]

    static constraints = {
    }
}

坐标域定义为

class Coordinate {

    static belongsTo = [map: Map]

    static constraints = {
    }
}

现在我有一个简单的代码

def map = new Map(center: new Coordinate())

map.addToPath(new Coordinate())

map.save()

这是抛出异常

2016-11-02 13:08:55,970 [Thread-15] ERROR util.JDBCExceptionReporter  - NULL not allowed for column "MAP_ID"; SQL statement:
insert into coordinate (id, version, map_id, path_idx) values (null, ?, ?, ?) [23502-164]
Exception thrown

org.springframework.dao.DataIntegrityViolationException: could not insert: [exampletest.Coordinate]; SQL [insert into coordinate (id, version, map_id, path_idx) values (null, ?, ?, ?)]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not insert: [exampletest.Coordinate]

我想知道错误的原因是什么。我认为由于坐标属于Map,因此保存地图会自动保存其子节点,在本例中为Coordinate。我感谢任何帮助,并解决了这个问题我坚持。谢谢!

0 个答案:

没有答案