Hibernate tips 2

Formula

(from : http://weishuwei.iteye.com/blog/68030)

1,formula ="()",里面的是sql语句,字段和表名都应该和数据库相应,而不是字段,若带有参数如cur.id= currencyID,这个currencyID才是对象的东东.
2,formula ="( sql )",这个括号不能少,不然会报错,我试了几次,没括号就报错,添上就没问题
3,操作字段一定要用别名

 

(from:http://www.cnblogs.com/iloveu/archive/2009/01/20/1378810.html)

* formula公式里的字段都是数据库里的字段名,除了查询条件参数是要和配置文件里的字段匹配。

 

(from:http://zhiweiv.iteye.com/blog/361525)

这下子对于formula里的sql语句为什么必须放在括号里应该明白了,因为formula里面的值会作为一条sql语句select和from之间的内容,我们把sql语句放到括号里实际上就是做为一条子查询执行!!! 例如最开始示例的的rank对应的sql语句就应如下样子

 

dynamic-update

- when true, dynamic generate sql, if any changes for the property, it will appear in update sql, otherwise, wont be included.

 

dynamic-insert

- when true, insert will dynamic generate sql, if the property is NOT null, it will be included in SQL, otherwise, wont.

 

lazy-loading

1. If no record in database, still lazy-load uninitialized object.

2. only throw exception when call method of the proxy object

3. when access ID: getID(). wont trigger any database query

 

Hibernate performance

1. lazy-loading

2. batch-size: the size of elements to retrieve each time

3. one-to-one & many-to-one, better use outer-join to fetch data, it will reduce query times.

4. max-fetch-depth: outer join tables depths...

 

Join Query

1. hibernate configure: outer-join="true", sql will be left outer join

2. HQL e.g. Person has items, "from Person p inner join p.items" No "on" like the one in SQL

3. SQL, we have "select xxx inner join xxx ON...."

4. HQL join will return a list of array which contains joined objects. e.g. "from customer c left outer join c.order"; if we only want customer, we can use "Select c from customer c left outer join c.order"

5. HQL join fetch will get first object and load second fetch object as reference

6. inner-join & outer-join , result is all arrays which contains joined objects

 

HQL & Criteria

* hql will check whether passed object has map or not

* criteria will load all implementor of given class.

* check field null using "is null", not "=null" which wont get any result (might has grammar error)

 

Implict Join applys to "Many-to-one" and "one-to-one" relationship. Not "One-to-Many"

 

 left/right (outer) join : outer can be omited

 

Hibernate configure

use_query_cache=true, is not enought, need set Query.setCachable("true")

 

Query or Criteria result

1. If more than one result return, need use setMaxResult() to restrict output and then use uniqueResult

Criterial #Expression #Order

 

你可能感兴趣的:(sql,C++,c,Hibernate,C#)