用grails创建自关联树
在网上找到的大神代码:
class SystemMenu {
String name;
String description;
SystemMenu parent;
static belongsTo = [parent: SystemMenu]
static hasMany = [children: SystemMenu];
static mapping = {
parent: [column: 'parent_Id', lazy: "true", cascade: "none"]
children joinTable: [name: 'children', key: 'parent_Id', column: 'Id', lazy: "true", inverse: "false", cascade: "none"]
}
static constraints = {
parent(nullable: true)
}
}
挺好使的
但是如果 想把 ID策略设置为 uuid
就会出错
class Node {
String id;
String name;
String description;
Node parent;
Integer indexNum;
static belongsTo = [parent: Node]
static hasMany = [children: Node];
static mapping = {
id generator: 'uuid.hex'
parent: [column: 'parent_Id', lazy: "true", cascade: "none"]
children joinTable: [name: 'children', key: 'parent_Id', column: 'Id', lazy: "true", inverse: "false", cascade: "none"],sort: 'indexNum',order: 'asc'
}
static constraints = {
parent(nullable: true)
}
}
在创建时就会报这错
create: Create
Data conversion error converting "402881873ffa8c4c013ffa8c91090000"; SQL statement:
insert into node (version, description, index_num, name, parent_id, id) values (?, ?, ?, ?, ?, ?)
如果用 id generator: 'native' 策略 就没问题
感觉挺诧异的.
grails 版本 2.2.3