@Fetch(FetchMode.JOIN)和JOIN FETCH之间有什么区别

时间:2017-05-22 15:53:36

标签: java hibernate jpa

今天我遇到了重复的查询问题(N + 1选择)。 我的问题是annotationJQL command之间的区别是什么? JOIN FETCH

对我而言,注释没有任何区别。

我收到的结果。任何人都可以解释一下吗?

这是我的班级:

@Entity
@Table(name = "tab_resp")
public class Responsavel implements Serializable, IBeanEnable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @GenField(fieldText = "Nome", columnName = "Nome", type = GenField.CHAR_STRING, isRepresentation = true)
    @Column(length = 70)
    private String nome;
    @GenField(fieldText = "Email", columnName = "Email", type = GenField.CHAR_STRING)
    @Column(length = 150)
    private String email;
    @GenField(fieldText = "Empresa", columnName = "Empresa", type = GenField.OBJECT)
    @OneToOne//(fetch = FetchType.EAGER)
    @Fetch(FetchMode.JOIN)
    private Empresa empresa;
    @GenField(fieldText = "Cargo", columnName = "Cargo", type = GenField.OBJECT)
    @OneToOne//(fetch = FetchType.EAGER)
    @Fetch(FetchMode.JOIN)    
    private Cargo cargo;
    @GenField(fieldText = "Data Cadastro", columnName = "Data Cadastro", type = GenField.DATE, dateTimeFormat = "dd/MM/yyyyy HH:mm:ss")
    @Column(name = "dh_cad")
    private LocalDateTime dataHoraRegistro;
    @GenField(fieldText = "Data Atualização", columnName = "Data Atualização", type = GenField.DATE, dateTimeFormat = "dd/MM/yyyyy HH:mm:ss")
    @Column(name = "dh_atu")
    private LocalDateTime dataHoraAtualizacao;
    @GenField(fieldText = "Ativo", columnName = "Ativo", type = GenField.BOOLEAN)
    @Convert(converter = BooleanToIntegerConverter.class)
    private Boolean ativo;
...

结果:

Hibernate: select responsave0_.id as id1_18_, responsave0_.ativo as ativo2_18_, responsave0_.cargo_id as cargo_id7_18_, responsave0_.dh_atu as dh_atu3_18_, responsave0_.dh_cad as dh_cad4_18_, responsave0_.email as email5_18_, responsave0_.empresa_id as empresa_8_18_, responsave0_.nome as nome6_18_ from tab_resp responsave0_
Hibernate: select cargo0_.id as id1_5_0_, cargo0_.ativo as ativo2_5_0_, cargo0_.dataHoraRegistro as dataHora3_5_0_, cargo0_.descricao as descrica4_5_0_ from tab_carg cargo0_ where cargo0_.id=?
Hibernate: select empresa0_.id as id1_10_0_, empresa0_.ativo as ativo2_10_0_, empresa0_.dh_atu as dh_atu3_10_0_, empresa0_.dh_cad as dh_cad4_10_0_, empresa0_.nome as nome5_10_0_ from tab_empr empresa0_ where empresa0_.id=?
Hibernate: select cargo0_.id as id1_5_0_, cargo0_.ativo as ativo2_5_0_, cargo0_.dataHoraRegistro as dataHora3_5_0_, cargo0_.descricao as descrica4_5_0_ from tab_carg cargo0_ where cargo0_.id=?
Hibernate: select cargo0_.id as id1_5_0_, cargo0_.ativo as ativo2_5_0_, cargo0_.dataHoraRegistro as dataHora3_5_0_, cargo0_.descricao as descrica4_5_0_ from tab_carg cargo0_ where cargo0_.id=?
Hibernate: select cargo0_.id as id1_5_0_, cargo0_.ativo as ativo2_5_0_, cargo0_.dataHoraRegistro as dataHora3_5_0_, cargo0_.descricao as descrica4_5_0_ from tab_carg cargo0_ where cargo0_.id=?
Hibernate: select cargo0_.id as id1_5_0_, cargo0_.ativo as ativo2_5_0_, cargo0_.dataHoraRegistro as dataHora3_5_0_, cargo0_.descricao as descrica4_5_0_ from tab_carg cargo0_ where cargo0_.id=?
Hibernate: select cargo0_.id as id1_5_0_, cargo0_.ativo as ativo2_5_0_, cargo0_.dataHoraRegistro as dataHora3_5_0_, cargo0_.descricao as descrica4_5_0_ from tab_carg cargo0_ where cargo0_.id=?
Hibernate: select cargo0_.id as id1_5_0_, cargo0_.ativo as ativo2_5_0_, cargo0_.dataHoraRegistro as dataHora3_5_0_, cargo0_.descricao as descrica4_5_0_ from tab_carg cargo0_ where cargo0_.id=?
Hibernate: select cargo0_.id as id1_5_0_, cargo0_.ativo as ativo2_5_0_, cargo0_.dataHoraRegistro as dataHora3_5_0_, cargo0_.descricao as descrica4_5_0_ from tab_carg cargo0_ where cargo0_.id=?
Hibernate: select empresa0_.id as id1_10_0_, empresa0_.ativo as ativo2_10_0_, empresa0_.dh_atu as dh_atu3_10_0_, empresa0_.dh_cad as dh_cad4_10_0_, empresa0_.nome as nome5_10_0_ from tab_empr empresa0_ where empresa0_.id=?
Hibernate: select cargo0_.id as id1_5_0_, cargo0_.ativo as ativo2_5_0_, cargo0_.dataHoraRegistro as dataHora3_5_0_, cargo0_.descricao as descrica4_5_0_ from tab_carg cargo0_ where cargo0_.id=?
Hibernate: select cargo0_.id as id1_5_0_, cargo0_.ativo as ativo2_5_0_, cargo0_.dataHoraRegistro as dataHora3_5_0_, cargo0_.descricao as descrica4_5_0_ from tab_carg cargo0_ where cargo0_.id=?
Hibernate: select cargo0_.id as id1_5_0_, cargo0_.ativo as ativo2_5_0_, cargo0_.dataHoraRegistro as dataHora3_5_0_, cargo0_.descricao as descrica4_5_0_ from tab_carg cargo0_ where cargo0_.id=?
Hibernate: select cargo0_.id as id1_5_0_, cargo0_.ativo as ativo2_5_0_, cargo0_.dataHoraRegistro as dataHora3_5_0_, cargo0_.descricao as descrica4_5_0_ from tab_carg cargo0_ where cargo0_.id=?
Hibernate: select cargo0_.id as id1_5_0_, cargo0_.ativo as ativo2_5_0_, cargo0_.dataHoraRegistro as dataHora3_5_0_, cargo0_.descricao as descrica4_5_0_ from tab_carg cargo0_ where cargo0_.id=?

带有Fecth选项的JQL只会产生一个查询命令。

dao.consulta("select r from Responsavel r JOIN FETCH r.cargo c JOIN FETCH r.empresa e");

结果:

Hibernate: select responsave0_.id as id1_18_0_, cargo1_.id as id1_5_1_, empresa2_.id as id1_10_2_, responsave0_.ativo as ativo2_18_0_, responsave0_.cargo_id as cargo_id7_18_0_, responsave0_.dh_atu as dh_atu3_18_0_, responsave0_.dh_cad as dh_cad4_18_0_, responsave0_.email as email5_18_0_, responsave0_.empresa_id as empresa_8_18_0_, responsave0_.nome as nome6_18_0_, cargo1_.ativo as ativo2_5_1_, cargo1_.dataHoraRegistro as dataHora3_5_1_, cargo1_.descricao as descrica4_5_1_, empresa2_.ativo as ativo2_10_2_, empresa2_.dh_atu as dh_atu3_10_2_, empresa2_.dh_cad as dh_cad4_10_2_, empresa2_.nome as nome5_10_2_ from tab_resp responsave0_ inner join tab_carg cargo1_ on responsave0_.cargo_id=cargo1_.id inner join tab_empr empresa2_ on responsave0_.empresa_id=empresa2_.id

0 个答案:

没有答案