3 types of mapping for inheritance
- map for each solid subclass (parent class is abstracted), can not support polymophy . e.g. "select from employ" wont retrieve hourly_employee and monthly_employee
- Only one class with flag to differeniate children; support polymorphism "from employee" will pickup "hourly_employee", "monthly_employee'
- one map for each table (parent + children)
Hibernate collection
1. Set
2. List - use index property to set index no, table must have index column
3. Map - use index property to set key, table must have key column defined.
One-to-One
If a class has two same type of property e.g. HomeAddress & CompanyAddress.
customer mapping uses: many-to-one
Address uses: one-to-one, we can setup property-ref="homeAddress", so that we can setup binary-relationship between homeAddress and Customer. but NOT companyAddress and Customer again.
Can use one-to-one on both side. they will share one primary key
Hibernate Configure
max_fetch_depth : defines length of relationship from the entity you want to retrieve.
e.g. In this relationship School--->Student--->Subject
if you want to read School and set max_fetch_depth '1', then you can only retrieve School + Students.
fetch strategy
1. Select - lazy loading
2. Join - immediate load related entities.
3. subselect - use subQuery
batch-size (select one entity which has reference to a collection)
Help hibernate to reduce n times of query into one to save time.
Hibernate optimize the second SELECT (either lazy or non-lazy) by fetching up to N other collections
To tell Hibernate to use the latter solution is to tell it that a certain class is batch-able . You do this by adding the batch-size
attribute to either a.) the entity definition for the association being fetched (e.g. the definition for the Owner
class) or b.) the collection definition on a class with a collection mapping
* 是设定对数据库进行批量删除,批量更新和批量插入的时候的批次大小,有点相当于设置Buffer缓冲区大小的意思。
@BatchSize=16
defines one-to-many relationship
fetch-size
Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed. The number of rows specified affects only result sets created using this statement. If the value specified is zero, then the hint is ignored. The default value is zero.
*设定JDBC 的Statement读取数据的时候每次从数据库中取出的记录条数。
* A non-zero value determines the JDBC fetch size (calls Statement.setFetchSize()
).
The setFetchSize() control how many rows are fetched at a time by the JDBC driver. So, if you for example have setMaxResults(1000) and setFetchSize(100) the query will return no more than 1000 rows, and will do so in batches of 100 rows at a time. A larger fetch size will typically use more memory since the JDBC driver will have to cache more information, but may improve execution time since less time is needed for network communication. How much performance improvement you can get depends on a lot of factors, such as total number of rows, network speed, available memory, the JDBC driver implementation, etc. I guess a lot also depends on which query method you use. For example, Query.list() will load all entities into a list (and use more memory), but Query.scroll() will load data as needed. Combining scroll() with evict() will reduce the memory used for the first-level cache as well.
So, setMaxResults() and setFetchSize() are not something that you choose one or the other. They are both useful on their own or together and the "best" values may vary from situation to situation depending on the data, hardware limitations, and more.
Reference:
http://www.mkyong.com/hibernate/hibernate-fetching-strategies-examples/
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html
http://community.jboss.org/wiki/AShortPrimerOnFetchingStrategies
http://www.javalobby.org/java/forums/m91885142.html
http://hi.baidu.com/lifa868/blog/item/1326b6da3f60696cd1164ef2.html/cmtid/3e42a1dc3a02b4d78c1029a8