Errors and observability

Read and catch ORM errors, inspect the ORM with ormDiagnostics(), and watch your SQL in BxLens to find N+1 queries.

On this page

Things go wrong: a duplicate email, a typo in a property name, a page that suddenly runs forty queries. BxORM is built to make these easy to see and fix.

Errors written for humans

Every ORM error has a type that starts with orm, and a message that says what happened and how to fix it:

orm.entity.notFound: There is no entity named [Usr]. Did you mean [User]?
orm.property.unknown: User has no property [emial]. Did you mean [email]?
orm.notFound: No [Post] with id [99] was found.
orm.argument: Tag::deleteWhere() needs a non-empty filter; an empty one would affect every row.

A few of the most common types:

TypeWhen
orm.notFoundAn OrFail load matched nothing
orm.query.nonUniqueA "one result" load matched several rows
orm.property.unknownA property name that does not exist
orm.constraint.uniqueA unique constraint rejected the change
orm.constraint.notNullA required value is missing
orm.lazy.noSessionA lazy relationship was read after its session ended

Catching errors

BoxLang matches catch types by dotted prefix, so you can catch one exact type, a family, or every ORM error:

try {
    transaction {
        entitySave( entityNew( "User", { email : form.email, name : form.name } ) );
    }
} catch ( "orm.constraint.unique" e ) {
    message = "That email address is already registered.";
} catch ( "orm" e ) {
    rethrow;
}

ormDiagnostics()

When something looks wrong, ask the ORM about itself. ormDiagnostics() never throws:

d = ormDiagnostics();
println( d.status );           // running
println( d.entityCount );      // 3
println( d.hibernateVersion ); // 7.4.x

It also holds the last startup error, the entities per datasource, the settings that explain most surprises, and the current session's state.

Listening to the ORM

BxORM announces events that tools can listen to, at no cost when nothing listens:

EventFires
onORMQueryAfter each SQL statement
onORMFlushAfter each session flush
onORMExceptionWhen a statement fails

A small interceptor can log slow SQL:

class {
    function onORMQuery( data ) {
        if ( data.elapsedNanos > 100000000 ) { // 100 ms
            writeLog( text = "Slow ORM #data.kind#: #data.sql#", type = "warning" );
        }
    }
}
boxRegisterInterceptor( interceptor : new SlowSql(), points : [ "onORMQuery" ] );

BxLens: see every query

BxLens is BoxLang's observability tool, and it listens to these events for you. Install it next to BxORM:

bx-module install bx-lens

ORM is a BxLens integration, off until you turn it on: an admin switches it on from the console (Modules page, Integrations, or the ORM page), or sets collectors.orm.enabled to true. Then:

  • Queries panel (free). Each ORM statement appears with the request's other queries: its SQL, time, rows and the BoxLang line that ran it, labelled ORM. Slow query and N+1 detection apply to it.
  • ORM page (BoxLang+). Totals per datasource, flushes, recent failures and Hibernate statistics.

Open the bar on any page, or the console at /~bxlens/index.bxm.

Fixing an N+1

Here is the blog's home page, listing posts with their author:

posts = entityLoad( "Post", { published : true }, "createdDate desc" );
for ( p in posts ) {
    println( "#p.getTitle()# by #p.getAuthor().getName()#" );
}

author is lazy, so each getAuthor().getName() runs one more query: 1 query for the posts, plus N for the authors. BxLens flags it as N+1. When you only need a few fields, select them in one query with a criteria projection:

rows = entityCriteria( "Post" )
    .isTrue( "published" )
    .orderByDesc( "createdDate" )
    .asStruct( "id,title,author.name" )
    .list();

for ( row in rows ) {
    println( "#row.title# by #row.author.name#" );
}

Try it

  • Trigger three different ORM errors on purpose and read each message.
  • Catch orm.constraint.unique when a second user signs up with Ann's email.
  • Print ormDiagnostics() and find your dbcreate setting in it.
  • Install BxLens, turn on the ORM integration and load the home page before and after the fix.

Learn more

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