EntitySave()

edit

Saves or updates data of the entity and all related entities to the database.

Requires Extension: Hibernate ORM Engine Extension

EntitySave( entity=object, forceInsert=boolean );

Returns: void

Argument Description
entity
object, required
edit

Name of the entity that must be saved in the database.

forceInsert
boolean, optional
edit

If true, then Lucee always tries to insert the entity as a new record.

Usage Notes

edit

entitySave() does not immediately write to the database. It marks the entity as pending in the ORM session. The actual INSERT or UPDATE executes when the session flushes — either at transaction commit, on explicit ORMFlush(), or at request end if flushAtRequestEnd is true.

For new entities with generator="native" or generator="identity", the INSERT executes immediately because Hibernate needs the database-generated ID.

When forceInsert is true, Hibernate always attempts an INSERT regardless of whether the entity already exists. Use this when you know the entity is new but Hibernate might think otherwise (e.g. assigned IDs).

Always wrap saves in a transaction block for rollback safety. See ORM - Sessions and Transactions.

Examples

edit
// Save a new entity
product = entityNew( "Product", { name: "Widget", price: 9.99 } );
entitySave( product );
ormFlush();
// Save inside a transaction (recommended)
transaction {
	user = entityNew( "User", { name: "Susi" } );
	entitySave( user );
	// commit flushes automatically
}
// Force insert — always INSERT, never UPDATE
product = entityNew( "Product", { id: createUUID(), name: "Gadget" } );
entitySave( product, true );
ormFlush();

See also