Relationships

Connect users, posts and tags with many-to-one, one-to-many and many-to-many relationships.

On this page

A blog is more than users. A user writes posts, and a post has tags. In this lesson you add the Post and Tag entities and connect all three.

The Post and Tag entities

@persistent
@table( "tags" )
class {

    @fieldtype( "id" )
    @generator( "native" )
    property id;

    @ormtype( "string" )
    @unique
    property name;
}
@persistent
@table( "posts" )
class {

    @fieldtype( "id" )
    @generator( "native" )
    property id;

    @ormtype( "string" )
    @notNull
    property title;

    @ormtype( "text" )
    property body;

    @ormtype( "boolean" )
    @default( false )
    property published;

    // Many posts, one author
    @fieldtype( "many-to-one" )
    @cfc( "User" )
    @fkcolumn( "userID" )
    @lazy
    property author;

    // Many posts, many tags, stored in the posts_tags link table
    @fieldtype( "many-to-many" )
    @cfc( "Tag" )
    @linktable( "posts_tags" )
    @fkcolumn( "postID" )
    @inversejoincolumn( "tagID" )
    property tags;

    @ormtype( "timestamp" )
    @autoTimestamp( "create" )
    property createdDate;

    @ormtype( "timestamp" )
    @autoTimestamp( "update" )
    property updatedDate;
}

And the other side of the author relationship, on User:

// One user, many posts
@fieldtype( "one-to-many" )
@cfc( "Post" )
@fkcolumn( "userID" )
@inverse
@lazy
@cascade( "all-delete-orphan" )
property posts;

The three kinds

KindExampleWhere the link lives
many-to-onePost.authorA userID column on posts
one-to-manyUser.postsThe same userID column, seen from User
many-to-manyPost.tagsThe posts_tags link table

@cfc( "Post" ) names the related entity, and @fkcolumn names the foreign key column.

Post.author and User.posts describe the same foreign key from both sides. Only one side should write it, or Hibernate updates the column twice. @inverse on User.posts says "the other side owns this". For a one-to-many and many-to-one pair, mark the collection side inverse.

Generated methods

For each relationship, BxORM generates helpers on the entity:

MethodWhat it doesOn
hasPosts()true when the relationship has any itemsAll relationships
addPosts( post )Adds an entity to the collectionone-to-many, many-to-many
removePosts( post )Removes an entity from the collectionone-to-many, many-to-many

The name follows the property, so Post.tags gets hasTags(), addTags() and removeTags().

Prefer singular names for one item? Add @singularName( "tag" ) to the property: the methods become hasTag(), addTag() and removeTag() (the plural ones are not generated then).

Set both sides of a bidirectional relationship, so the objects in memory match the database:

import blog.models.User;
import blog.models.Tag;

transaction {
    ann    = User::findWhereOrFail( { email : "ann@example.com" } );
    ormTag = Tag::firstOrCreate( { name : "orm" } );

    post = entityNew( "Post", { title : "Relationships", author : ann } );
    post.addTags( ormTag );
    ann.addPosts( post );

    // The cascade on User.posts saves the new post too
    entitySave( ann );
}

@cascade( "all-delete-orphan" ) passes saves and deletes from a user to their posts, and deletes a post that is removed from ann.posts:

transaction {
    ann.removePosts( post ); // the post is deleted when the transaction ends
}

That also means a post still inside a loaded user's posts collection cannot simply be deleted with entityDelete(): the cascade saves it again. Remove it from the collection instead.

Lazy loading

@lazy means "load this when I first use it". Loading a user does not load their posts. Calling ann.getPosts() runs one more query, the first time:

ann = User::get( 1 );          // select from users
println( ann.getPosts().len() ); // now: select from posts where userID = 1

Lazy is the right default for collections. Watch out for loops that touch a lazy relationship once per row: that is the N+1 problem, and lesson 11 shows how to spot and fix it.

Try it

  • Create two posts for Ann, each with two tags, then print each post's title and tag names.
  • Load a post and print its author's name. Watch the SQL: when does the user query run?
  • Remove one post from ann.posts in a transaction and check the posts table.
  • Add a posts relationship to Tag (many-to-many, inverse) so a tag can list its posts.

Learn more

Edit this page Download Markdown Last updated Nov 23, 2026, 5:56:16 PM Report an issue
Lesson 6 of 17 - Learn BxORM