简体   繁体   中英

Grails GORM: Mapping tree legacy table

I'm currently trying to map a legacy (mysql) table with the following 3 relevant fields:

id -> Long
parentId -> Long
title -> String

id and parentId describe the tree-structure. What I need is a method to search all nodes under a given one for a certain title and retrieve that node's id . I am new to Grails and can't get my head around mapping that one-to-many relationship based on an existing table.

To map the class, try something like this:

class Foo {

    String title
    Foo parent

    static mapping = {
        parent column: 'parentId'
    }
}

You don't need to specify the id as a Long because that is the default. You will, however, need to specify the id if it has a column name other than id .

As far as the parent object goes, you will need to map it by the foreign key column's name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM