在Hibernate中运行多个查询的模型类。 [英] Model Class running Multiple Queries in Hibernate..!

查看:116
本文介绍了在Hibernate中运行多个查询的模型类。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用hibernate关系映射时,使用

  @OneToOne 
@oneToMany
@ManyToOne

关系它为每个提取做出多个查询。



<任何人都可以帮助我理解这种关系是如何工作的。

解决方案

Hibernate关系适用于不同的提取策略。 Hibernate为检索数据提供了4种策略:

SELECT

  @OneToMany(mappedBy =tableName,cascade = CascadeType.ALL)
@Column (name =id)
@Fetch(FetchMode.SELECT)




在这个方法中有多个SQL被触发。第一个触发
用于检索父表中的所有记录。其余的
因为检索每个家长记录的记录而被解雇。这基本上是
的N + 1问题。第一个查询从数据库中检索N个记录,在
这种情况​​下为N个父记录。对于每个Parent,新的查询将检索
Child。因此,对于N个父项,N个查询从
子表中检索信息。


JOIN p>

  @OneToMany(mappedBy =tableName,cascade = CascadeType.ALL)
@Column(name =id)
@Fetch(FetchMode.JOIN)




除了事实上所有的
数据库检索事件都发生在JOIN获取中,而不像在SELECT
中发生,因为它在需要的基础上发生。这可能会成为重要的
表现考虑。


SUBSELECT

  @OneToMany(mappedBy =tableName,cascade = CascadeType.ALL)
@Column(name =id)
@ Fetch(FetchMode.SUBSELECT)




激发了两个SQL。一个检索所有Parent,第二个使用WHERE子句中的
SUBSELECT查询检索具有
匹配的父ID的所有子项。




BATCH

  @OneToMany(mappedBy =tableName,cascade = CascadeType.ALL)
@Column(name =id)
@@ BatchSize(size = 2)




批量大小映射到检索其子级的父数。
因此,我们可以指定一次获取的记录数。但是
将执行多个查询!!



一对多&多对多允许 - 加入,选择和子选择

多对一&一对一允许 - 加入和选择







< Hibernate还可以区分(何时获取关联)

p>


关联,集合或属性会在
加载Parent时立即获取。 (lazy =false)

2. 延迟收集抓取 -


当应用程序在
上调用对集合的操作时,会获取集合。 (lazy =true)


3。超懒惰collection fetching -


集合的单个元素可以根据需要从数据库
中访问,Hibernate尝试不提取除非绝对需要(适用于非常大的集合)
(lazy =extra)


4. 代理抓取 -


当一个方法不是$
(lazy =proxy)


5。 No-proxy 获取 -


当实例变量为
时,会获取单值关联与代理抓取相比,这种方法不太懒惰(lazy =no-proxy)




6. 懒惰属性提取 -


属性或者在访问实例
变量时获取单值关联。 (lazy =true)



一对多&多对多允许立即使用,延迟使用,额外使用延迟功能

多对一&一对一允许立即代理,无代理


When I use hibernate relations mapping using

@OneToOne 
@oneToMany
@ManyToOne

relations its making multiple Queries, for every fetch.

Can anyone help me understanding how the relations work..??

解决方案

The Hibernate Relations Works with different Fetch Strategies..!!

Hibernate provides 4 strategies for retrieving data:

SELECT

@OneToMany(mappedBy="tableName", cascade=CascadeType.ALL)
@Column(name="id") 
@Fetch(FetchMode.SELECT)

In this Method there are Multiple SQLs fired. This first one is fired for retrieving all the records in the Parent table. The remaining are fired for retrieving records for each Parent Record. This is basically the N+1 problem. The first query retrieves N records from database, in this case N Parent records. For each Parent a new query retrieves Child. Therefore for N Parent, N queries retrieve information from Child table.

JOIN

@OneToMany(mappedBy="tableName", cascade=CascadeType.ALL)
@Column(name="id")
@Fetch(FetchMode.JOIN) 

This is similar to the SELECT fetch strategy except that fact that all database retrieval take place upfront in JOIN fetch unlike in SELECT where it happens on a need basis. This can become an important performance consideration.

SUBSELECT

 @OneToMany(mappedBy="tableName", cascade=CascadeType.ALL)
 @Column(name="id")
 @Fetch(FetchMode.SUBSELECT)

Two SQLs are fired. One to retrieve all Parent and the second uses a SUBSELECT query in the WHERE clause to retrieve all child that has matching parent ids.

BATCH

@OneToMany(mappedBy="tableName", cascade=CascadeType.ALL)
@Column(name="id")
@@BatchSize(size=2)

The batch size maps to the number of Parent whose child are retrieved. So we can specify the number of records to be fetched at a time.But Multiple queries will be executed.!!

one-to-many & many-to-many allows - join, Select and SubSelect

many-to-one & one-to-one allows - Join and Select


Hibernate also distinguishes between (when is the associations are fetched)

1.Immediate fetching -

an association, collection or attribute is fetched immediately, when the Parent is loaded. (lazy="false")

2.Lazy collection fetching -

a collection is fetched when the application invokes an operation upon that collection. (This is the default for collections.(lazy="true")

3."Extra-lazy" collection fetching -

individual elements of the collection are accessed from the database as needed. Hibernate tries not to fetch the whole collection into memory unless absolutely needed (suitable for very large collections) (lazy="extra")

4.Proxy fetching -

a single-valued association is fetched when a method other than the identifier getter is invoked upon the associated object. (lazy="proxy")

5."No-proxy" fetching -

a single-valued association is fetched when the instance variable is accessed. Compared to proxy fetching, this approach is less lazy.(lazy="no-proxy")

6.Lazy attribute fetching -

an attribute or single valued association is fetched when the instance variable is accessed. (lazy="true")

one-to-many & many-to-many allows Immediate, Layzy, Extra Lazy

many-to-one & one-to-one allows Immediate Proxy, No Proxy

这篇关于在Hibernate中运行多个查询的模型类。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆