๐Ÿ”ท Core

bx-orm-relationships

Use this skill when defining relationships between BoxLang ORM entities (bx-orm 2.x, Hibernate 7.4): one-to-one, one-to-many, many-to-one, many-to-many, fkcolumn, linktable, inversejoincolumn, inverse and mappedBy, lazy and fetch, cascade options, singularName and the generated has/add/remove methods, orderBy and where on collections, struct-keyed collections, value collections (fieldtype=collection), batch size and collection caching.

$ npx skills add ortus-boxlang/skills/boxlang-modules/bx-orm/bx-orm-relationships
$ coldbox ai skills install ortus-boxlang/skills/boxlang-modules/bx-orm/bx-orm-relationships
๐Ÿ”— https://skills.boxlang.io/skills/raw/ortus-boxlang/skills/boxlang-modules~bx-orm~bx-orm-relationships

bx-orm: Entity Relationships

BoxLang is the AI-native software productivity platform for building, modernizing and running applications, with developers and AI agents working together.

Relationship Overview

TypeUse CaseExample
one-to-oneSingle associated recordUser โ†’ Address
one-to-manyParent with collectionUser โ†’ Posts
many-to-oneChild pointing to parentPost โ†’ Author
many-to-manyMulti on both sidesAuthor โ†” Book

Every relationship property uses fieldtype plus class (alias cfc): an entity name, class name or class path on the same datasource.

One-to-Many and Many-to-One (Bidirectional)

// User.bx: the collection side is the inverse side
class persistent="true" entityName="User" {
    property name="id" fieldtype="id" generator="native";
    property name="posts"
        fieldtype    = "one-to-many"
        class        = "Post"
        fkcolumn     = "author_id"   // FK column in the posts table
        inverse      = "true"        // Post.author owns the FK
        singularName = "post"
        cascade      = "all-delete-orphan";
}

// Post.bx: the owning side holds the FK column
class persistent="true" entityName="Post" {
    property name="id" fieldtype="id" generator="native";
    property name="author" fieldtype="many-to-one" class="User"
        fkcolumn="author_id";
}

Instead of inverse="true", mappedBy="author" on the collection names the owning property. Both tell Hibernate the collection does not write the FK.

user  = entityLoadByPK( "User", 1 );
posts = user.getPosts();      // array of Post entities
user.hasPost();               // true if the collection has any items
user.hasPost( somePost );     // true if somePost is in the collection
user.addPost( newPost );      // returns the entity, for chaining
user.removePost( oldPost );
newPost.setAuthor( user );    // set the owning side too

Generated Relationship Methods

MethodGenerated for
has<Name>( [entity] )All relationships (no argument on to-one)
add<Name>( entity )one-to-many, many-to-many
remove<Name>( entity )one-to-many, many-to-many

<Name> is the property name, or singularName when set (posts with singularName="post" gives hasPost(), addPost(), removePost()). Getters and setters (getPosts(), setPosts()) always use the property name. Methods you write yourself are never replaced. In 2.x a to-many getX() returns a snapshot, so getPosts().each( p => removePost( p ) ) is safe.

One-to-One

// Shared primary key: no fkcolumn, both rows have the same id
property name="profile" fieldtype="one-to-one" class="Profile" cascade="all";

// Profile.bx: its id comes from the owner; constrained adds the PK FK
property name="id" fieldtype="id" generator="foreign"
    params="{ property = 'user' }";
property name="user" fieldtype="one-to-one" class="User" constrained="true";

// Foreign-key one-to-one: with fkcolumn it is mapped as a many-to-one
property name="address" fieldtype="one-to-one" class="Address"
    fkcolumn="address_id";

Many-to-Many

// Author.bx: owning side writes the link rows
property name="books"
    fieldtype         = "many-to-many"
    class             = "Book"
    linktable         = "author_books"
    fkcolumn          = "author_id"    // this entity's FK in the link table
    inversejoincolumn = "book_id"      // the other entity's FK
    singularName      = "book";

// Book.bx: inverse side, same link table, columns swapped
property name="authors"
    fieldtype         = "many-to-many"
    class             = "Author"
    linktable         = "author_books"
    fkcolumn          = "book_id"
    inversejoincolumn = "author_id"
    inverse           = "true"
    singularName      = "author";

linkschema and linkcatalog place the link table. A one-to-many with a linktable is mapped as a unique many-to-many through that table.

Relationship Annotations

AnnotationDescription
fieldtypeone-to-one, one-to-many, many-to-one, many-to-many
class / cfcRelated entity (required)
fkcolumnFK column (comma list for multi-column keys)
linktableJoin table; requires fkcolumn
inversejoincolumnThe other entity's FK column in the link table
linkschema / linkcatalogSchema / catalog of the link table
inversetrue: this side does not write the FK
mappedByOwning property on the other entity
lazytrue, false, proxy, no-proxy, extra
fetchTo-one only: select (default) or join
cascadeComma list, see below (default none)
singularNameName used by the has/add/remove methods
orderByRaw SQL ORDER BY for a to-many collection
whereRaw SQL filter on a to-many collection
typeCollection type: array (default) or struct
batchSizeCollections fetched per round trip
constrainedone-to-one only: PK is also an FK to the target
missingRowIgnoredmany-to-one: a dangling FK loads as null
notnullRequired to-one association
cacheUse / cacheNameCache a to-many collection (2.x)

orderBy and where use column names, not property names.

Cascade Options

ValueBehavior (2.x)
noneNo cascade (default)
save-updateentitySave() persists new and merges detached children
persist / createPersist new children only
mergeMerge children with the owner
delete / removeDelete children with the owner
delete-orphanDelete, plus delete children removed from the collection
allAll operations
all-delete-orphanall plus orphan removal
refresh, evict, lock, replicateCascade that operation

Combine values with commas: cascade="save-update,delete". Hibernate 7 removed the save-update style, so 2.x maps it to persist plus merge (1.x used Hibernate 5's own save-update, and persist was its alias). Unknown values are ignored silently. Orphan removal applies to to-many collections only.

Lazy vs Eager Loading

// Lazy (default): loaded on first access
property name="posts" fieldtype="one-to-many" class="Post"
    fkcolumn="author_id" lazy="true";

// Eager to-one
property name="address" fieldtype="one-to-one" class="Address" lazy="false";

// Eager to-one, fetched with a SQL join (no `lazy` attribute)
property name="manufacturer" fieldtype="many-to-one" class="Manufacturer"
    fkcolumn="FK_manufacturer" fetch="join";

Associations are lazy unless lazy="false", including to-one ones.

  • In 2.x every lazy to-one (true, proxy, no-proxy) is a lazy proxy, and the proxyLazyLoading setting has no effect. In 1.x proxyLazyLoading decided whether lazy="true" became proxy.
  • lazy="extra" works like true in 2.x (Hibernate 7 has no extra-lazy collections, so size() loads the collection). In 1.x it was extra-lazy.
  • fetch="join" makes a to-one eager only when lazy is not set (lazy wins). bx-orm 2.x ignores fetch on to-many collections; use lazy.

Struct and Value Collections

// Entity collection keyed by a column (struct instead of array)
property name="itemsByCode" fieldtype="one-to-many" class="Item"
    fkcolumn="shelfId" type="struct" structKeyColumn="itemCode"
    structKeyType="string";

// Value collection (2.x): plain values in their own table
property name="tags" fieldtype="collection" type="array"
    table="article_tags" fkcolumn="article_id"
    elementColumn="tag" elementType="string";

structKeyColumn is required for type="struct". structKeyFormula can compute the key with SQL. A value collection with type="struct" also takes structKeyColumn/structKeyType.

N+1 Prevention

class persistent="true" entityName="Post" batchSize="20" {}  // entity level

property name="comments" fieldtype="one-to-many" class="Comment"
    fkcolumn="post_id" batchSize="50";                          // collection

Without a collection batchSize, the defaultBatchSize ORM setting (16) applies.

Common Pitfalls

  • โŒ Do NOT write inversejoin: the annotation is inversejoincolumn (unknown annotations are ignored)
  • โŒ Do NOT use linktable without fkcolumn: the ORM fails to start
  • โŒ Do NOT leave both sides of a bidirectional pair owning: mark the collection (or one many-to-many side) inverse="true" or use mappedBy
  • โŒ Do NOT use lazy="false" on large collections: every load pulls them
  • โŒ Do NOT expect lazy="extra" to count without loading in 2.x
  • โœ… Declare fkcolumn explicitly; an owning one-to-many without it borrows the target's back-reference FK, or falls back to a join table with a warning
  • โœ… Set singularName on collections for addPost() / removePost()
  • โœ… Use cascade="all-delete-orphan" on owned children (order โ†’ line items)
  • โœ… Set both sides in memory (addPost() and setAuthor()) on bidirectional pairs