Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
For full Hibernate configuration support, you can point the ORM extension to an XML file containing any configuration properties you wish to configure:
this.ormSettings = {
// ...
ormconfig : "./config/persistence.xml"
};
You can then populate the XML file with valid Hibernate configuration syntax:
<!-- persistence.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"https://hibernate.org/dtd/hibernate-reverse-engineering-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- https://docs.jboss.org/hibernate/orm/5.4/javadocs/org/hibernate/cfg/AvailableSettings.html#USE_SQL_COMMENTS -->
<property name="hibernate.use_sql_comments">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
</session-factory>
</hibernate-configuration>
Get up and running in seconds!
You can install this extension by adding the extension ID to the lucee-extensions
environment variable and restarting the server:
You can install a specific version by appending a version string to the extension ID:
(For all available version numbers, see our or the .)
Running the box server set env.lucee-extensions
command will edit your server.json
file to look something like this:
This config instructs Lucee (not CommandBox) to install the Ortus ORM Extension when Lucee starts up.
You can manually install the extension by opening the Lucee server admin page and browsing to Extension > Applications
page, where you'll see a "Ortus ORM Extension" option. Clicking this option will open the installation screen, where you can choose any extension version and click "Install" to install from Forgebox.
You can also download a .lex
file from and upload this through the Lucee Server admin's Applications page.
Whichever method you use to install the extension, we recommend restarting the server after installing.
Via CommandBox:
box server set env.LUCEE_EXTENSIONS="D062D72F-F8A2-46F0-8CBC91325B2F067B" && box restart
box server set env.LUCEE_EXTENSIONS="D062D72F-F8A2-46F0-8CBC91325B2F067B;version=6.5.2" && box restart
{
// ...
"env":{
"LUCEE_EXTENSIONS":"D062D72F-F8A2-46F0-8CBC91325B2F067B;version=6.5.2"
}
}
server restart
How do Hibernate logs work in the Ortus ORM Extension?
The Ortus ORM Extension reconfigures all Hibernate logging output on startup to log ERROR
-level events to the server console.
This depends on the version of Lucee you are running - specifically, on whether you're on Log4J 2 or not.
Lucee versions containing the older log4j 1.x can use this code to set a custom logging level and redirect the Hibernate logs to the server console:
void function setupHibernateLogging( level = "WARN" ){
var Logger = createObject( "java", "org.apache.log4j.Logger" );
var log4jLevel = createObject( "java", "org.apache.log4j.Level" );
var hibernateLog = Logger.getLogger( "org.hibernate" );
// set a custom log level
hibernateLog.setLevel( log4jLevel[ arguments.level ] );
/**
* Redirect all Hibernate logs to system.out
*/
if ( listFindNoCase( "Lucee", server.coldfusion.productname ) ) {
var printWriter = getPageContext().getConfig().getOutWriter();
var layout = createObject( "java", "lucee.commons.io.log.log4j.layout.ClassicLayout" );
var consoleAppender = createObject( "java", "lucee.commons.io.log.log4j.appender.ConsoleAppender" ).init(
printWriter,
layout
);
hibernateLog.addAppender( consoleAppender );
writeDump( var = "** Lucee Hibernate Logging Redirected", output = "console" );
}
}
For versions of Lucee running log4j 2.x, the above workaround does not apply. If you are interested in tweaking the Hibernate logging level in the Lucee Hibernate extension, I would suggest sponsoring this feature and future users can benefit from the shared knowledge.
The Ortus ORM Extension is a native Lucee Extension that allows your CFML application to integrate with the powerful Hibernate ORM
The Ortus ORM Extension is a native Lucee Extension that allows your CFML application to integrate with the powerful Hibernate ORM. With Hibernate, you can interact with your database records in an object oriented fashion, using components to denote each record and simple getters and setters for each field value:
component entityName="Auto" persistent="true" {
property name="id" type="string" fieldtype="id" ormtype="string";
property name="make" type="string";
property name="model" type="string";
function onPreInsert(){
log.info( "Inserting new Auto: #getMake()# #getModel()#" );
}
}
The Ortus ORM extension also enables transactional persistence, where an error during a save will roll back the entire transaction to prevent leaving the database in a broken state:
transaction{
try{
entitySave(
entityNew( "Purchase", {
productID : "123-expensive-watch",
purchaseTime : now(),
customerID : customer.getId()
})
);
var cartProducts = entityLoad( "CartProduct", customer.getID() );
entityDelete( cartProducts );
} catch ( any e ){
// don't clear the user's cart if the purchase failed
transactionRollback();
rethrow;
}
}
Lucee 5.3.9.73 and above
Java 8, 11 or 17
The Ortus ORM Extension bundles Hibernate 5.6.15.FINAL
since extension version 6.2.0.
Previous versions of the Ortus ORM Extension bundle Hibernate 5.4.29.FINAL
:
The Ortus ORM extension is an open source Lucee server extension with no license purchase necessary. If you are looking to further the development of this extension, consider sponsoring a feature or opening a support contract.
Add Object Relational Mapping to any CFML app with Hibernate ORM
Use native CFML methods to update and persist entities to the database (entityNew()
, entitySave()
, ormFlush()
, etc.)
Supports 80+ database dialects, from SQLServer2005
to MySQL8
and PostgreSQL
60% faster startup than the Lucee Hibernate extension
Generate your mapping XML once and never again with the autoGenMap=false
ORM configuration setting
React to entity changes with pre and post event listeners such as onPreInsert()
, onPreUpdate()
and onPreDelete()
Over 20 native CFML functions:
EntityDelete()
EntityLoad()
EntityLoadByExample()
EntityLoadByPK()
EntityMerge()
EntityNameArray()
EntityNameList()
EntityNew()
EntityReload()
EntitySave()
EntityToQuery()
ORMClearSession()
ORMCloseAllSessions()
ORMEvictCollection()
ORMEvictEntity()
ORMEvictQueries()
ORMExecuteQuery()
ORMFlush()
ORMGetSession()
ORMGetSessionFactory()
ORMReload()
See the extension changelog for a full list of enhancements and bug fixes.
Our expertise with Hibernate ORM and Lucee Server allows us to give back to the community, as well as offer premium support to enterprises looking for a level up in their Hibernate implementations. If you need performance optimization, session management or caching integrations, please contact us for support.
Support Plans: https://www.ortussolutions.com/services/support
Bug Tracker: https://ortussolutions.atlassian.net/browse/OOE
All about entity identifiers, from primary keys, and generators to composite keys and field types.
Identifier properties are denoted via fieldtype="id"
.
We highly recommend disabling updates on identifier properties via update="false"
:
property name="id"
fieldtype="id"
ormtype="string"
generator="assigned"
update="false";
The Assigned generator is the default identifier generator type, and simply allows the application (your CFML) to assign identifier values prior to insertion. You can think of this as generator=none
.
lets the application assign an identifier to the object before save() is called. This is the default strategy if no element is specified. - Hibernate 3.3 mapping reference docs
property name="id"
fieldtype="id"
generator="assigned"
update="false";
Assigned generators will commonly use a preInsert()
event listener to assign the identifer value:
function preInsert( entity ){
setId( createUUID() );
}
A select generator will attempt to select the next identifier value from the specified selectKey
column:
retrieves a primary key, assigned by a database trigger, by selecting the row by some unique key and retrieving the primary key value. - Hibernate 3.3 mapping reference docs
property name="userID"
fieldtype="id"
generator="select"
selectKey="ssn"
update="false";
The UUID generator uses Hibernate's uuid generator under the hood:
uses a 128-bit UUID algorithm to generate identifiers of type string that are unique within a network (the IP address is used). The UUID is encoded as a string of 32 hexadecimal digits in length. - Hibernate 3.3 mapping reference docs
property name="userID"
fieldtype="id"
generator="uuid"
update="false";
The Increment generator uses a simple incrementing value to create identifiers. Do not use in concurrent applications, as the values may no longer be unique.
Generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table. Do not use in a cluster. - Hibernate 3.3 mapping reference docs
property name="userID"
fieldtype="id"
generator="increment"
update="false";
There are several lesser-known identifier generator types, including:
foreign
- use a foreign CFC's generator configuration
seqhilo
sequence
select
- select the next value from the column denoted in selectKey
uuid
- use Hibernate's UUID generation
Learn transaction management with the ORM Extension
A transaction is any discrete unit of work used to pool database queries and statements to execute at once. This helps us to prevent a failed update from leaving the database in a broken state.
To denote a transaction, we wrap it in the transaction{}
tag:
transaction{
// queries go here
}
The transaction will automatically commit (persist) at the end of the transaction block.
Savepoints are not currently supported on ORM transactions.
Version 6.x release notes for the Ortus ORM Extension
Fixes a regression on where empty string values are coerced to NULL
when an ORM type is declared. Originally reported against 6.4.0
, resolved in 6.5.0
, then regressed in 6.5.1
. - Resolves .
Fixes empty string values coercing to NULL
when no property type is declared. - Resolves , introduced in 6.5.0.
Fixes an incorrect property name lookup for the unsavedValue
persistent property attribute.
Fixes the pre-event listeners to ignore empty strings in entity state properties if the field type is one of string
, character
, or text
. This resolves issues where a preInsert()
or preUpdate()
throws a "can't cast [] to date value" when processing event listeners if a date field (for example) is unpopulated or has an empty default
attribute.
Add the entity name to the exception message when attempting to persist changes from preInsert
or preUpdate
event listeners. The updated exception message is now:
Error populating event state for persistance in [<entity name>] entity pre-event listener method: <error message from Hibernate>
Bump Lucee build dependency to 5.4.4.38
to avoid .
Resolve an by upgrading logback-core
to 1.3.14
. .
New ORMQueryExecute()
alias for the ORMExecuteQuery
. This new alias behaves identically to the ORMExecuteQuery()
method, but is named consistently with the queryExecute()
method.
Fixes custom configuration support via this.ormSettings.ormConfig
.
Fixes named argument support for entityLoad()
-
Fixes named argument support for entityLoadByPK()
-
While not technically a change in ORM functionality, the useDBforMapping
implementation has been greatly improved "under the hood", with tests to boot.
Fixed pre-event listeners to include parent component properties when checking for entity mutations to persist back to the event entity state. This resolves issues with changes made in preInsert()
/preUpdate()
not persisting if the changes are made on a persistent property from a parent component. Resolves .
Refactored nullability checks to occur after pre-event listener methods fire. Resolves
Added context to the error message in CFCGetter
, which handles retrieving entity values from Hibernate code. This improves odd error messages in some edge cases with the Hibernate tuplizer.
Switched the library to use .
Upgrades EHCache version from 2.10.6
to 2.10.9.2
.
Drops an embedded rest-management-private-classpath
directory
Drops a number of (unused) vulnerable jackson and jetty libraries such as jackson-core.
As an added bonus, this reduces the final .lex
extension file size by over 6 MB. 🎉
Note: While it is not 100% clear, .
This brings the Hibernate dependencies up to date (released Feb. 2023), and should not change any CFML-facing features for most users. (See )
See the migration guides for more info:
Due to the Hibernate 5.6 upgrade, if you are using the PostgreSQL81
dialect and have CLOB
columns in your database, .
The default ehcache.xml
for EHCache changed to include and properties to match . Both these values represent default settings in EHCache itself.
Fixes handling of "timezone"
-typed column values. Previously, fields defined with ormtype="timezone"
would neither use the default
value nor allow new values to be set.
Fixes entity state changes in preInsert()
/preUpdate()
listeners for properties with no default
defined.
Lots of java source code cleanup that won't affect the CFML experience, but will aid in faster development and fewer bugs.
Any hibernate exceptions returned during schema generation are once again logged to the Lucee ORM log file.
Dropped the public method from the Dialect class. This method was unused (to my knowledge) and unnecessary.
Switched to to limit false positives. Security vulnerabilities will now be published on the .
Bumped Lucee dependency to to remove vulnerability notices on and . These vulnerabilities are only theoretical, since Lucee is a dependency and not bundled with the extension.
Second-Level Caching
The extension will now throw an error if you try to configure an unsupported cache provider like , , etc. Previously, the extension would silently switch to ehcache if any cache provider besides EHCache was configured.
Hibernate Logging
This version re-enables Hibernate logging via SLF4j and LogBack. Hibernate root and cache loggers are defaulted to level, while SQL logging is set to if is enabled. (Set to .)
OWASP Dependency CVE Scans
The extension now generates a dependency CVE report via . Any known CVEs contained in dependencies ( excluding and -scoped dependencies) will be noted in .
New Repo Layout
Java source moved to
All java classes are now under the package
Dropped the java source format-on-push in favor of format-on-save IDE tooling
New Test Layout
Internal tests rewritten to native Testbox specs
Cloned all ORM tests from the Lucee repository
Updated to TestBox 5.0
New Build (and .jar file) Layout
We re-architected the build to inline most dependencies. I.e. we no longer copy in extension dependencies as (custom-built) OSGI bundles, but instead as compiled classes.
This resolves intermittent issues with bundle resolution and/or duplicate bundle collision upon installing the ORM extension into a Lucee server prior to uninstalling the Lucee Hibernate extension.
This also removes a number of direct dependencies on custom OSGI bundles, thus it is more reliable and will offer easier dependency upgrades with less pain.
Other
The attribute is deprecated in Hibernate 5.x, and is no longer generated on HBM/XML mapping files to avoid Hibernate warning that Use of DOM4J entity-mode is considered deprecated.
The definition file for all built-ins was missed during the conversion to a Maven build. (Since ). This caused the and built-in method calls to be picked up by Lucee core before being routed to this extension. No known errors resulted from this mistake, but we feel embarrassed anyway. 😅
Clear ORM context data once per ORM reload, not once per ORM entity parsing. This should improve ORM startup/reload time and avoid difficult session or cache manager lifecycle issues.
Improve your database performance with secondary caching in Hibernate ORM.
A secondary cache provider is a class which manages a level of caching that is secondary to Hibernate's main caching context - the Hibernate session. A secondary cache enables longer-running cache contexts, more fine-grained control over cache busting, and other performance-related benefits.
The only setting necessary to enable secondary caching is the secondaryCacheEnabled
setting:
this.ormSettings = {
secondaryCacheEnabled : true
};
To configure the caching, specify the path to an XML cache configuration file in cacheConfig
:
this.ormSettings = {
secondaryCacheEnabled: true,
cacheConfig : "./config/ehcache.xml"
};
This ehcache.xml
cache configuration then should look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" name="default">
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120"
maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
<cache
name="Autos"
maxElementsInMemory="20"
overflowToDisk="false"
eternal="true">
</cache>
</ehcache>
Notice how our ehcache.xml
defines a default cache configuration?
<defaultCache
maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120"
maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
We highly recommend adding a cache configuration for each cacheable entity. This will help you optimize caching, and will silence error logs like the below:
WARN: HHH90001006: Missing cache[default-update-timestamps-region] was created on-the-fly. The created cache will use a provider-specific default configuration: make sure you defined one. You can disable this warning by setting 'hibernate.cache.ehcache.missing_cache_strategy' to 'create'
Here's a quick example. Say we have an Autos.cfc
persistent component with caching enabled:
component persistent="true" cacheUse="true"{
// persistent properties...
}
For this entity, we'll want to create a <cache></cache>
entry with a name
attribute that matches the entity name OR our cacheName
component annotation:
<cache
name="Autos"
maxElementsInMemory="20"
overflowToDisk="false"
eternal="true">
</cache>
While there is a cacheProvider
setting, only EHCache (currently) is supported as a secondary cache provider.
this.ormSettings = {
secondaryCacheEnabled : true,
// NOT SUPPORTED!
cacheProvider : "ConcurrentHashMap"
};
Thus, any usage of cacheProvider
other than "ehcache"
will be ignored.
The Ortus ORM Extension offers a number of CFML functions for loading and manipulating entities as well as managing the ORM session:
EntityDelete
allows you to delete the row associated with an instantiated entity from the database:
entityDelete( myEntity );
Only a single argument is accepted - the entity to delete - and the deletion is not persisted until the session is flushed.
entityDelete( myEntity );
// entity (row) still exists in the database.
ormFlush();
// entity (row) is now wiped from the database.
Returns: null
Returns: Array
|Component
|null
Returns: Array
|Component
|null
EntityLoadByPK()
allows you to instantiate an entity using the row identified by a primary key:
var theUser = entityLoadByPk( "User", url.userID );
A third argument, unique
, is documented but not implemented in either the Lucee Hibernate extension or the Ortus ORM Extension.
Returns: Component
|null
EntityMerge()
will merge a "detached" entity (meaning, not connected to any open session) back into the session.
For example, running ormClearSession()
will detach all loaded entities from the session. If you then make changes to an entity via a setter and run ormFlush()
, those entity modifications will not be persisted unless you first merge the entity back to the session:
var detachedAutoEntity = entityLoadByPK( "Auto", "12345" );
// make a change but don't save it
detachedAutoEntity.setModel( "Fusion" );
// clear session - will "detach" the entity
ormClearSession();
// "merge" it back to the session
var merged = entityMerge( detachedAutoEntity );
// changes should be reflected in the new entity
expect( merged.getModel() ).toBe( "Fusion" );
Returns: Component
EntityNameArray()
returns an array of all mapped entity names for the CFML application:
var entityTypes = entityNameArray();
EntityNameArray accepts no arguments.
Returns: Array
True to its name, EntityNameList returns a string list of all mapped entity names for the CFML application:
var entityTypes = entityNameList();
You can pass a string delimiter value as the first argument, if you don't like commas:
var entityTypes = entityNameList( "|" );
Returns: string
The entityNew()
method allows you to create a new instance of a known entity type:
var myCar = entityNew( "Auto" );
You can also pass a struct of properties to populate into the entity:
var myCar = entityNew( "Auto", {
make : "Ford",
model : "Fusion",
id : createUUID()
} );
Note that if you try to populate a property which does not exist, you will get an error:
var myCar = entityNew( "Auto", {
make : "Ford",
model : "Fusion",
propThatDoesntExist : "abc"
} );
This will throw an error: component [Auto] has no function with name [setPROPTHATDOESNTEXIST]
Returns: Component
entityReload()
will reload or refresh the entity state from the database. Local, unpersisted modifications will be replaced with database values.
Here's a quick example:
var myCar = entityLoadByPK( "Auto", "12345" );
// make a change but don't save it
myCar.setModel( "Revuelto" );
// reload the entity
entityReload( myCar );
// our local changes should be replaced with the DB value
expect( myCar.getModel() ).toBe( "Aventador" );
Returns: null
EntitySave()
is how you save entity modifications. The changes will not persist to the database until ormFlush()
is called:
var myCar = entityNew( "Auto", {
make : "Ford",
model : "Fusion",
id : createUUID()
} );
entitySave( myCar );
ormFlush();
EntitySave accepts an optional boolean parameter, forceInsert
, which will tell Hibernate to skip the entity existence check and insert the entity:
var myCar = entityNew( "User", {
username : "Johnny.Appleseed",
password : "McinT0sh"
} );
entitySave( myCar, true );
Most of the time this will be unnecessary.
Returns: null
Returns: Query
Returns: Boolean
Returns: null
Returns: null
Returns: null
Returns: null
Returns: null
Returns: null
Returns: Array
|Struct
|any
Returns: null
Returns: Session
Returns: SessionFactory
Alias for ORMExecuteQuery()
.
Added in v6.4.0
(unreleased, version number subject to change.) - 5600833
Returns: Array
|Struct
|any
Returns: null
Hibernate's secondary cache is a powerful tool that can greatly improve the performance of your application. By storing frequently accessed data in memory, the secondary cache can reduce the number of database queries that need to be made, resulting in faster response times and more efficient use of system resources. Whether you're working on a small project or a large enterprise application, the secondary cache is an essential part of any Hibernate-based system. So if you want to get the most out of your Hibernate application, take advantage of this powerful caching feature!
Please see the caching section for all the wonderful caching strategies you can do with Hibernate.
A secondary cache provider is a class that manages a level of caching secondary to Hibernate's main caching context - the Hibernate session. A secondary cache enables longer-running cache contexts, more fine-grained control over cache busting, and other performance-related benefits.
The only setting necessary to enable secondary caching is the secondaryCacheEnabled
setting:
this.ormSettings = {
secondaryCacheEnabled : true
};
To configure the caching, specify the path to an XML cache configuration file in cacheConfig
:
this.ormSettings = {
secondaryCacheEnabled: true,
cacheConfig : "./config/ehcache.xml"
};
This ehcache.xml
cache configuration then should look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" name="default">
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120"
maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
<cache
name="Autos"
maxElementsInMemory="20"
overflowToDisk="false"
eternal="true">
</cache>
</ehcache>
While there is a cacheProvider
setting, only EHCache (currently) is supported as a secondary cache provider.
this.ormSettings = {
secondaryCacheEnabled : true,
// NOT SUPPORTED!
cacheProvider : "ConcurrentHashMap"
};
Thus, any usage of cacheProvider
other than "ehcache"
will be ignored.
Savepoints are not currently supported on ORM transactions.
Entity properties are how we map table columns to CFML object values. You can specify an entity property by setting persistent="true"
on any property
inside a persistent CFML component:
component persistent="true"{
property name="name" type="string" persistent="true";
}
By default, all properties inside a persistent=true
component are assumed to be persistent as well. Thus, we can skip the persistent=true
annotation for brevity:
component persistent="true"{
property name="name" type="string";
}
persistent
boolean
Define this property as a persistent property. If false
, this property will be completely ignored in Hibernate / the ORM extension.
name
string
Property name
default
string
Default value for the property. This only reaches the CFML engine, and does not affect creation of the entity table.
column
string
Table column where the property value is stored.
dbDefault
string
Set the default value for the property.
Make sure you quote datetime dbdefault
values to avoid invalid date errors in MySQL:
property
name ="createdOn"
ormtype ="datetime"
dbdefault="'2016-10-10'";
There are several "type" concepts and annotations you may need to use to keep the proper format when persisting or retrieving table data. While these may look similar, they are not equivalent.
type
date
,numeric
CFML data type
fieldtype
column
,id
, one-to-many
Denote a special type of field, like an identifier or relationship field.
ormType
big_decimal
,timestamp
Data type for the database value.
sqlType
nvarchar
A vendor-specific SQL type used for table creation only. This can normally be ignored.
The fieldtype
attribute allows you to define the behavior and purpose of this property:
property
name="userID"
fieldtype="id"
type="string";
There are several options for the fieldtype
value:
column
- By far the most common, this is the default behavior of every field.
id
- Notes this field as the primary key or part of the a composite key.
collection
- Denote a collection of items - this could represent a struct, an array
version
- Denote a version field, useful for optimistic locking on an entity
timestamp
- Denote a timestamp field
one-to-one
- Denote a one-to-one relationship
one-to-many
- Denote a one-to-many relationship
many-to-one
- Denote a many-to-one relationship
many-to-many
- Denote a many-to-many relationship
primary
- Not currently used.
generator
string
One of increment
,identity
,native
,seqhilo
,uuid
,guid
,select
,foreign
, assigned
params
struct
, string
{ "table: "uuid_table", column: "uuid_value_column" }
sequence
string
selectkey
string
Only used with generator=select
. Specify the select key to use when selecting sequence values from the database.
generated
string
always
, insert
, never
.
insert
boolean
Allow inserting values to new rows.
update
boolean
Allow value updates on existing rows.
notnull
boolean
Specify a not-null constraint.
uniquekey
string
Specify a key name for a unique constraint. Useful for defining a unique constraint across multiple columns.
unique
boolean
Specify a unique constraint. Default false
.
validateParams
string
Not currently supported
validate
string
Not currently supported
These property annotations are used when setting fieldtype="collection"
and collectiontype
is either struct
or map
.
structkeycolumn
string
structkeytype
string
elementtype
string
elementcolumn
string
table
string
Define the table where the foreign items are stored.
lazy
boolean
, string
Define a lazy association retrieval type. One of true
, false
, extra
.
inverse
boolean
Specify that the association "owner" is referenced and managed on the opposite entity - not this current entity.
inversejoincolumn
string
Specify the join column on the associated entity. For inverse
associations only.
linkschema
string
Specify the schema name of the link table
linkcatalog
string
Specify the catalog name of the link table
linktable
string
Specify the name of the link table
missingRowIgnored
boolean
Do not throw an error if a foreign key has no match in the foreign entity
fkcolumn
string
orderby
string
fetch
string
cascade
string
constrained
boolean
Only valid for one-to-one
relationships.
optimisticLock
boolean
Enable optimistic locking on this association. One of all
, dirty
, version
, none
.
mappedby
string
Specify the property on the association that maps to this entity.
cfc
string
Specify the CFC location of the foreign entity.
joinColumn
string
Join the foreign entity on this column in this entity.
where
string
Arbitrary SQL where clause for the relation.
singularname
Not currently supported
scale
integer
length
integer
precision
integer
formula
string
Define this property as a computed property by using a SQL query to determine the property value. Formula properties cannot be modified.
index
string
Key name for a property value index.
cacheUse
string
Define a cache type to use for this property. One of read-only
, nonstrict-read-write
, read-write
, or transactional
.
cacheName
string
Set the name of the cache to use for this property.
unSavedValue
string
Set a value to use for newly instantiated objects.
property name="totalPosts"
ormType="integer"
formula="(
SELECT COUNT(*)
FROM posts
WHERE posts.FK_user = id
)";
Easily configure Hibernate with CFML
The ORM can be configured by a struct of settings set in this.ormSettings
in your main Application.cfc
:
The full list of available properties you can use to configure the ORM are the following:
By using the ormsettings.dialect
you can tell Hibernate which specific database dialect to use for building queries. By default, Hibernate tries to inspect the datasource and define it for you. 95% of the time, this works. However, if you want a specific one, then you can use the following names or a fully qualified Java class name.
Here is an example configuration from the popular ContentBox Modular CMS application
You can define your own naming convention for table and column names by pointing this.ormSettings.namingStrategy
to a custom CFC path:
The UnderscoreNamingStrategy.cfc
component should then define two methods: getTableName()
and getColumnName()
:
component{
this.ORMenabled = true;
this.ormSettings = {
// << Here Be ORM Configuration! 🤪 >>
};
}
autoGenMap
true
Specifies whether ColdFusion should automatically generate entity mappings for the persistent CFCs. If autogenmap=false
, the mapping should be provided in the form of .HBMXML
files.
autoManageSession
true
Allows the engine to manage the Hibernate session. It is recommended not to let the engine manage it for you.
Use transaction
blocks in order to demarcate your regions that should start, flush and end a transaction.
https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#transactions
cacheConfig
true
Specifies the location of the configuration file that the secondary cache provider should use. This setting is used only when secondaryCacheEnabled=true
. See Secondary Cache below.
cacheProvider
"ehcache"
Specifies the cache provider that ORM should use as a secondary cache. The values can be:
Ehcache
ConcurrentHashMap
The fully qualified name of the class for any other cache provider.
See Secondary Cache below.
catalog
Specifies the default Database Catalog that ORM should use.
cfclocation
empty
Specifies the directory (or array of directories) that should be used to search for persistent CFCs to generate the mapping.
Always specify it or pay a performance startup price.
Important:
If it is not set, the extension looks at the application directory, its sub-directories, and its mapped directories to search for persistent CFCs.
datasource
application.datasource
This setting defines the data source to be utilized by the ORM. If not used, defaults to the this.datasource
in the Application.cfc
dbcreate
none
update
: Creates the database according to your ORM model. It only does incremental updates. It will never remove tables, indexes, etc.
dropcreate
: Same as above but it destroys the database if it has ny content and recreates it every time the ORM is reloaded.
none
: Does not change the database at all.
dialect
autodiscover
The dialect to use for your database. By default Hibernate will introspect the datasource and try to figure it out. See the dialects section below. You can also use the fully Java qualified name of the class. See the dialects section below.
eventHandling
false
If true, then it enables the ORM event callbacks in entities and globally via the eventHandler
eventHandler
The CFC path of the CFC that will manage the global ORM events.
flushAtRequestEnd
true
Specifies if an orm flush should be called automatically at the end of a request. In our opinion this SHOULD never be true. A database persistence should be done via transaction
tags and good transaction demarcation.
logSQL
false
Specifies if the SQL queries should be logged to the console.
namingstrategy
default
Defines the naming convention to use on table and column names.
- default
: Uses the table or column names as is
- smart
: This strategy changes the logical table or column name to uppercase.
- CFC PATH
: Use your own CFC to determine naming - see Custom Naming Strategy.
ormconfig
The path to a custom Hibernate configuration file: - hibernate.properties - hibernate.cfc.xml Please see Custom Hibernate Config
savemapping
false
If enabled, the ORM will create the Hibernate mapping XML (*.hbmxml
) files alongside the entities. This is great for debugging your entities and relationships.
schema
The default database schema to use
secondaryCacheEnabled
false
Enable the secondary cache or not. See our Caching section.
skipCFCWithError
false
If true
, then the ORM will ignore CFCs that have compile time errors in them. Use false
to throw exceptions.
sqlScript
Path to a SQL script file that will be executed after the ORM is initialized. A great way to seed a database.
useDBForMapping
true
Specifies whether the database has to be inspected to identify the missing information required to generate the Hibernate mapping.
The database is inspected to get the column data type, primary key and foreign key information.
Cache71
Support for the Caché database, version 2007.1.
CockroachDB192
Support for the CockroachDB database version 19.2.
CockroachDB201
Support for the CockroachDB database version 20.1.
CUBRID
Support for the CUBRID database, version 8.3. May work with later versions.
DB2
Support for the DB2 database, version 8.2.
DB297
Support for the DB2 database, version 9.7.
DB2390
Support for DB2 Universal Database for OS/390, also known as DB2/390.
DB2400
Support for DB2 Universal Database for iSeries, also known as DB2/400.
DB2400V7R3
Support for DB2 Universal Database for i, also known as DB2/400, version 7.3
DerbyTenFive
Support for the Derby database, version 10.5
DerbyTenSix
Support for the Derby database, version 10.6
DerbyTenSeven
Support for the Derby database, version 10.7
Firebird
Support for the Firebird database
FrontBase
Support for the Frontbase database
H2
Support for the H2 database
HANACloudColumnStore
Support for the SAP HANA Cloud database column store.
HANAColumnStore
Support for the SAP HANA database column store, version 2.x. This is the recommended dialect for the SAP HANA database. May work with SAP HANA, version 1.x
HANARowStore
Support for the SAP HANA database row store, version 2.x. May work with SAP HANA, version 1.x
HSQL
Support for the HSQL (HyperSQL) database
Informix
Support for the Informix database
Ingres
Support for the Ingres database, version 9.2
Ingres9
Support for the Ingres database, version 9.3. May work with newer versions
Ingres10
Support for the Ingres database, version 10. May work with newer versions
Interbase
Support for the Interbase database.
JDataStore
Support for the JDataStore database
McKoi
Support for the McKoi database
Mimer
Support for the Mimer database, version 9.2.1. May work with newer versions
MySQL5
Support for the MySQL database, version 5.x
MySQL5InnoDB
Support for the MySQL database, version 5.x preferring the InnoDB storage engine when exporting tables.
MySQL57InnoDB
Support for the MySQL database, version 5.7 preferring the InnoDB storage engine when exporting tables. May work with newer versions
MariaDB
Support for the MariaDB database. May work with newer versions
MariaDB53
Support for the MariaDB database, version 5.3 and newer.
Oracle8i
Support for the Oracle database, version 8i
Oracle9i
Support for the Oracle database, version 9i
Oracle10g
Support for the Oracle database, version 10g
Pointbase
Support for the Pointbase database
PostgresPlus
Support for the Postgres Plus database
PostgreSQL81
Support for the PostgrSQL database, version 8.1
PostgreSQL82
Support for the PostgreSQL database, version 8.2
PostgreSQL9
Support for the PostgreSQL database, version 9. May work with later versions.
Progress
Support for the Progress database, version 9.1C. May work with newer versions.
SAPDB
Support for the SAPDB/MAXDB database.
SQLServer
Support for the SQL Server 2000 database
SQLServer2005
Support for the SQL Server 2005 database
SQLServer2008
Support for the SQL Server 2008 database
Sybase11
Support for the Sybase database, up to version 11.9.2
SybaseAnywhere
Support for the Sybase Anywhere database
SybaseASE15
Support for the Sybase Adaptive Server Enterprise database, version 15
SybaseASE157
Support for the Sybase Adaptive Server Enterprise database, version 15.7. May work with newer versions.
Teradata
Support for the Teradata database
TimesTen
Support for the TimesTen database, version 5.1. May work with newer versions
// THE CONTENTBOX DATASOURCE NAME
this.datasource = "contentbox";
// ORM SETTINGS
this.ormEnabled = true;
// cfformat-ignore-start
this.ormSettings = {
// ENTITY LOCATIONS, ADD MORE LOCATIONS AS YOU SEE FIT
cfclocation : [
// If you create your own app entities
"models",
// The ContentBox Core Entities
"modules/contentbox/models",
// Custom Module Entities
"modules_app",
// Custom Module User Entities
"modules/contentbox/modules_user"
],
// THE DIALECT OF YOUR DATABASE OR LET HIBERNATE FIGURE IT OUT, UP TO YOU TO CONFIGURE.
dialect : request.$systemHelper.getSystemSetting( "ORM_DIALECT", "" ),
// DO NOT REMOVE THE FOLLOWING LINE OR AUTO-UPDATES MIGHT FAIL.
dbcreate : "update",
secondarycacheenabled: request.$systemHelper.getSystemSetting( "ORM_SECONDARY_CACHE", false ),
cacheprovider : request.$systemHelper.getSystemSetting( "ORM_SECONDARY_CACHE", "ehCache" ),
logSQL : request.$systemHelper.getSystemSetting( "ORM_LOGSQL", false ),
sqlScript : request.$systemHelper.getSystemSetting( "ORM_SQL_SCRIPT", "" ),
// ORM SESSION MANAGEMENT SETTINGS, DO NOT CHANGE
flushAtRequestEnd : false,
autoManageSession : false,
// ORM EVENTS MUST BE TURNED ON FOR CONTENTBOX TO WORK DO NOT CHANGE
eventHandling : true,
eventHandler : "cborm.models.EventHandler",
// THIS IS ADDED SO OTHER CFML ENGINES CAN WORK WITH CONTENTBOX
skipCFCWithError : true,
// TURN ON FOR Debugging if ORM mappings are not working.
savemapping : false
};
this.ormSettings = {
// ...
namingStrategy : "models.orm.UnderscoreNamingStrategy"
}
// models/orm/UnderscoreNamingStrategy.cfc
component{
function getTableName( string tableName ){
// funky table name conversion here...
}
function getColumnName( string columnName ){
// funky column name conversion here...
}
}
Release history for all versions of the Ortus ORM Extension
In this section, you will find the release notes for each version we release under this major version. If you are looking for the release notes of previous major versions, use the version switcher at the top left of this documentation book. Here is a breakdown of our major version releases.
A new layout, cache and logging improvements, and much more.
Our initial fork of the Lucee Extension.
More info on this documentation gitbook for the Ortus ORM Extension
The source code for this book is hosted in GitHub:
You can freely contribute to it and submit pull requests. The contents of this book is copyright by Ortus Solutions, Corp and cannot be altered or reproduced without author's consent. All content is provided "As-Is" and can be freely distributed.
The majority of code examples in this book are done in cfscript
.
The majority of code generation and running of examples are done via CommandBox: The ColdFusion (CFML) CLI, Package Manager, REPL - https://www.ortussolutions.com/products/commandbox
Both Lucee Server and Hibernate ORM are licensed under the LGPL v2.1.
The information in this book is distributed “as is”, without warranty. The author and Ortus Solutions, Corp shall not have any liability to any person or entity with respect to loss or damage caused or alleged to be caused directly or indirectly by the content of this training book, software and resources described in it.
We highly encourage contribution to this book and our open source software. The source code for this book can be found in our GitHub repository where you can submit pull requests.
10% of the proceeds of this book will go to charity to support orphaned kids in El Salvador - https://www.harvesting.org/. So please donate and purchase the printed version of this book, every book sold can help a child for almost 2 months.
Shalom Children’s Home is one of the ministries that is dear to our hearts located in El Salvador. During the 12 year civil war that ended in 1990, many children were left orphaned or abandoned by parents who fled El Salvador. The Benners saw the need to help these children and received 13 children in 1982. Little by little, more children came on their own, churches and the government brought children to them for care, and the Shalom Children’s Home was founded.
Shalom now cares for over 80 children in El Salvador, from newborns to 18 years old. They receive shelter, clothing, food, medical care, education and life skills training in a Christian environment. The home is supported by a child sponsorship program.
We have personally supported Shalom for over 6 years now; it is a place of blessing for many children in El Salvador that either have no families or have been abandoned. This is good earth to seed and plant.
Entity relationships let you define an association between two entity types.
Every relationship property must define the state of the relationship. In each case, the relationship property is defined in an entity which represents the "left" side of the relationship. If you bring a Posts
relationship into the User
entity, then from the User
entity, the User
is the left side of the relationship, and Posts
is the right.
A one to one relationship couples a single row on the left side to a single row on the right side:
property name="Contact"
fieldtype="one-to-one"
cfc="Contact";
One to one relationships can be seen as simply extending the entity with additional information. Since there is no possibility of multiple records per entity instance, it may be worthwhile to set lazy=false
to fetch the related entity along with any entity load:
property name="Contact"
fieldtype="one-to-one"
cfc="Contact"
lazy="false";
A one to many relationship couples a single row on the left side (the "one") to multiple rows on the right side (the "many"):
component entityName="User" persistent="true"{
property name="posts"
fieldtype="one-to-many"
cfc="Post";
}
You'll normally want to set lazy="true"
to (for example) avoid fetching every Post on a User:
property name="posts"
fieldtype="one-to-many"
cfc="Post"
lazy="true";
A many to one relationship couples multiple rows on the left side (the "many") to a single row on the right side( the "one"):
component entityName="Post" persistent="true"{
property name="Authors"
fieldtype="many-to-one"
cfc="User";
}
Thus, a single Post can have only one Author... but an author (or a User, really) can have many Posts.
A many to many relationship allows each entity to relate to multiple rows on the opposite side. A good example might be a blog which allows multiple authors for a single blog post. Each blog post can then have multiple authors, and each author has (likely) written multiple blog posts:
component entityName="User" persistent="true"{
property name="posts"
fieldtype="many-to-many"
cfc="Post"
linktable="user_posts_link";
}
The linktable
attribute is required on a many-to-many
property to set the locationfor storing join records. You can further alter the storage location via linkschema
and linkcatalog
:
property name="posts"
fieldtype="many-to-many"
cfc="Post";
linkschema="blog"
linkcatalog="dbo";
On many* relationships like one-to-many
, many-to-one
, etc., you'll be manipulating the full set of items via the property name: hasAuthors()
, setAuthors()
, etc. However, this plural terminology becomes weird when used on a single item addition or removal, such as addAuthors( Author )
. For this reason, you can define a singularName=STRING
attribute to clean up your entity manipulation:
property name="Authors"
singularName="Author"
fieldtype="many-to-one"
cfc="User";
Man, you wanna get lazy? How about don't-fetch-the-data-until-you-need-it kinda lazy?
Using lazy=true
, we can tell Hibernate not to fetch the relational data unless and until a method call such as Authors()
attempts to load it. This greatly improves performance on page requests which do not need that particular data:
property name="posts"
fieldtype="one-to-many"
cfc="Post"
lazy="true";
We can also use lazy=extra
to only retrieve the rows that we touch. This may work well if we commonly only access a few rows out of the full set of child items:
property name="posts"
fieldtype="one-to-many"
cfc="Post"
lazy="extra";
Easily run actions on entity insertion, update, and more with event listeners
Hibernate ORM allows reacting to various events in the session lifecycle such as onPreInsert
, onPostUpdate
, onFlush
, etc. You can enable event handling in CFML by setting eventHandling
to true
in your this.ormSettings
struct:
this.ormSettings = {
eventHandling: true
};
This will enable two different event listener types for listening to Hibernate events:
Listen to all events across all entities via the global event handler
Listen to specific events on a specific entity via entity event handler methods
To use a global event handler, you must set a path to the global event handler using the eventHandler
setting:
this.ormSettings = {
eventHandling: true,
eventHandler : "path/to/global/EventHandler.cfc"
};
The EventHandler.cfc
must then contain function definitions matching the ORM events you wish to listen for.
Currently, the available event names are:
onFlush
preLoad
postLoad
preInsert
postInsert
preUpdate
postUpdate
preDelete
onDelete
postDelete
onEvict
onClear
onDirtyCheck
onAutoFlush
Here's an example of an EventHandler configured for all events:
component {
public component function init(){
return this;
}
function onFlush( entity ) {
// Do something upon function call
}
function preLoad( entity ){
// Do something upon function call
}
function postLoad( entity ){
// Do something upon function call
}
function preInsert( entity ){
// Do something upon function call
}
function postInsert( entity ){
// Do something upon function call
}
function preUpdate( entity, Struct oldData ){
// Do something upon function call
}
function postUpdate( entity ){
// Do something upon function call
}
function preDelete( entity ){
// Do something upon function call
}
function onDelete( entity ) {
// Do something upon function call
}
function postDelete( entity ) {
// Do something upon function call
}
function onEvict() {
// Do something upon function call
}
function onClear( entity ) {
// Do something upon function call
}
function onDirtyCheck( entity ) {
// Do something upon function call
}
function onAutoFlush( entity ) {
// Do something upon function call
}
}
You can also listen to events on a specific entity at the entity level by adding methods to the entity (component) itself:
component persistent="true"{
function preInsert( entity ){
setDateCreated( now() );
}
function preUpdate( entity ){
setDateModified( now() );
}
}
Here is the full list of event types which can be listened to in entity event listeners:
preLoad
postLoad
preInsert
postInsert
preUpdate
postUpdate
preDelete
onDelete
postDelete
Version 5.x release notes for the Ortus ORM Extension
We now set the JAXB system property based on the JRE version. If less than JRE 11, we set . If JRE 11 or greater, we set .
This prevents the following warning from being logged on each ORM method call:
WARNING: Using non-standard property: javax.xml.bind.context.factory. Property javax.xml.bind.JAXBContextFactory should be used instead.
See OOE-3.
We now set a System property to ensure the JAXB API can find its implementation in CommandBox environments. This may trigger a log message, but shouldn't cause any concern. Vanilla Tomcat installations may need to overwrite or clear this property. LDEV-4276
Improved logo for Lucee admin 🤩
Entity changes made in and do not persist OOE-2
Switched to Maven for a faster, more stable build process
Improved entity event listeners for a much speedier ORM startup (8924b58a9058d296e2a783ccfabbf90e26dc9c1b)
New and Improved logo for Lucee admin visibility (10bdf56a7a78f0221ab1a6e66a5512a92819e5b7)
Entity has no state when listener method (, for example) is fired (014814263b5d31b8bac4c17479c2ca731ceb4e7c, OOE-1)
Upgraded dom4j library from 1.6.1 to 2.1.4. This removes two potential vulnerabilities in dom4j's XML parsing capabilities.
ORMExecuteQuery ignores argument if struct is passed
Adds support for - LDEV-3525
Adds javadocs auto-published to apidocs.ortussolutions.com
ORM events not firing (LDEV-4308)
Session close on transaction end (LDEV-4017)
length not used on varchar fields (LDEV-4150)
Dramatic improvements in initialization performance
Cuts ORM reload time by 60%
Better build/test documentation
Improved maintenance and build docs
Learn the basics of modeling ORM entities
A persistent entity is a CFML component that is marked as a database entity via the persistent
annotation upon the component definition:
component persistent="true"{
}
By default, the entity name will be the CFC file name - minus the file extension, of course. We can modify the entity name via the entityname
annotation:
component persistent="true" entityname="Author" {
}
And the table name via the table
annotation:
component persistent="true" entityname="Author" table="authors" {
}
Here's the full list of available annotations for a persistent component:
persistent
boolean
false
Mark this component as an ORM entity
entityname
string
Set a custom entity name which is different than the CFC name
table
string
Specify the database table name
schema
string
Specify the database schema name.
catalog
string
Specify the database catalog name.
dynamicinsert
Specifies whether INSERT SQL is to be generated at runtime. Only those columns whose values are not null are included in the SQL.
dynamicinsert
boolean
false
Specifies whether INSERT SQL is to be generated at runtime. Only those columns whose values are not null are included in the SQL.
dynamicupdate
boolean
false
Specifies whether UPDATE SQL is to be generated at runtime. Only those columns whose values are not null are included in the SQL.
readonly
boolean
false
Specify whether table is readonly or not
selectbeforeupdate
boolean
Specify whether Hibernate should never perform an SQL UPDATE unless it is certain that an object is actually modified. In cases when a transient object is associated with a new session using update(), Hibernate performs an extra SQL SELECT to determine if an UPDATE is actually required.
optimisticlock
string
Determines the locking strategy. It can be any one of: all
,dirty
,version
,none
batchsize
integer
An integer value that specifies the number of records to be retrieved at a single instance.
lazy
boolean
true
Whether loading is to be done lazily or not.
rowid
string
Specify the row id
discriminatorColumn
string
Use this attribute to define the discriminator column to be used in inheritance mapping
discriminatorValue
string
Use this attribute to define the discriminator value to be used in inheritance mapping
joinColumn
string
Define a join column for inheritance mapping
embedded
boolean
Marks CFC as embedded, used when a CFC has an embedded object which also needs to be persisted along with the parent's data
cacheUse
string
Specify the caching strategy to be used for caching this component's data in the secondary cache: read-only
cacheName
string
Specify the name of the secondary cache
saveMapping
boolean
false
Specifies whether the generated Hibernate mapping file has to be saved to disk. If you set the value to true, the Hibernate mapping XML file is saved as {CFC name}.hbm.xml
in the same directory as the CFC.
datasource
string
Name a specific datasource to persist this entity to.