`

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

 

 

 

分享到:
评论

相关推荐

    hibernate-mapping参数详解

    ### Hibernate Mapping 参数详解 在 Hibernate 中,为了将 Java 对象映射到数据库表,并实现对象关系映射(ORM),开发者需要定义一系列配置属性来指导 Hibernate 如何进行转换。本文将详细解析 `hibernate-mapping...

    XDoclet插件包以及从Java持久化类生成hibernate mapping小例子

    在“XDoclet插件包以及从Java持久化类生成hibernate mapping小例子”中,我们重点关注的是如何通过XDoclet从Java持久化类生成与Hibernate相关的配置文件。首先,我们需要在Java类中添加特定的XDoclet注解,这些注解...

    hibernate-mapping

    《Hibernate Mapping:深入理解与应用》 Hibernate Mapping是Java领域中一种重要的对象关系映射(ORM)技术,它使得开发者可以使用面向对象的方式来操作数据库,极大地简化了数据存取的操作。在Hibernate中,...

    ant hibernate mapping 生成映射文件 生成配置文件

    请把这个文件解压后方到一个盘下面.在build.xml中的 指出这个文件存放的位置.然后就可以使用了...里面有一个成功的例子 请读者参考 在构建域对象的时候一定要指明在配置文件中的配置信息..

    hibernate mapping 文档

    详细的hibernate映射文件讲解 , 包含主键映射 , 属性映射 , 容器映射等 ,让你更轻松的使用hibernate

    MyEclipse10.7由表自动生成Hibernate实体对象

    在使用MyEclipse 10.7开发JavaWeb项目时,开发者可以通过内置的MyEclipse Database Explorer工具,利用Hibernate反向工程功能,从数据库表自动生成对应的Hibernate实体类。这一过程极大地简化了数据库操作与Java对象...

    详解 hibernate mapping配置

    本文将深入探讨`hibernate mapping`配置,包括如何在`hibernate.cfg.xml`文件中引入映射文件,以及实体类与数据库表之间的映射规则。 首先,每个Hibernate应用在启动时通常会加载一个配置文件——`hibernate.cfg....

    Hibernate 离线的配置方法(hibernate-mapping-3.0.dtd)

    因为Hibernate在读出hbm.xml文件时需要通过网络读取到hibernate-mapping-3.0.dtd 文件。 如果没有网络不能正常工作。 所以提供上述文件。 以及hibernate-mapping-3.0.dtd,hibernate-configuration-3.0.dtd提供下载...

    hibernate-mapping-3.0.dtd

    hibernate-mapping-3.0.dtd 配置后,就会在xml中进行提示

    hibernate的dtd 包含hibernate.properties

    在这个主题中,我们主要关注的是Hibernate的配置文件`hibernate.properties`以及它的DTD(Document Type Definition)文件,包括`hibernate-configuration-3.0.dtd`和`hibernate-mapping-3.0.dtd`。 首先,`...

    hibernate-configuration-3.0.dtd、hibernate-mapping-3.0.dtd

    《深入理解Hibernate配置与映射:hibernate-configuration-3.0.dtd与hibernate-mapping-3.0.dtd解析》 在Java世界里,Hibernate作为一款强大的对象关系映射(ORM)框架,极大地简化了数据库操作。而`hibernate-...

    hibernate。hbm.xml配置详解

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="包名"> 类名" table="表名"> <!-- 主键配置 --> <!-- 普通属性...

    hibernate如何用O/R Mapping框架来实现我们的数据层

    ### Hibernate与O/R Mapping框架详解 #### 一、O/R Mapping概述 O/R Mapping(Object-Relational Mapping)即对象关系映射,是一种程序技术,用于将对象模型表示的应用程序对象与关系数据库中的表进行相互转换。在...

    hibernate配置文件详解

    Hibernate 是一个流行的 ORM(Object-Relational Mapping)框架,用于简化 Java 应用程序中的数据库交互。 Hibernate 的配置文件是其核心组件之一,用于定义 Hibernate 的运行期参数。下面我们将详细介绍 Hibernate ...

    Hibernate's JPA.rar

    标题"Hibernate's JPA.rar"表明这是一个关于使用Hibernate实现Java Persistence API (JPA)的压缩文件,可能包含示例代码、配置文件或者必要的库文件。Hibernate是一个流行的对象关系映射(ORM)框架,它使得Java...

Global site tag (gtag.js) - Google Analytics