简体   繁体   English

网格产生太多的休眠查询-FetchMode.JOIN不起作用

[英]Grid produces too many hibernate queries - FetchMode.JOIN doesn't work

I'm developing a webapp using Spring MVC and Hibernate. 我正在使用Spring MVC和Hibernate开发一个webapp。 Thing is, that I need to show all my customer's clients, and each client has another entity associated ("Cobrador", I don't know the english translation here, sorry), I'm using JQgrid for such goal. 问题是,我需要向所有客户的客户展示,并且每个客户都有另一个关联的实体(“ Cobrador”,抱歉,我不知道这里的英文翻译),我正在使用JQgrid来实现这一目标。 When I execute the grid, I see in the log: 当执行网格时,我在日志中看到:

Hibernate: select cliente0_.id as id1_0_, cliente0_.activo as activo2_0_, cliente0_.apellido as apellido3_0_, cliente0_.cobrador as cobrador8_0_, cliente0_.dni as dni4_0_, cliente0_.email as email5_0_, cliente0_.nombre as nombre6_0_, cliente0_.telefono as telefono7_0_ from clientes cliente0_ where cliente0_.activo=1
Hibernate: select cobrador0_.id as id1_1_0_, cobrador0_.activo as activo2_1_0_, cobrador0_.apellido as apellido3_1_0_, cobrador0_.dni as dni4_1_0_, cobrador0_.email as email5_1_0_, cobrador0_.nombre as nombre6_1_0_, cobrador0_.telefono as telefono7_1_0_ from cobradores cobrador0_ where cobrador0_.id=?
Hibernate: select cobrador0_.id as id1_1_0_, cobrador0_.activo as activo2_1_0_, cobrador0_.apellido as apellido3_1_0_, cobrador0_.dni as dni4_1_0_, cobrador0_.email as email5_1_0_, cobrador0_.nombre as nombre6_1_0_, cobrador0_.telefono as telefono7_1_0_ from cobradores cobrador0_ where cobrador0_.id=?
Hibernate: select cobrador0_.id as id1_1_0_, cobrador0_.activo as activo2_1_0_, cobrador0_.apellido as apellido3_1_0_, cobrador0_.dni as dni4_1_0_, cobrador0_.email as email5_1_0_, cobrador0_.nombre as nombre6_1_0_, cobrador0_.telefono as telefono7_1_0_ from cobradores cobrador0_ where cobrador0_.id=?

Basically getting the clients, and then, for each client go gets the associated "cobrador". 基本上获取客户,然后为每个客户获取关联的“ cobrador”。 My Client entity is configured as follow: 我的客户实体配置如下:

@Entity
@Table(name="clientes")
public class Cliente {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;    
private String apellido;
private String nombre;
private int dni;
private String telefono;
private String email;
private boolean activo;
@ManyToOne
@JoinColumn(name="cobrador")
@Fetch(FetchMode.JOIN)
private Cobrador cobrador;

//Contructors, getters and setters
}

BTW: The final hibernate execution is: 顺便说一句:最终的休眠执行是:

@Override
@Transactional
public List<T> getAllFiltering(String filter) { 
    Query q = sessionFactory.getCurrentSession().createQuery("from " + type.getSimpleName() + " " + filter);
    return q.list();
}

Where is and filter is " where activo=true". where is和filter是“ where activo = true”。

Is there anyway to configure this relationship in order to execute only 1 query when loading the grid? 无论如何,在加载网格时是否要配置此关系以便仅执行1个查询?

Thanks in advance! 提前致谢!

I know it is not very convenient but Hibernate will not use the fetch strategy if you are using an HQL query (IE using the createQuery method). 我知道这不是很方便,但是如果您使用的是HQL查询(使用createQuery方法的IE),则Hibernate将不会使用获取策略。 If you want to make it work, you must use the Criteria API or specify the join in the HQL query. 如果要使其起作用,则必须使用Criteria API或在HQL查询中指定联接。

In your case the query might be something like this : 在您的情况下,查询可能是这样的:

from Cliente c left join fetch c.cobrador

From the Hibernate documentation : Hibernate文档中

The fetch strategy defined in the mapping document affects: 映射文档中定义的获取策略会影响:

  • retrieval via get() or load() 通过get()或load()检索
  • retrieval that happens implicitly when an association is navigated 导航关联时隐式发生的检索
  • Criteria queries 条件查询
  • HQL queries if subselect fetching is used HQL查询是否使用子选择获取

As you can see, the fetch strategy defined doesn't affect HQL queries if JOIN is the fetchMode. 如您所见,如果JOIN是fetchMode,则定义的访存策略不会影响HQL查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM