Hibernate Mapping

3 types of mapping for inheritance

  1. 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
  2. Only one class with flag to differeniate children; support polymorphism "from employee" will pickup "hourly_employee", "monthly_employee'
  3. 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缓冲区大小的意思。

*A non-zero value enables use of JDBC2 batch updates by Hibernate. e.g. recommended values between 5 and 30

 

@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() ).

https://forum.hibernate.org/viewtopic.php?f=1&t=1002569 写道
The setMaxResults() control how many rows the query will return. Even if there are more matching rows only the maximum specified number are returned. The setMaxResults() is typically used together with setFirstResult() to implement paging. Eg. the first query returns records 1 to 1000, the second query returns 1001 to 2000, and so on.

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

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值