Method Signatures
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:
1
component persistent="true" name="User" extends="cborm.models.ActiveEntity"{
2
3
property name="id" column="user_id" fieldType="id" generator="uuid";
4
property name="lastName";
5
property name="userName";
6
property name="password";
7
property name="lastLogin" ormtype="date";
8
}
Copied!
Then we could do the following:
1
user = getInstance( "User" ).findByLastName( "Majano" );
2
3
users = getInstance( "User" ).findAllByLastNameLike( "Ma%" );
4
5
users = getInstance( "User" ).findAllByLastLoginBetween( "01/01/2010", "01/01/2012" );
6
7
users = getInstance( "User" ).findAllByLastLoginGreaterThan( "01/01/2010" );
8
9
users = getInstance( "User" ).findAllByLastLoginGreaterThanAndLastNameLike( "01/01/2010", "jo%" );
10
11
count = getInstance( "User" ).countByLastLoginGreaterThan( "01/01/2010" );
12
13
count = getInstance( "User" ).countByLastLoginGreaterThanAndLastNameLike( "01/01/2010", "jo%" );
Copied!
You can also use the virtual entity service instead of active entity.
1
// Get a virtual entity service via DI, there are many ways to get a virtual entity service
2
// Look at virtual entity service docs for retrieval
3
property name="userService" inject="entityservice:userService";
4
5
user = userService.findByLastName( "Majano" );
6
7
users = userService.findAllByLastNameLike( "Ma%" );
8
9
users = userService.findAllByLastLoginBetween( "01/01/2010", "01/01/2012" );
10
11
users = userService.findAllByLastLoginGreaterThan( "01/01/2010" );
12
13
users = userService.findAllByLastLoginGreaterThanAndLastNameLike( "01/01/2010", "jo%" );
14
15
count = userService.countByLastLoginGreaterThan( "01/01/2010" );
16
17
count = userService.countByLastLoginGreaterThanAndLastNameLike( "01/01/2010", "jo%" );
Copied!
If you just use a vanilla Base ORM Service, then the first argument must be the entityName:
1
// Get a virtual entity service via DI, there are many ways to get a base entity service
2
// Look at base entity service docs for retrieval
3
property name="userService" inject="entityservice";
4
5
user = userService.findByLastName( "User", "Majano" );
Copied!
Last modified 1yr ago
Copy link