Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to define a Parent/Child relationship with CF9 ORM

New Here ,
Nov 25, 2010 Nov 25, 2010

Hi,

a rather simple database scheme

Object {

    id,

    name

}

ObjectRelation {

    parentID, -- points to id in the Object table

    childID    -- points to id in the Object table

}

how would i define an Object Model component with properties

component {

    property "name"

    property "children"

    property "parent"

}

obviously root object(s) don't have any parent elements.

TOPICS
Database access
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 25, 2010 Nov 25, 2010

Quick question - have you read *any* of the documentation? What have you tried thus far?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 25, 2010 Nov 25, 2010

i actually RTFM! so far i got the children relationship working like this

property name="children"
    fieldtype="one-to-many"
    cfc="Object"
    linktable="ObjectRelation"
    fkcolumn="parentID"
    singularname="child"
    lazy=true
    inversejoincolumn="childID";

i had the "inverse" version of this for the parent relationship, it didn't work all to well though.

property name="parent"
    fieldtype="many-to-one"
    cfc="Object"
    linktable="ObjectRelation"
    fkcolumn="childID"
    lazy=true
    inversejoincolumn="parentID";

with that whenever i try to call object.getParent().getName() i get a

Error Messages: Value must be initialized before use.
Its possible that a method called on a Java object created by CreateObject returned null.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 25, 2010 Nov 25, 2010

Glad to hear it

I haven't done any ORM in a while so I'm sure someone will correct me, but I don't believe you need the linktable attribute unless it's a many-to-many; in your case you should be able to add a property to the child object called parent, with a cfctype of whatever it is, and a fkcolumn attribute of the foreign key column.

That should be all you need. Try cutting your attributes down to the absolute bear minimum and try that, alas I don't have a working example to hand I'm afraid.

O.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 25, 2010 Nov 25, 2010
LATEST

the relation is built with the ObjectRelation table, so i think ORM needs to know the

name of the relation table in order to get the parent.

what's more, in the hibernate log file i see alot of select with outer join, didn't disassamble them yet though. however, i believe that is why i get more objects than there actually are.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources