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) }
c { … }??????????? {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
…
}def c = Account.createCriteria()
def now = new Date()
def results = c.list {
transactions {
between('date',now-10, now)
}
}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} 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')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("balance", 500, 1000)??????????????? |
eq("branch", "London")
??????????????????????? |
eqProperty("lastTransaction","firstTransaction")
???????????????? |
gt("balance",1000)???????????????????????? |
gtProperty("balance","overdraft")
?????????????????????? |
ge("balance",1000)?????????????????????? |
geProperty("balance","overdraft")
?????????????????????? |
idEq(1)
???? like ?? ?SQL?ILIKE? |
ilike("holderFirstName","Steph%")
?????????????????????????? |
in("holderAge",[18..65])?????????????? |
isEmpty("transactions")?????????????????? |
isNotEmpty("transactions")???????null |
isNull("holderGender")??????????????? |
isNotNull("holderGender")???????????????? |
lt("balance",1000)???????????????????????? |
ltProperty("balance","overdraft")
????????????? |
le("balance",1000)?????????????????????? |
leProperty("balance","overdraft")
SQL? like ?? ??? |
like("holderFirstName","Steph%")
????????????????? |
ne("branch", "London")
????????????????? |
neProperty("lastTransaction","firstTransaction")
??????? NOT |
not {
between("balance", 500, 1000)
}?? OR ??? |
or {
between("balance", 500, 1000)
eq("branch", "London")
}?????????????????????? |
order("holderLastName", "desc")
????????????????????????? |
sizeEq("transactions", 10)


