Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
The ORM module supports the concept of dynamic finders and counters for ColdFusion ORM entities. A dynamic finder/counter looks like a real method but it is a virtual method that is intercepted by via onMissingMethod
. This is a great way for you to do finders and counters using a programmatic and visual representation of what HQL to run.
This feature works on the Base ORM Service, Virtual Entity Services and also Active Entity services. The most semantic and clear representations occur in the Virtual Entity Service and Active Entity as you don't have to pass an entity name around.
Most of the Hibernate extensions like criteria builders and even some dynamic finders and counters will have to rely on the underlying Java type in order to work. You do this in ColdFusion by using the javaCast()
function available to you. So if you are using a primary key that is an Integer
you might have to do the following in order to match your variable to the underlying Java type:
If you do not type it, then ColdFusion assumes it is a string and passes a string to Hibernate which will throw an exception as it is supposed to be an integer.
We have created two methods available to you in the base orm service, virtual service, criteria builders, active entity, etc to help you with these translations by automatically casting the values for you:
So instead of casting it manually you can just let us do the work:
We have three types of dynamic finders and counters:
findBy
: Find ONE entity according to method signature, if more than one record is found an exception is thrown
findAllBy
: Find ALL entities according to method signature
countBy
: Give you a count of entities according to method signature
Let's say you have the following entity:
Then we could do the following:
You can also use the virtual entity service instead of active entity.
If you just use a vanilla Base ORM Service, then the first argument must be the entityName
:
A method expression is made up of the prefixes: findBy, findAllBy, countBy
followed by the expression that combines a query upon one or more properties:
If a conditional keyword is not passed, we assume you want equality. Remember that!
IMPORTANT The ? means that you can concatenate the same pattern over and over again.
The available conditionals in ColdBox are:
LessThanEquals
- Less than or equal to passed value
LessThan
- Less than to passed value
GreaterThanEquals
- Greater than or equal to passed value
GreaterThan
- Greater than to passed value
Like
- Equivalent to the SQL like expression
NotEqual
- Not equal to the passed value
isNull
- The property must be null
isNotNull
- The property must not be null
NotBetween
- The property value must not be between two values
Between
- The property value must be between two values
NotInList
- The property value must not be in the passed in simple list or array
inList
- The property value must be in the passed in simple list or array
The only valid operators are:
And
Or
If you pass a structure as the last argument to your dynamic finder/counter call, we will consider that by convention to be your query options.
The valid query options are:
ignorecase
: Ignores the case of sort order when you set it to true. Use this option only when you specify the sortorder parameter.
maxResults
: Specifies the maximum number of objects to be retrieved.
offset
: Specifies the start index of the resultset from where it has to start the retrieval.
cacheable
: Whether the result of this query is to be cached in the secondary cache. Default is false.
cachename
: Name of the cache in secondary cache.
timeout
: Specifies the timeout value (in seconds) for the query
There are a few properties you can instantiate a base service with or set them afterwards that affect operation. Below you can see a nice chart for them:
So if I was to base off my services on top of this gem, I can do this:
The BaseORMService is a core model CFC of the module that will provide you with a tremendous gammut of API methods to interact with ColdFusion ORM Entities.
The idea behind this support class is to provide a very good base or parent service layer that can interact with ColdFusion ORM via hibernate and entities inspired by Spring's Hibernate Template support. This means that you don't need to create a service layer CFC in order to work with ORM entities.
It provides tons of methods for query executions, paging, transactions, session metadata, caching and much more. You can either use the class on its own or create more concrete service layers by inheriting from this class.
In order to get started with the base ORM service you need to know how to get access to it. You can do this via WireBox injection DSL or by the model's ID.
The module also registers a new WireBox DSL called entityservice
which can produce virtual or base orm entity services that you can use to inject into your own event handlers or models.
entityservice
- Inject a global ORM service
entityservice:{entityName}
- Inject a Virtual entity service according to entityName
You can also request a Base ORM Service via the registered WireBox ID which is exactly the same as the entityService
DSL:
Once you have access to the injected base ORM service, you can use it in all of its glory.
Important Please check out the latest API Docs for the latest methods and functionality.
Once you have a reference to the base ORM service then you can use any of its methods to interact with ORM entities. The drawback about leveraging the base ORM model is that you cannot add custom functions to it or tell it to work on a specific entity for all operations. It is a very simple API, but if you need more control then we can start using other approaches shown below.
We also have a virtual service layer that can be mapped to specific entities and create entity driven service layers virtually. Meaning you don't have to be passing any entity names to the API methods to save you precious typing time.
This is where you create your own CFC that inherits from our Base ORM Service model and either add or override methods. You can read more about it in our Concrete Services Section
Let's say you are using the virtual services and base ORM service but you find that they do not complete your requirements, or you need some custom methods or change functionality. Then you will be building concrete services that inherit from the base or virtual entity services. This is the very purpose of these support classes as most of the time you will have custom requirements and your own style of coding. Here is a custom AuthorService
we created:
Then you can just inject your concrete service in your handlers, or other models like any other normal model object.
Returns the count by passing name value pairs as arguments to this function. One mandatory argument is to pass the 'entityName'. The rest of the arguments are used in the where class using AND notation and parameterized. Ex: countWhere(entityName="User",age="20");
This function returns numeric
Return the count of instances in the DB for the given entity name. You can also pass an optional where statement that can filter the count. Ex: count('User','age > 40 AND name="joe"'). You can even use named or positional parameters with this method: Ex: count('User','age > ? AND name = ?',[40,"joe"])
This function returns numeric
Update: Please use our updated objects instead so you can have much more control, granularity and coolness!
Do a hibernate criteria based query with projections. You must pass an array of criterion objects by using the Hibernate Restrictions object that can be retrieved from this service using getRestrictions(). The Criteria interface allows to create and execute object-oriented queries. It is powerful alternative to the HQL but has own limitations. Criteria Query is used mostly in case of multi criteria search screens, where HQL is not very effective.
This function returns any which can be an array or query depending on passed arguments
Property
Type
Required
Default
Description
queryCacheRegion
string
false
ORMService.defaultCache
The name of the secondary cache region to use when doing queries via this base service
useQueryCaching
boolean
false
false
To enable the caching of queries used by this base service
eventHandling
boolean
false
true
Announce interception events on new() operations and save() operations: ORMPostNew, ORMPreSave, ORMPostSave
useTransactions
boolean
false
true
Enables ColdFusion safe transactions around all operations that either save, delete or update ORM entities
defaultAsQuery
boolean
false
true
The bit that determines the default return value for list(), createCriteriaQuery() and executeQuery() as query or array of objects
Key | Type | Required | Default |
entityName | string | Yes | --- |
Key | Type | Required | Default |
entityName | string | Yes | --- |
useQueryCaching | boolean | No | Same as BaseService |
queryCacheRegion | string | No | Same as BaseService |
Key | Type | Required | Default | Description |
entityName | any | Yes | --- | The entity to run count projections on |
criteria | array | No | [] | The array of hibernate criterion to use for the projections |
Key | Type | Required | Default | Description |
entityName | string | Yes | --- |
where | string | No |
params | any | No | strucnew() | Named or positional parameters |
Key | Type | Required | Default | Description |
entityName | any | Yes | --- | The entity name to run the criteria on |
criteria | array | No | [] | Array of criterion |
sortOrder | string | No |
offset | numeric | No | 0 |
max | numeric | No | 0 |
timeout | numeric | No | 0 |
Ignore | boolean | No | false |
asQuery | boolean | No | true |
Delete by using an HQL query and iterating via the results, it is not performing a delete query but it actually is a select query that should retrieve objects to remove
This function returns void
| Key | Type | Required | Default | description | | query | string | Yes | --- | | | params | any | No | --- | | | max | numeric | No | 0 | | | offfset | numeric | No | 0 | | | flush | boolean | No | false | | | transactional | boolean | No | From Property | Use transactions or not | | datasource | string | false | | The datasource to use or use the default datasource |
Deletes entities by using name value pairs as arguments to this function. One mandatory argument is to pass the 'entityName'. The rest of the arguments are used in the where class using AND notation and parameterized. Ex: deleteWhere(entityName="User",age="4",isActive=true);
This function returns numeric
Key
Type
Required
Default
Description
entityName
string
Yes
---
transactional
boolean
No
From Property
Use transactions not
Key
type
Required
Default
Description
entity
any
Yes
---
flush
boolean
No
false
transactional
boolean
No
From Property
Use Transactions or not
Key
Type
Required
Default
Description
entityName
string
Yes
---
The entity to purge
flush
boolean
No
false
transactional
boolean
No
From Property
Use transactions or not
Key
Type
Required
Default
Description
entityName
string
Yes
---
The name of the entity to delte
id
any
Yes
---
A single ID or array of IDs
flush
boolean
No
false
transactional
boolean
No
From Property
Use transactions not
Key
Type
Required
Default
entityName
string
Yes
---
collectionName
string
No
---
id
any
No
---
Key | Type | Required | Default | Description |
entityName | string | Yes | --- |
criteria | struct | Yes | --- | A structure of criteria to filter on |
sortOrder | string | false | --- | The sort ordering |
Retrieve all the instances from the passed in entity name using the id argument if specified. The id can be a list of IDs or an array of IDs or none to retrieve all. If the id is not found or returns null the array position will have an empty string in it in the specified order
This function returns array of entities found
Key
Type
Required
Default
Description
cacheName
string
No
---
datasource
string
No
---
The datasource to use
Key
Type
Required
Default
Description
entity
any
Yes
---
Key
Type
Required
Default
Description
entityName
any
Yes
---
id
any
Yes
---
Key
Type
Required
Default
Description
query
string
Yes
---
params
any
No
[runtime expression]
offset
numeric
No
0
max
numeric
No
0
timeout
numeric
No
0
asQuery
boolean
No
true
unique
boolean
No
false
Return a unique result
datasource
string
No
---
Use a specific or default datasource
Key
Type
Required
Default
Description
query
string
No
---
params
any
No
[runtime expression]
offset
numeric
No
0
max
numeric
No
0
timeout
numeric
No
0
ignoreCase
boolean
No
false
example
any
No
---
Key
Type
Required
Default
Description
example
any
Yes
---
The entity sample
unique
boolean
false
false
Return an array of sample data or none
Key | Type | Required | Default | Description |
entityName | string | Yes | --- |
criteria | struct | Yes | --- | A structure of criteria to filter on |
Key | Type | Required | Default | Description |
entityName | string | Yes | --- |
id | any | Yes | --- |
returnNew | boolean | false | true | If id is 0 or empty and this is true, then a new entity is returned. |
Key | Type | Required | Default | Description |
entityName | string | Yes | --- |
id | any | No | --- |
sortOrder | string | false | --- | The sort orering of the array |
Key | Type | Required | Default | Description |
entityName | any | Yes | --- |
Key | Type | Required | Default | Description |
query | string | No | --- |
params | any | No | [runtime expression] |
example | any | No | --- |
Key | Type | Required | Default | Description |
entityName | string | Yes | --- |
List all of the instances of the passed in entity class name. You can pass in several optional arguments like a struct of filtering criteria, a sortOrder string, offset, max, ignorecase, and timeout. Caching for the list is based on the useQueryCaching class property and the cachename property is based on the queryCacheRegion class property.
This function returns array
Key
Type
Required
Default
Description
datasource
string
false
---
The default or specific datasource use
Key
Type
Required
Default
Description
entityName
string
Yes
---
Key
Type
Required
Default
Description
entityName
string
Yes
---
Key
Type
Required
Default
Description
datasource
string
false
---
The default or specific datasource to use
Key
Type
Required
Default
Description
entityName
string
Yes
---
criteria
struct
No
[runtime expression]
sortOrder
string
No
offset
numeric
No
0
max
numeric
No
0
timeout
numeric
No
0
ignoreCase
boolean
No
false
asQuery
boolean
No
true
Get a brand new criteria builder object to do criteria queries with (See ORM:CriteriaBuilder)
This function returns This function returns coldbox.system.orm.hibernate.CriteriaBuilder
Populate an entity with a structure of name-value pairs. Make sure the names of the properties match the keys in the structure.
This function returns void
INFO With composeRelationships=true, you can populate one-to-many, many-to-one, many-to-many, and one-to-one relationships from property values in the memento. For 'many-to-one' and 'one-to-one' relationships, the value of the property in the memento should be a single value of the primary key of the target entity to be loaded. For 'one-to-many' and 'many-to-many' relationships, the value of the property in the memento should a comma-delimited list or array of the primary keys of the target entities to be loaded.
Key
Type
Required
Default
Description
entityName
any
true
---
properties
struct
false
{}
A structure of name-value pairs to populate the new entity with
Key
Type
Required
Default
Description
entityName
string
true
---
The entity name to bind the criteria query to
useQueryCaching
boolean
false
false
Do automatic query caching for queries
queryCacheRegion
string
false
criterias.{entityName}
The queryCacheRegion name property for all queries in this criteria object
Key
Type
Required
Default
Description
target
any
Yes
---
The entity to populate
xml
any
Yes
---
The xml string or xml document object to populate with
root
string
false
The xml root node to start the population with, by default it uses the XMLRoot.
scope
string
No
Use scope injection instead of setter injection, no need of setters, just tell us what scope to inject to
trustedSetter
Boolean
No
false
Do not check if the setter exists, just call it, great for usage with onMissingMethod() and virtual properties
include
string
No
A list of keys to ONLY include in the population
exclude
string
No
A list of keys to exclude from the population
Key
Type
Required
Default
Description
target
any
Yes
---
The entity to populate
JSONString
string
Yes
---
The JSON packet to use for population
scope
string
No
Use scope injection instead of setter injection, no need of setters, just tell us what scope to inject to
trustedSetter
Boolean
No
false
Do not check if the setter exists, just call it, great for usage with onMissingMethod() and virtual properties
include
string
No
A list of keys to ONLY include in the population
exclude
string
No
A list of keys to exclude from the population
Key
Type
Required
Default
Description
target
any
Yes
---
The entity to populate
memento
struct
Yes
---
The structure of name-value pairs to try to populate the entity with
scope
string
No
Use scope injection instead of setter injection, no need of setters, just tell us what scope to inject to
trustedSetter
Boolean
No
false
Do not check if the setter exists, just call it, great for usage with onMissingMethod() and virtual properties
include
string
No
A list of keys to ONLY include in the population
exclude
string
No
A list of keys to exclude from the population
nullEmptyInclude
string
No
A list of keys to NULL when empty, specifically for ORM population. You can also specify "*" for all fields
nullEmptyExclude
string
No
A list of keys to NOT NULL when empty, specifically for ORM population. You can also specify "*" for all fields
composeRelationships
boolean
No
true
When true, will automtically attempt to compose relationships from memento
Key | Type | Required | Default | Description |
target | any | Yes | --- | The entity to populate |
qry | query | Yes | --- | The query to populate with |
rowNumber | numeric | false | 1 | The row to use to populate with. |
scope | string | No | --- | Use scope injection instead of setter injection, no need of setters, just tell us what scope to inject to |
trustedSetter | Boolean | No | false | Do not check if the setter exists, just call it, great for usage with onMissingMethod() and virtual properties |
include | string | No | --- | A list of columns to ONLY include in the population |
exclude | string | No | --- | A list of columns to exclude from the population |
Key | Type | Required | Default | Description |
entity | any | Yes | --- |
forceInsert | boolean | No | false |
flush | boolean | No | false |
transactional | boolean | No | true | Use ColdFusion transactions or not |
Key | Type | Required | Default | Description |
entities | array | Yes | --- | The array of entities to persist |
forceInsert | boolean | No | false |
flush | boolean | No | false |
transactional | boolean | No | true | Use ColdFusion transactions or not |
Key
Type
Required
Default
Description
entity
any
Yes
---
Key
Type
Required
Default
Description
entity
any
Yes
---
Key
Type
Required
Default
Description
entity
any
Yes
---