Japanese Builders Reference

Last updated by admin 3 years ago

Hibernate Criteria ???? {excerpt:hidden=true} Hibernate Criteria Builder {excerpt}

?? {excerpt:hidden=true} Description {excerpt}

{excerpt:hidden=true} A builder for creating criteria-based queries analogous to those found in the Hibernate Criteria API, the nodes on this builder map the the static methods found in the Restrictions class of the Hibernate Criteria API. Example Usage: {excerpt} criteria-based???????????????Hibernate Criteria API??????????????????????Hibernate Criteria API?Restrictions ????????????????????????

def c = Account.createCriteria()
def results = c {
	like("holderFirstName", "Fred%")
	and {
		between("balance", 500, 1000)
		eq("branch", "London")
	}
	maxResults(10)
	order("holderLastName", "desc")
}

Criteria???????????????? {excerpt:hidden=true} Setting properties in the Criteria instance {excerpt}

{excerpt:hidden=true} If a node within the builder tree doesn't match a particular criterion it will attempt to set a property on the Criteria object itself. Thus allowing full access to all the properties in this class. The below example calls "setMaxResults" and "setFirstResult" on the Criteria instance: {excerpt} ?????????????????????????????????????????Criteria?????????????????????????????????????????????????????????????????"setMaxResults"?"setFirstResult"?Criteria???????????????

import org.hibernate.FetchMode as FM
....
def results = c.list {
	maxResults(10)
	firstResult(50)
	fetchMode("aRelationship", FM.EAGER)
}
{excerpt:hidden=true} If you invoke the builder with no method name such as: {excerpt} ????????????????????????
c { … }
{excerpt:hidden=true} It defaults to the 'list' call, if this is not desired use the specific methods 'list','get' and 'scroll' which return a list, a unique result and a scrollable result set respectively. {excerpt} 'list'??????????????????????????????????scrollable????? 'list','get','scroll'?????????????????????????

??????????? {excerpt:hidden=true} Querying Associations {excerpt}

{excerpt:hidden=true} Associations can be queried by having a node that matches the property name. For example say the Account class had many Transaction objects: {excerpt} ???????????????????????????????????????????????Account???????Transaction??????????????

class Account {
    …
    def relatesToMany = [transactions:Transaction]
    Set transactions
    …
}
{excerpt:hidden=true} We can query this association by using the property name "transaction" as a builder node: {excerpt} ???????"transaction"?????????????????????????????????
def c = Account.createCriteria()
def now = new Date()
def results = c.list {
       transactions {
            between('date',now-10, now)
       }
}
{excerpt:hidden=true} The above code will find all the accounts that have performed transactions within the last 10 days. {excerpt} ????????10????transactions??????accounts???????

{excerpt:hidden=true} You can also nest such association queries within logical blocks: {excerpt} ???????association??????????????????

def c = Account.createCriteria()
def now = new Date()
def results = c.list {
     or {
        between('created',now-10,now)
        transactions {
             between('date',now-10, now)
        }
     }
}
{excerpt:hidden=true} Here we find all accounts that have either performed transactions in the last 10 days OR have been recently created in the last 10 days. {excerpt} ????10????transactions??????accounts ???10???????????????????

??????????????????? {excerpt:hidden=true} Querying with Projections {excerpt}

{excerpt:hidden=true} Projections to be used to customise the results. To use projections you need to define a "projections" node within the criteria builder tree. There are equivalent methods within the projections node to the methods found in the Hibernate Projections class: {excerpt} ???????????????????????????????????????????criteria??????????"projection"????????????????projections??????????Hibernate Projections?????????

def c = Account.createCriteria()

def numberOfBranches = c.get { projections { countDistinct('branch') } }

Scrollable Results???  {excerpt:hidden=true} Using Scrollable Results {excerpt}

{excerpt:hidden=true} You can use Hibernate's ScrollableResults feature by calling the scroll method: {excerpt} scroll??????????Hibernate?ScrollableResults????????

def results = crit.scroll {
      maxResults(10)
}
def f = results.first()
def l = results.last()
def n = results.next()
def p = results.previous()

def future = results.scroll(10) def accountNumber = results.getLong('number')

{excerpt:hidden=true} To quote the documentation of Hibernate ScrollableResults: {excerpt} Hibernate ScrollableResults???????????????
A result iterator that allows moving around within the results by arbitrary increments. The Query / ScrollableResults pattern is very similar to the JDBC PreparedStatement/ ResultSet pattern and the semantics of methods of this interface are similar to the similarly named methods on ResultSet.

Contrary to JDBC, columns of results are numbered from zero.

??? ?????? {excerpt:hidden=true} Node Reference {excerpt}

|| ??? || ?? || ? || | and | Logical AND operator
?? AND ??? |

and {
   between("balance", 500, 1000)
   eq("branch", "London")
}
| | between | Where the property value is between to distinct values
????????????? |
between("balance", 500, 1000)
| | eq | Where a property equals a particular value
??????????????? |
eq("branch", "London")
| | eqProperty | Where one property must equal another
??????????????????????? |
eqProperty("lastTransaction","firstTransaction")
| | gt | Where a property is greater than a particular value
???????????????? |
gt("balance",1000)
| | gtProperty | Where a one property must be greater than another
???????????????????????? |
gtProperty("balance","overdraft")
| | ge | Where a property is greater than or equal to a particular value
?????????????????????? |
ge("balance",1000)
| | geProperty | Where a one property must be greater than or equal to another
?????????????????????? |
geProperty("balance","overdraft")
| | idEq | Where an objects id equals the specified value
?????????????????????? |
idEq(1)
| | ilike | A case sensitive 'like' expression 
???? like ?? ?SQL?ILIKE? |
ilike("holderFirstName","Steph%")
| | in | Where a one property is contained within the specified list of values
?????????????????????????? |
in("holderAge",[18..65])
| | isEmpty | Where a collection property is empty
?????????????? |
isEmpty("transactions")
| | isNotEmpty | Where a collection property is not empty
?????????????????? |
isNotEmpty("transactions")
| | isNull | Where a property is null
???????null |
isNull("holderGender")
| | isNotNull | Where a property is not null
??????????????? |
isNotNull("holderGender")
| | lt | Where a property is less than a particular value
???????????????? |
lt("balance",1000)
| | ltProperty | Where a one property must be less than another
???????????????????????? |
ltProperty("balance","overdraft")
| | le | Where a property is less than or equal to a particular value
????????????? |
le("balance",1000)
| | leProperty | Where a one property must be less than or equal to another
?????????????????????? |
leProperty("balance","overdraft")
| | like | Equivalent to SQL like expression
SQL? like ?? ???  |
like("holderFirstName","Steph%")
| | ne | Where a property does not equals a particular value
????????????????? |
ne("branch", "London")
| | neProperty | Where one property does not equal another
????????????????? |
neProperty("lastTransaction","firstTransaction")
| | not | Negates an expression, logical NOT
??????? NOT |
not {
   between("balance", 500, 1000)
}
| | or | Logical OR operator
?? OR ??? |
or {
   between("balance", 500, 1000)
   eq("branch", "London")
}
| | order | Order the results by a particular property
?????????????????????? |
order("holderLastName", "desc")
| | sizeEq | Where a collection property's size equals a particular value
????????????????????????? |
sizeEq("transactions", 10)
|